Skip to content

Instantly share code, notes, and snippets.

@szilardhuber
Last active May 24, 2020 14:05
Show Gist options
  • Save szilardhuber/fddae384a6b57cac419b25a05b2006af to your computer and use it in GitHub Desktop.
Save szilardhuber/fddae384a6b57cac419b25a05b2006af to your computer and use it in GitHub Desktop.
Defold tanks spawn
go.property("spawn_interval", 2)
go.property("max_speed", 100)
function init(self)
msg.post(".", "acquire_input_focus")
self.moving = false
self.firing = false
self.input = vmath.vector3()
self.dir = vmath.vector3(0, 1, 0)
self.speed = self.max_speed
self.timer = self.spawn_interval
self.score = 0
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
if self.firing then
local angle = math.atan2(self.dir.y, self.dir.x) -- [1]
local pos = go.get_position()
local rot = vmath.quat_rotation_z(angle) -- [2]
local props = { dir = self.dir } -- [3]
factory.create("#rocketFactory", pos, rot, props) -- [4]
end
self.input.x = 0
self.input.y = 0
self.moving = false
self.speed = self.max_speed
self.firing = false
self.timer = self.timer - dt
if self.timer < 0 then
local angle = math.atan2(self.dir.y, self.dir.x) -- [1]
local pos = go.get_position()
pos.x = math.random(550) + 50
pos.y = math.random(400) + 50
local rot = vmath.quat_rotation_z(angle) -- [2]
factory.create("#tankFactory", pos, rot) -- [4]
self.timer = self.spawn_interval
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
local currentpos = go.get_position()
-- 2.1 is a strange magic number here... TODO
local newpos = currentpos + (message.normal * message.distance * 2.1)
go.set_position(newpos)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment