My rocket script in player_rocket.script:
go.property("direction", vmath.vector3())
function init(self)
self.speed = 300
self.life = 1
end
function update(self, dt)
self.life = self.life - dt
if self.life < 0 then
explode(self)
else
move(self, dt)
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("animation_done") then
go.delete()
elseif message_id == hash("collision_response") then
explode(self)
go.delete(message.other_id)
msg.post("/gui#ui", "add_score", {score = 100})
end
end
function explode(self)
-- Give ourselves some time to complete the animation
self.life = 10000
-- Reset rotation otherwise the explosion will be rotated
go.set_rotation(vmath.quat())
-- Stop moving
self.speed = 0
-- Play explosion
msg.post("#sprite", "play_animation", {id = hash("explosion")})
end
function move(self, dt)
local pos = go.get_position()
pos = pos + self.direction * self.speed * dt
go.set_position(pos)
end
My tank script in tank.script:
local defs = require "modules/defines"
go.property("parent", msg.url())
go.property("dead_zone", 5)
function init(self)
self.target = nil
self.speed = 50
end
function final(self)
msg.post(self.parent, "tank_dead", { id = go.get_id() })
end
function update(self, dt)
if self.target == nil then
new_target(self)
else
-- move towards the target at some speed
move(self, dt)
-- If we're close enough, we've arrived, drop the target
check_arrived(self)
end
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action)
-- Add input-handling code here
-- Remove this function if not needed
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
function new_target(self)
print(go.get_id() .. " has no target, finding new target")
-- If we don't have a target, pick a random location and move towards it
-- TODO: Find a way to get a random location within the level bounds
x, y, w, h = tilemap.get_bounds("/map#map")
local xt = math.random(w * defs.tile_size)
local yt = math.random(h * defs.tile_size)
self.target = vmath.vector3(xt, yt, 1)
end
function move(self, dt)
if self.target == nil then
print("WTF " .. go.get_id())
else
print("we have target")
local p = go.get_position()
local dir = vmath.normalize(self.target - p)
p = p + (dir * self.speed * dt)
go.set_position(p)
end
end
function check_arrived(self)
local sqr_dist = vmath.length_sqr(self.target - go.get_position())
local sqr_zone = self.dead_zone * self.dead_zone
if sqr_zone > sqr_dist then
print(go.get_id() .. " arrived at target")
self.target = nil
end
end