Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
selimanac committed Oct 8, 2024
1 parent e217218 commit 4f8d6b1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 59 deletions.
2 changes: 1 addition & 1 deletion example/assets/box.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
embedded_components {
id: "sprite"
type: "sprite"
data: "default_animation: \"40x40_rect\"\n"
data: "default_animation: \"40x40\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"textures {\n"
" sampler: \"texture_sampler\"\n"
Expand Down
File renamed without changes
File renamed without changes
4 changes: 2 additions & 2 deletions example/assets/main.atlas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
images {
image: "/example/assets/images/40x40_rect.png"
image: "/example/assets/images/40x40.png"
}
images {
image: "/example/assets/images/60x30_rect.png"
image: "/example/assets/images/60x30.png"
}
extrude_borders: 2
127 changes: 71 additions & 56 deletions example/test.script
Original file line number Diff line number Diff line change
@@ -1,114 +1,128 @@
local boxes = {}
local box_group = -1

local ray_start = vmath.vector3(0, 0, 0)
local ray_end = vmath.vector3(365, 370, 0)
local boxes = {}
local box_group = -1
local ray_start = vmath.vector3(0, 0, 0)
local ray_end = vmath.vector3(365, 370, 0)
local query_result = {}
local result_count = 0
local collision_bits = {
PLAYER = 1, -- (2^0)
ENEMY = 2, -- (2^1)
ITEM = 4, -- (2^2)
ALL = bit.bnot(0) -- -1 for all results
}
local mask_bits = bit.bor(collision_bits.ENEMY, collision_bits.ITEM)

local function add_box(box_position, box_type, isStatic, category)
local box_id = factory.create("/factory#box", box_position)
local sprite_url = msg.url(nil, box_id, "sprite")

if box_type == 1 then
sprite.play_flipbook(sprite_url, "60x30")
end

local query_result = {}
local result_count = 0

local function add_box(box_pos)
local pos = vmath.vector3(box_pos.x, box_pos.y, 0)
local box = factory.create("/factory#box", pos)
local sprite = msg.url(nil, box, "sprite")
local boxlabel = msg.url(nil, box, "label")
local size = go.get(sprite, "size")
local box_label = msg.url(nil, box_id, "label")
local sprite_size = go.get(sprite_url, "size")

-- Insert AABB into the group.
local box_proxy_id = daabbcc.insert_gameobject(box_group, box, size.x, size.y)
local box_aabb_id = -1

if isStatic then
box_aabb_id = daabbcc.insert_aabb(box_group, box_position.x, box_position.y, sprite_size.x, sprite_size.y, category)
else
box_aabb_id = daabbcc.insert_gameobject(box_group, box_id, sprite_size.x, sprite_size.y, category)
end

label.set_text(boxlabel, box_proxy_id)
local t = { pos = pos, box = box, sprite = sprite, size = size, proxy_id = box_proxy_id }

label.set_text(box_label, box_aabb_id .. "\n" .. category)
local t = { position = box_position, id = box_id, sprite_url = sprite_url, size = sprite_size, aabb_id = box_aabb_id }
table.insert(boxes, t)
end

function init(self)
msg.post(".", "acquire_input_focus")



-- New group for AABBs.
box_group = daabbcc.new_group()

-- Insert AABB into the group.
add_box(vmath.vector3(260, 280, 0))
add_box(vmath.vector3(250, 280, 0))
add_box(vmath.vector3(525, 490, 0))
add_box(vmath.vector3(650, 250, 0))
add_box(vmath.vector3(500, 550, 0))
add_box(vmath.vector3(600, 250, 0))
add_box(vmath.vector3(550, 250, 0))
add_box(vmath.vector3(600, 300, 0))
add_box(vmath.vector3(600, 200, 0))
add_box(vmath.vector3(700, 570, 0))
add_box(vmath.vector3(260, 280, 0), 1, true, collision_bits.PLAYER)
add_box(vmath.vector3(250, 280, 0), 0, true, collision_bits.ALL)
add_box(vmath.vector3(525, 490, 0), 0, true, collision_bits.ALL)
add_box(vmath.vector3(650, 250, 0), 1, true, collision_bits.ALL)
add_box(vmath.vector3(500, 550, 0), 1, false, collision_bits.ENEMY)
add_box(vmath.vector3(600, 250, 0), 0, true, collision_bits.ALL)
add_box(vmath.vector3(550, 250, 0), 0, true, collision_bits.ITEM)
add_box(vmath.vector3(600, 300, 0), 0, true, collision_bits.ALL)
add_box(vmath.vector3(600, 200, 0), 0, true, collision_bits.ALL)
add_box(vmath.vector3(700, 570, 0), 0, true, collision_bits.ALL)

-- Move and update position
boxes[2].pos.x = boxes[2].pos.x + 50
boxes[2].pos.y = boxes[2].pos.y + 50
go.set_position(boxes[2].pos, boxes[2].box)
boxes[2].position.x = boxes[2].position.x + 50
boxes[2].position.y = boxes[2].position.y + 50
go.set_position(boxes[2].position, boxes[2].id)

-- Updates the AABB position and size when you change it is position or size.
daabbcc.update_aabb(box_group, boxes[2].proxy_id, boxes[2].pos.x, boxes[2].pos.y, boxes[2].size.x, boxes[2].size.y)
daabbcc.update_aabb(box_group, boxes[2].aabb_id, boxes[2].position.x, boxes[2].position.y, boxes[2].size.x, boxes[2].size.y)

-- Query the possible overlaps using AABB. Returns itself.
query_result, result_count = daabbcc.query_aabb(box_group, boxes[2].pos.x, boxes[2].pos.y, boxes[2].size.x,
local result, count = daabbcc.query_aabb(box_group, boxes[2].position.x, boxes[2].position.y, boxes[2].size.x,
boxes[2].size.y)

print(result_count)
if query_result then
pprint(query_result)
print("count: ", count)
if result then
pprint(result)
end

go.animate(boxes[5].box, "position.x", go.PLAYBACK_LOOP_PINGPONG, boxes[7].pos.x + 150, go.EASING_INOUTBACK, 2)
go.animate(boxes[5].id, "position.x", go.PLAYBACK_LOOP_PINGPONG, boxes[7].position.x + 150, go.EASING_INOUTBACK, 2)
end

function update(self, dt)
-- pprint(dt)
msg.post("@render:", "draw_line", { start_point = ray_start, end_point = ray_end, color = vmath.vector4(1, 1, 1, 1) })

-- raycast_sort
query_result, result_count = daabbcc.raycast_sort(box_group, ray_start.x, ray_start.y, ray_end.x, ray_end.y)
-- raycast
query_result, result_count = daabbcc.raycast(box_group, ray_start.x, ray_start.y, ray_end.x, ray_end.y, mask_bits)
if query_result then
print("raycast_sort")
print("raycast")
print(result_count)
pprint(query_result)
end

-- raycast
query_result, result_count = daabbcc.raycast(box_group, ray_start.x, ray_start.y, ray_end.x, ray_end.y)
-- raycast_sort
query_result, result_count = daabbcc.raycast_sort(box_group, ray_start.x, ray_start.y, ray_end.x, ray_end.y)
if query_result then
print("raycast")
print("raycast_sort")
print(result_count)
pprint(query_result)
end

-- query_id_sort
query_result, result_count = daabbcc.query_id_sort(box_group, boxes[5].proxy_id)
-- query_id_sort
query_result, result_count = daabbcc.query_id_sort(box_group, boxes[5].aabb_id)
if query_result then
print("query_id_sort")
print(result_count)
pprint(query_result)
end

-- query_sort
query_result, result_count = daabbcc.query_aabb_sort(box_group, boxes[3].pos.x, boxes[3].pos.y, boxes[3].size.x,
--query_aabb_sort
query_result, result_count = daabbcc.query_aabb_sort(box_group, boxes[3].position.x, boxes[3].position.y, boxes[3].size.x,
boxes[3].size.y)
if query_result then
print("query_sort")
print(result_count)
pprint(query_result)
end

-- query_id
query_result, result_count = daabbcc.query_id(box_group, boxes[5].proxy_id)
--query_id
query_result, result_count = daabbcc.query_id(box_group, boxes[5].aabb_id)
if query_result then
print("query_id")
print(result_count)
pprint(query_result)
end

-- query
query_result, result_count = daabbcc.query_aabb(box_group, boxes[3].pos.x, boxes[3].pos.y, boxes[3].size.x, boxes[3].size.y)
-- query_aabb
query_result, result_count = daabbcc.query_aabb(box_group, boxes[3].position.x, boxes[3].position.y, boxes[3].size.x, boxes[3].size.y)
if query_result then
print("query")
print(result_count)
Expand All @@ -118,8 +132,9 @@ end

function on_input(self, action_id, action)
-- Move and update position
boxes[3].pos.x = action.x
boxes[3].pos.y = action.y

go.set_position(vmath.vector3(boxes[3].pos.x, boxes[3].pos.y, 0), boxes[3].box)
boxes[3].position.x = action.x
boxes[3].position.y = action.y
ray_end.x = action.x
ray_end.y = action.y
go.set_position(vmath.vector3(boxes[3].position.x, boxes[3].position.y, 0), boxes[3].id)
end

0 comments on commit 4f8d6b1

Please sign in to comment.