Skip to content

Commit

Permalink
New APIs to enable massive amounts of entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
jyaif committed Feb 15, 2021
1 parent fac9ee1 commit 5012990
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 55 deletions.
4 changes: 2 additions & 2 deletions content/levels/sample_enemy/level.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function new_enemy_bullet(x, y, angle)
pewpew.customizable_entity_start_exploding(id, 10)
end)
-- Make the bullet collide with player ships: add 3 damage to the ship, and destroy the bullet.
pewpew.customizable_entity_set_player_collision_callback(id, function(player_id, ship_id)
pewpew.customizable_entity_set_player_collision_callback(id, function(entity_id, player_id, ship_id)
pewpew.add_damage_to_player_ship(ship_id, 3)
pewpew.customizable_entity_start_exploding(id, 10)
pewpew.customizable_entity_start_exploding(entity_id, 10)
end)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
meshes = {
{
vertexes = {{0,0}, {1000,0}, {1000,1000}, {0,1000}},
colors = {0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff},
segments = {{0,1,2,3,0}}
}
}
7 changes: 7 additions & 0 deletions content/levels/sample_optimized_entities/baf_graphic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
meshes = {
{
vertexes = {{-20,-20}, {20,-20}, {20,20}, {-20,20}},
colors = {0xffff00ff, 0xffff00ff, 0xffff00ff, 0xffff00ff},
segments = {{0,1,2,3,0}}
}
}
70 changes: 70 additions & 0 deletions content/levels/sample_optimized_entities/level.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- Set how large the level will be.
pewpew.set_level_size(1000fx, 1000fx)

local bafs = {}

function baf_update(entity_id)
local data = bafs[entity_id]
local x, y = pewpew.entity_get_position(entity_id)
x = x + data[1]
pewpew.customizable_entity_set_mesh_angle(entity_id, data[3], 1fx, 0fx, 0fx)
data[3] = data[3] + 0.1000fx

pewpew.entity_set_position(entity_id, x, y)
if pewpew.entity_get_is_started_to_be_destroyed(entity_id) == true then
bafs[entity_id] = nil
pewpew.customizable_entity_set_player_collision_callback(entity_id, nil)
pewpew.customizable_entity_set_weapon_collision_callback(entity_id, nil)
pewpew.entity_set_update_callback(entity_id, nil)
pewpew.customizable_entity_configure_wall_collision(entity_id, true, nil)
end
end

function baf_collide_with_ship(entity_id, player_index, ship_entity_id)
pewpew.customizable_entity_start_exploding(entity_id, 20)
end

function baf_collide_with_wall(entity_id)
local data = bafs[entity_id]
data[1] = -data[1]
end

function baf_weapon_collision(entity_id, weapon_description)
local data = bafs[entity_id]
if data[2] == 0 then
pewpew.customizable_entity_start_exploding(entity_id, 10)
else
data[2] = data[2] - 1
end
return true
end

for i=0,1200 do
local id = pewpew.new_customizable_entity(fmath.random_fixedpoint(100fx, 900fx), fmath.random_fixedpoint(100fx, 900fx))
-- Format: {dx, life, angle}
bafs[id] = {5fx, 3, fmath.random_fixedpoint(0fx, fmath.tau())}

pewpew.customizable_entity_set_mesh(id, "/dynamic/baf_graphic.lua", 0)
pewpew.customizable_entity_set_position_interpolation(id, true)
pewpew.entity_set_radius(id, 20fx)

-- Makes sure the entity won't be drawn when not visible.
pewpew.customizable_entity_set_visibility_radius(id, 20fx)

pewpew.customizable_entity_set_weapon_collision_callback(id, baf_weapon_collision)
pewpew.entity_set_update_callback(id, baf_update)
pewpew.customizable_entity_set_player_collision_callback(id, baf_collide_with_ship)
pewpew.customizable_entity_configure_wall_collision(id, true, baf_collide_with_wall)
end

-- Create an entity at position (0,0) that will hold the background mesh.
local background = pewpew.new_customizable_entity(0fx, 0fx)
pewpew.customizable_entity_set_mesh(background, "/dynamic/background_graphic.lua", 0)

-- Create and configure the player's ship.
local player_x = 250fx
local player_y = 100fx
local player_index = 0 -- there is only one player
local ship_id = pewpew.new_player_ship(player_x, player_y, player_index)
local weapon_config = {frequency = pewpew.CannonFrequency.FREQ_10, cannon = pewpew.CannonType.DOUBLE}
pewpew.configure_player_ship_weapon(ship_id, weapon_config)
5 changes: 5 additions & 0 deletions content/levels/sample_optimized_entities/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name":"Sample: Optimized entities",
"descriptions":["An optimized way of creating entities."],
"entry_point":"level.lua"
}
34 changes: 0 additions & 34 deletions content/levels/sample_simple_level/level.lua

