-
Notifications
You must be signed in to change notification settings - Fork 0
entity
content/entity.lua defines pplaf.entity - Entity framework.
Examples: assets/entities/
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 whichgroupentity will be stored. That's useful, as it allows you to iterate through certain entities, like enemies, players, bullets, etc.grouphas to be defined first byadd_group().animation- entity can be animated. You can set this parameter to name ofanimation typeand entity will be animated automatically. See how to set up animation types in animation.proto- if presented,protowill be set asmetatableof the entity. So, entity will have access to any variables inproto.constructor- if presented, it will be called upon entity creation.entityis entity that was created,...are any parameters you've passed tocreatefunction. You have to defineentity.idfor everything to work properly.destructor- if presented, it will be called upondestroyA()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 useload_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, assignsprototo 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,aifunction is called, if it was defined. AI and entity destruction are maintained bymain()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 definesentity:destroy()as prototype ofpewpew.entity_destroy(id). Also PPLAF definesentity:destroyA(...), which calls destructor and removes entity from PPLAF. You have to calldestroyA(), or entity will be stuck inPPLAFstorage.destructoris optional, as there are multiple ways you can destroy entity.
entity typewas created, it's time to load it for further use:
pplaf.entity.load_by_typed_files('/dynamic/assets/entities/',
'strange_triangle'
)We defined entity type and created entity. Let's see, what stored in it:
hp- remember, every variable defined inprotowill be available inentity.id- entity's id.type- table withentity type.animation- table with information, required foranimationframework to work, ifanimationwas defined inentity 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')- add_group(str
group_name) - add_groups(str
group_name) - get_group(str
group_name) - load_by_typed_dir(str
path,...) - def_types_in_pplaf
- def_types_globally
- load_by_typed_files(str
path,...) - create(fx
x, fxy, strtype,...) - main()
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.
Creates group.
pplaf.entity.add_group'player'Creates groups.
pplaf.entity.add_groups('player', 'player_bullet')Returns iteratable group.
local players = pplaf.entity.get_group'player'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'
)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'
)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)Any typed declared available in pplaf.entity.types now will be available globally.
def_types_globally()
strange_triangle:create(0fx, 0fx)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')Maintains entities' AI and destruction.
pplaf.entity.main()