This repository was archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
examples
glebi574 edited this page Nov 30, 2023
·
3 revisions
PPLAF has plenty of stuff, this article will give you some examples on how you might use certain tools and functions.
Examples on how to define groups, load types and create entities:
pplaf.entity.add_group'player'
-- adding group 'player', now any entity can be added in this group
pplaf.entity.load_by_typed_files('/dynamic/assets/entities/', 'rotating_triangle')
-- defining type 'rotating_triangle', now entities of this type can be created
pplaf.entity.create(0fx, 0fx, 'rotating_triangle')
-- creating entity of type 'rotating_triangle' at (0fx;0fx)
pplaf.entity.def_types_globally()
-- now any entity types loaded before will be accessible globally
rotating_triangle:create(0fx, 0fx)
-- so, now we can create entities like thatExamples of entity type functionality:
-- in entity type file
local hp = 10
-- this variable is static.
-- It's accessible only by functions defined in this file.
return {
-- anything defined there is accessible by entity.type.*
hp = 10,
-- this variable is also static. It's accessible
-- by entity.type.hp or pplaf.entity.types.type_name.hp.
group = 'enemy',
-- group defines in which group entity will be stored
animation = 'rotating_triangle',
-- animation is name of animation type, that will
-- define how entity should be animated
proto = {
hp = 10,
},
-- proto is entity's prototype. Anything defined here
-- will be accessible by and specific per entity.
constructor = function(entity, x, y, ...)
end,
-- constructor is called upon entity creation.
-- You can define variables or functions in it,
-- maintain certain states, or whatever you want.
destructor = function(entity, ...)
end,
-- destructor is called upon entity:destroyA(...) call.
-- You can maintain entity destruction in it.
-- If you want entity to explode first, you can define
-- additional states to track entity's explosion and
-- destroy it afterwards.
ai = function()
end,
-- ai is called while entity is alive and presented in
-- PPLAF storage. You can maintain there movement of entity,
-- its weapons, states or literally anything.
}Example of how proto works:
-- in entity type:
proto = {
hp = 10,
lifetime = 30,
maintain_weapon = function()
end,
}
-- proto is prototype of entity, any variables and functions
-- defined there will be accessible by entity.
-- in ai() or other space, that has access to entity of that type
print(entity.hp) -- 10
print(entity.lifetime ) -- 30
print(entity.maintain_weapon) -- function: 0x...Example of how to use constructor:
-- in entity type:
constructor = function(entity, x, y, ...)
local args = {...}
-- any additional variables that were passed
entity.rotation_speed = args[1]
-- let's say, we want to set rotation speed per entity
entity.id = pewpew.new_customizable_entity(x, y)
-- creating entity and defining entity.id
entity:start_spawning(15)
entity:set_mesh('/dynamic/your_path/mesh.lua', 0)
entity:set_position_interpolation(true)
-- as you can see, prototype function don't require
-- you to pass id, as original pewpew functions do
endExample of how to use destructor:
-- in entity type:
destructor = function(entity, ...)
local args = {...}
-- you can pass additional arguments to destroyA(...)
angle = args[1]
entity:set_mesh_angle(angle, 0fx, 0fx, 1fx)
-- let's say we want to set entity's angle to certain
-- value on its destruction
entity:start_exploding(30)
-- Remember, entity is exploding, it's still alive,
-- so you should maintain such cases. You can explode
-- entity and call destroyA() in last tick, you can
-- explode entity and call destroyA() immediately if
-- you won't need to modify it later.
end
-- anywhere else
entity:destroyA(PI_FX)
-- destructor will be called and entity will be removed
-- from PPLAF storageExample of how to use ai:
-- in entity type
ai = function(entity)
-- ai is called every tick if entity is alive.
-- So, if entity is exploding, ai will still be called
-- and you should maintain such situations or call
-- destroyA().
entity.lifetime = entity.lifetime - 1
if entity.lifetime == 0 then
entity:destroyA()
end
endExamples of different useful functions you will use often:
-- if entity is colliding with any entity from provided group,
-- that entity will be returned. Otherwise returns nil.
function check_collision_with_group(entity1, group_name)
local x1, y1 = entity1:get_position()
-- get position of entity, that should collide with group
for _, entity2 in ipairs(pplaf.entity.get_group(group_name)) do
-- get_groupL(group_name) returns list of entities from group
-- with corresponding name.
if entity2:get_is_alive() and entity1 ~= entity2 then
-- don't check entities that aren't alive or same entity
local x2, y2 = entity2:get_position()
-- get position of entity from group
if pplaf.fmath.length(x1, y1, x2, y2) < entity1.radius + entity2.radius then
-- entities are required to have defined collision radiuses.
-- If they're colliding, entity from that group will be returned.
return entity2
end
end
end
end