This file was deleted.

5 changes: 0 additions & 5 deletions content/levels/sample_simple_level/manifest.json

This file was deleted.

7 changes: 0 additions & 7 deletions content/levels/sample_simple_level/square500x500_graphic.lua

This file was deleted.

6 changes: 3 additions & 3 deletions content/levels/sample_tutorial/box_template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ function box.new(x, y, outer_mesh_info, inner_mesh_info, callback)
self.inner_mesh_angle = 0fx


local function collision_callback(player_id, ship_id)
local function collision_callback(entity_id, player_id, ship_id)
-- Remove the update callback to stop the rotation of the inner_mesh.
pewpew.entity_set_update_callback(self.handle, nil)
pewpew.entity_set_update_callback(entity_id, nil)
-- Start the explosion
pewpew.customizable_entity_start_exploding(self.inner_mesh_handle, 40)
pewpew.customizable_entity_start_exploding(self.handle, 30)
pewpew.customizable_entity_start_exploding(entity_id, 30)
-- Notify about the collision
if callback ~= nil then
callback(player_id, ship_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function box.new(x, y, outer_mesh_info, inner_mesh_info, collision_callback, exp
pewpew.customizable_entity_set_mesh_z(inner_mesh_id, 10fx)
local inner_mesh_angle = 0fx

local function collision_callback_wrapper(player_id, ship_id)
local function collision_callback_wrapper(entity_id, player_id, ship_id)
-- Remove the update callback to stop the rotation of the inner_mesh.
pewpew.entity_set_update_callback(id, nil)
-- Start the explosion
Expand Down
2 changes: 1 addition & 1 deletion content/pewpewlive-wasm.js

Large diffs are not rendered by default.

Binary file modified content/pewpewlive-wasm.wasm
Binary file not shown.
20 changes: 18 additions & 2 deletions docs/raw_documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ var documentation = [
"return_types": [
],
"func_name":"entity_set_update_callback",
"comment":"Sets a callback that will be called at every tick as long as the entity identified by `id` is alive. Remove the callback by passing a nil `callback`.",
"comment":"Sets a callback that will be called at every tick as long as the entity identified by `id` is alive. Remove the callback by passing a nil `callback`. The callbacks gets the entity ID passed as a parameter.",
"parameters": [
{
"name":"entity_id",
Expand Down Expand Up @@ -974,8 +974,24 @@ var documentation = [
{
"return_types": [
],
"func_name":"customizable_entity_set_visibility_radius",
"comment":"Sets the radius defining the visibility of the entity. This allows the game to know when an entity is actually visible, which in turns allows to massively optimize the rendering. Use the smallest value possible. If not set, the rendering radius is an unspecified large number that effectively makes the entity always be rendered, even if not visible.",
"parameters": [
{
"name":"entity_id",
"type":"EntityId",
},
{
"name":"radius",
"type":"FixedPoint",
},
],
},
{
"return_types": [
],
"func_name":"customizable_entity_configure_wall_collision",
"comment":"`collide_with_walls` configures whether the entity should stop when colliding with walls. If `collision_callback` is not nil, it is called anytime a collision is detected.",
"comment":"`collide_with_walls` configures whether the entity should stop when colliding with walls. If `collision_callback` is not nil, it is called anytime a collision is detected. The callback receives the entity id as the first parameter.",
"parameters": [
{
"name":"entity_id",
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func list(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "name = \"%s\",\n", level.name)
fmt.Fprintf(w, "author = \"%s\",\n", level.author)
fmt.Fprintf(w, "level_id = \"/dev/%s\",\n", level.directory)
fmt.Fprintf(w, "publish_state = 0,\n")
fmt.Fprintf(w, "experimental = true\n")
fmt.Fprint(w, "},\n")
}
Expand Down

0 comments on commit 5012990

Please sign in to comment.