Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.
glebi574 edited this page Dec 9, 2023 · 18 revisions

content/entity.lua defines pplaf.entity - Entity framework.
Examples: assets/entities/

Overview

Entity type

To define PPLAF entity, first we have to create file that returns table with entity's type. Many things are optional(only group parameter is compulsory), so basic PPLAF entity looks like:

return {
  group = 'player',
}

Now lets take a look at other PPLAF functionality:

local hp = 10
return {
  hp = hp
  group = 'player',
  animation = 'rotating_triangle',
  proto = {hp = hp},
  constructor = function(entity, x, y, ...) end,
  destructor = function(entity, ...) end,
  ai = function(entity) end,
}
  • hp - that's just a variable. You can define any variable in entity.
  • group - defines in which group entity will be stored. That's useful, as it allows you to iterate through certain entities, like enemies, players, bullets, etc. group has to be defined first by add_group().
  • animation - entity can be animated. You can set this parameter to name of animation type and entity will be animated automatically. See how to set up animation types in animation.
  • proto - if presented, proto will be set as metatable of the entity. So, entity will have access to any variables in proto.
  • constructor - if presented, it will be called upon entity creation. entity is entity that was created, ... are any parameters you've passed to create function. You have to define entity.id for everything to work properly.
  • destructor - if presented, it will be called upon destroyA() call.
  • ai - if presented, this function will be called every tick if entity is alive and not exploding.

There are other variables, defined upon type load:

  • file_path - path of file with entity type.
  • folder_path - if you use load_by_typed_dir(), path to folder containing file with entity type will be stored.

Also function type:create(x, y, ...) assigned to type on its load. It calls pplaf.entity.create().
We defined everything, that PPLAF can process, so let's take a look at entity maintenance.

  • create() - this function creates entity, assigns proto to it, defines several function depending on what is stored in entity's type and calls constructor if one was defined.
  • ai() - while entity is alive, ai function is called, if it was defined. AI and entity destruction are maintained by main() function. If entity was destroyed, it won't be processed. It's somewhat hard to manually define if destroyed entity was already processed, or not, because of how entities are stored and processed by PPLAF, so be careful.
  • destruction - PPLAF defines entity:destroy() as prototype of pewpew.entity_destroy(id). Also PPLAF defines entity:destroyA(...), which calls destructor and removes entity from PPLAF. You have to call destroyA(), or entity will be stuck in PPLAF storage. destructor is optional, as there are multiple ways you can destroy entity.
    entity type was created, it's time to load it for further use:
pplaf.entity.load_by_typed_files('/dynamic/assets/entities/',
  'strange_triangle'
)

Entity

We defined entity type and created entity. Let's see, what stored in it:

  • hp - remember, every variable defined in proto will be available in entity.
  • id - entity's id.
  • type - table with entity type.
  • animation - table with information, required for animation framework to work, if animation was defined in entity type.
  • __indexP - internal value, used for entity destruction. Don't modify if you don't know what you're doing.

pewpew functions are assigned to entity with slightly changed names. Usually these changes remove customizable_entity_ or entity_. You can also assign any function or value in proto or manually in constructor.
If animation field exists, function entity:set_animation_variation(index) will be available. Index is variation index in range [0; N - 1].
To create loaded entity you call create():

pplaf.entity.create(100fx, 100fx, 'strange_triangle')

pplaf.entity

Variables

pewpew_proto - prototype of pewpew functions, that can be used with custom entities. For example: entity:set_mesh(path, index). See pewpew_proto.lua for more function prototypes.
types - table, containing loaded types with type names as their keys.

Functions

nil add_group(str group_name)

Creates group.

pplaf.entity.add_group'player'

nil add_groups(...)

Creates groups.

pplaf.entity.add_groups('player', 'player_bullet')

table get_group(str group_name)

Returns iteratable group.

local players = pplaf.entity.get_group'player'

nil load_by_typed_dir(str path, ...)

Loads entities from folder. Folder must contain folders, named by names, presented in ..., and those must contain file with entity type, named as entity.lua.

pplaf.entity.load_by_typed_dir('/dynamic/assets/entities/',
  'rotating_triangle',
  'strange_triangle'
)

nil load_by_typed_files(str path, ...)

Loads entities from folder. Folder must contain files, named as presented in ....

pplaf.entity.load_by_typed_files('/dynamic/assets/entities/',
  'rotating_triangle',
  'strange_triangle'
)

nil def_types_in_pplaf()

Any typed declared available in pplaf.entity.types now will be available in pplaf.entity.

def_types_in_pplaf()
pplaf.entity.strange_triangle:create(0fx, 0fx)

nil def_types_globally()

Any typed declared available in pplaf.entity.types now will be available globally.

def_types_globally()
strange_triangle:create(0fx, 0fx)

table create(fx x, fx y, str type, ...)

Creates entity in coordinates (x; y) of type and returns it. ... arguments are passed to constructor, if one was defined.

local entity = pplaf.entity.create(100fx, 100fx, 'rotating_triangle')

nil main()

Maintains entities' AI and destruction.

pplaf.entity.main()

Clone this wiki locally