Skip to content

Instantly share code, notes, and snippets.

@jason102888
Last active June 3, 2018 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason102888/2e714aab51e5654a6482dfa837725c1c to your computer and use it in GitHub Desktop.
Save jason102888/2e714aab51e5654a6482dfa837725c1c to your computer and use it in GitHub Desktop.
function init(self)
msg.post(".", "acquire_input_focus")
msg.post("#gameproxy", "load")
end
function final(self)
msg.post(".", "release_input_focus")
end
function on_message(self, message_id, message, sender)
if message_id == hash("show_game") then
msg.post("#gameproxy", "load")
msg.post("#titleproxy", "unload")
elseif message_id == hash("show_title") then
msg.post("#titleproxy", "load")
msg.post("#gameproxy", "unload")
elseif message_id == hash("proxy_loaded") then
print("proxy_loaded", sender)
msg.post(sender,"init")
msg.post(sender, "enable")
elseif message_id == hash("proxy_unloaded") then
print("proxy_unloaded", sender)
end
end
local function random_position(position)
return vmath.vector3(math.random(1, sys.get_config("display.width")), math.random(1, sys.get_config("display.height")), 0)
end
local function spawn_enemy(position)
return factory.create("#enemy_factory", random_position())
end
local function spawn_medic(position)
return factory.create("#medic_factory", random_position())
end
local function spawn_helicopter(position)
return factory.create("#helicopter_factory", random_position())
end
local function spawn_tank(position)
return factory.create("#tank_factory", random_position())
end
local function spawn_powerup(position)
return factory.create("#powerup_factory", random_position())
end
--local tank_position = random_position()
--local enemy_position = random_position()
--local helicopter_position = random_position()
--local powerup_position = random_position()
--local medic_position = random_position()
function init(self)
math.randomseed(os.time())
self.spawned_tank =factory.create("#tank_factory",tank_position,nil,{parent=msg.url() })
self.spawned_enemy =factory.create("#enemy_factory",enemy_position,nil,{parent=msg.url() })
self.spawned_helicopter =factory.create("#helicopter_factory",helicopter_position,nil,{parent=msg.url() })
self.spawned_medic =factory.create("#medic_factory",medic_position,nil,{parent=msg.url() })
--self.spawned_powerup =factory.create("#powerup_factory",powerup_position,nil,{parent=msg.url() })
self.tank = {}
self.powerup = {}
self.medic={}
self.helicopter={}
self.enemy={}
--self.t = math.random(3) + 1
for i=1,10 do
local tank_id = factory.create("#tank_factory", go.get_position(), go.get_rotation())
local enemy_id = factory.create("#enemy_factory", go.get_position(), go.get_rotation())
local helicopter_id = factory.create("#helicopter_factory", go.get_position(),go.get_rotation())
--table.insert(self.tank, id)
--local p = go.get_position()
--p.y = vmath.lerp(math.random(), min_y, max_y)
--local component = "#tank_factory"
--factory.create(component, p)
table.insert(self.helicopter, helicopter_id)
table.insert(self.enemy, enemy_id)
table.insert(self.tank, tank_id)
end
msg.post(".", "accuire_input_focus")
end
function on_message(self, message_id, message)
if message_id == hash("gameover")then
msg.post("controller#controller", "show_title")
end
--if message_id == hash("tank_dead") then
--self.spawn_tank = nil
--elseif messag_id==("collision_response") then
--table.insert(self.tank, id(vmath.vector3(random_position(40,996),random_position(40,996),0)))
--if #self.tank==0 then
--msg.post("controller:/controller","show_menu")
--end
--end
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, dt)
--self.t=self.t-dt
--if self.t<=0 then
--local x = math.random()
--local y = math.random()
--factory.create("#tank",vmath.vector3(x,y,0))
--self.t=math.random(3)+1
--end
end
function on_input(self, action_id, action)
end
go.property("speed", 50)
go.property("parent", msg.url())
local PLAYER = hash("player")
local WALL=hash("wall")
local COLLISON_RESPONSE =hash("collision_response")
local CONTACT_POINT_RESPONSE=hash("contact_point_response")
function init(self)
--self.target_id = player
--self.target_position = random_position_on_wall()
self.moving = false
self.healing = false
self.dir = vmath.vector3(0, 1, 0)
self.speed = 50
end
local function healing(self)
self.healing=false
healing=1
msg.post("player#script", "healing")
msg.post("#sprite", "play_animation", { id = hash("healing") })
end
local function random_position_on_wall()
local wall = math.random(1, 4)
if wall == 1 then
return vmath.vector3(0, math.random(0, 640), 0)
elseif wall == 2 then
return vmath.vector3(1156, math.random(0, 640), 0)
elseif wall == 3 then
return vmath.vector3(math.random(0, 1156), 0, 0)
else
return vmath.vector3(math.random(0, 1156), 640, 0)
end
end
function final(self)
msg.post(self.parent,"medic_dead")
end
function init(self)
self.target_id = player
self.target_position = random_position_on_wall()
end
function update(self, dt)
local pos = go.get_world_position()
local target_angle = -math.atan2(self.target_position.x - pos.x, self.target_position.y - pos.y)
local target_quat = vmath.quat_rotation_z(target_angle)
local current_quat = go.get_rotation()
-- rotate towards target
local q = vmath.slerp(0.08, current_quat, target_quat)
go.set_rotation(q)
-- move towards target
local d = vmath.rotate(q, vmath.vector3(0, 1, 0))
go.set_position(pos + d * self.speed * dt)
-- if we have a target id we assume that it will die this frame and pick a random
-- target position and reset the target id flag.
-- if the target is still alive the next frame we will get a new collision and
-- we'll be able to set the target position again.
-- if self.target_id then
self.target_position = random_position_on_wall()
--end
--self.target_id = player
end
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
if message.group == PLAYER then
local my_pos = go.get_world_position()
local other_pos = go.get_world_position(message.other_id)
local distance_to_target = vmath.length(my_pos - self.target_position)
local distance_to_collision = vmath.length(my_pos - other_pos)
if distance_to_collision < distance_to_target or not self.target_id then
self.target_position = other_pos
self.target_id = message.other_id
end
end
elseif message_id == CONTACT_POINT_RESPONSE then
if message.group == WALL then
go.set_position(go.get_position() + message.normal * message.distance)
self.target_position = random_position_on_wall()
end
end
if message_id ==hash("collision_response") and message.group ==PLAYER then
self.healing=true
go.delete()
msg.post("player#player", "healing",{1})
msg.post("#sprite", "play_animation", { id = hash("healing") })
print("heal", sender)
end
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
function init(self)
msg.post("#music", "play_sound", {delay = 1, gain = .5})
end
go.property("health", 5)
local COLLISION_RESPONSE= hash("collision_response")
local DAMGA = hash("damage")
local TANK = hash("tanks")
local MEDIC = hash("medic")
local ENEMY= hash ("enemy")
local HELICOPTER=hash("helicopter")
local POWERUP=hash("powerup")
-- t is the table
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 = 50
self.health=5
end
function final(self)
msg.post("#", "release_input_focus")
end
function on_message(self, message_id, message, sender)
if message_id ==COLLISION_RESPONSE and message.group ==TANK then
--self.health=self.health-message.damage
msg.post("/gui#ui", "take_damage", {damage = 1})
--self.health=health-message.damage
print("hurt", sender)
elseif message_id ==COLLISION_RESPONSE and message.group ==ENEMY then
--self.health=self.health-message.damage
--go.delete()
print("hurt", sender)
elseif message_id ==COLLISION_RESPONSE and message.group ==HELICOPTER then
--self.health=self.health-message.damage
print("hurt", sender)
elseif message_id ==hash("healing")then
msg.post("/gui#ui", "healing", {healing = 1})
self.health = self.health + message.healing
print("healing", sender)
elseif message_id ==COLLISION_RESPONSE and message.group ==POWERUP then
print("powerup", sender)
end
end
if message_id ==hash("damage") then
self.health=self.health-message.damage
print("hurt",sender)
msg.post("/gui#ui", "take_damage", {damage = 1})
end
--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)
local rot = vmath.quat_rotation_z(angle)
local props = { dir = self.dir }
factory.create("#rocketfactory", nil, rot, props)
end
if message_id == hash("collision_response") then
go.delete()
end
if self.health <=0 then
go.delete()
msg.post("/gui#ui", "gameover")
end
self.input.x = 0
self.input.y = 0
self.moving = false
self.firing = false
end
function on_input(self, action_id, action)
if action_id == hash("up") then
self.input.y = 1
elseif action_id == hash("down") then
self.input.y = -1
elseif action_id == hash("left") then
self.input.x = -1
elseif action_id == hash("right") then
self.input.x = 1
elseif action_id == hash("fire") and action.pressed then
self.firing = true
end
if vmath.length(self.input) > 0 then
self.moving = true
self.dir = vmath.normalize(self.input)
end
end
go.property("dir", vmath.vector3())
function init(self)
self.speed = 200
self.life = 1
end
local function explode(self) -- [1]
self.life = 1000
go.set_rotation(vmath.quat())
self.speed = 0
msg.post("#sprite", "play_animation", { id = hash("explosion") })
end
function update(self, dt)
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
self.life = self.life - dt
if self.life < 0 then
explode(self) -- [2]
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
go.property("speed", 40)
go.property("parent", msg.url())
local PLAYER = hash("player")
local WALL=hash("wall")
local COLLISON_RESPONSE =hash("collision_response")
local CONTACT_POINT_RESPONSE=hash("contact_point_response")
function init(self)
--self.target_id = player
--self.target_position = random_position_on_wall()
self.moving = false
self.firing = false
self.dir = vmath.vector3(0, 1, 0)
self.speed = 40
end
local function random_position_on_wall()
local wall = math.random(1, 4)
if wall == 1 then
return vmath.vector3(0, math.random(0, 640), 0)
elseif wall == 2 then
return vmath.vector3(1156, math.random(0, 640), 0)
elseif wall == 3 then
return vmath.vector3(math.random(0, 1156), 0, 0)
else
return vmath.vector3(math.random(0, 1156), 640, 0)
end
end
function final(self)
--im dead
msg.post(self.parent,"tank_dead")
end
function init(self)
self.damage=1
self.target_id = player
self.target_position = random_position_on_wall()
self.t=math.random(3) + 1
end
function update(self, dt)
local pos = go.get_world_position()
local target_angle = -math.atan2(self.target_position.x - pos.x, self.target_position.y - pos.y)
local target_quat = vmath.quat_rotation_z(target_angle)
local current_quat = go.get_rotation()
-- rotate towards target
local q = vmath.slerp(0.08, current_quat, target_quat)
go.set_rotation(q)
-- move towards target
local d = vmath.rotate(q, vmath.vector3(0, 1, 0))
go.set_position(pos + d * self.speed * dt)
-- if we have a target id we assume that it will die this frame and pick a random
-- target position and reset the target id flag.
-- if the target is still alive the next frame we will get a new collision and
-- we'll be able to set the target position again.
-- if self.target_id then
self.target_position = random_position_on_wall()
--end
--self.target_id = player
end
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
if message.group == PLAYER then
local my_pos = go.get_world_position()
local other_pos = go.get_world_position(message.other_id)
local distance_to_target = vmath.length(my_pos - self.target_position)
local distance_to_collision = vmath.length(my_pos - other_pos)
if distance_to_collision < distance_to_target or not self.target_id then
self.target_position = other_pos
self.target_id = message.other_id
end
end
elseif message_id == CONTACT_POINT_RESPONSE then
if message.group == WALL then
go.set_position(go.get_position() + message.normal * message.distance)
self.target_position = random_position_on_wall()
end
end
if message_id==hash("collision_response") and message.group == PLAYER then
msg.post("player#player", "damage",{1})
print("damage",sender)
end
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
function init(self)
msg.post(".", "acquire_input_focus")
end
function final(self)
msg.post(".", "release_input_focus")
end
function on_input(self, action_id, action)
if action.pressed and gui.pick_node(gui.get_node("playbutton"), action.x, action.y) then
msg.post("controller:/controller", "show_game")
end
end
local text_nodes = {"score","health1","health2","health3","health4","health5","wave","gameover","endscore",}
local box_nodes = {"playagain"}
local health = 5
function init(self)
self.score = 0
self.endscore = 0
self.health = 5
end
--function show(self)
--reset_lives(self)
--node = gui.get_node("game_over")
--gui.set_endabled(node,false)
--for i, id in pairs(test_nodes) do
--node = gui.get_node(id)
--local c = gui.get_color(node)
--gui.set_color(node, vmath.vector4(c.x, c.y, c.z, 0))
--c.w = 1.0
--gui.animate(node, gui.PROP_COLOR, c, gui.EASING_INOUTSINE, 0.75, 0.25, function(), end)
--end
--end
--function hide(self, disable)
--for i, id in pairs(text_nodes) do
--node = gui.get_node(id)
--local c = gui.get_color(node)
--c.w = 0.0
--gui.animate(node, gui.PROP_COLOR, c, gui.EASING_INOUTSINE, 0.75, 0.25,
--function()
--if disable == true then
--msg.post(".", "disable")
--end
--end
--end
--end
--function show_game_over(self)
--hide(false)
--node = gui.get_node("game_over")
--gui.set_color(node, vmath.vector4(1, 1, 1, 1)), gui.EASING_INOUTSINE, 7.0, 0.0,
--function()
--gui.set_enabled(node,false)
--end
--gui.PLAYBACK_ONCE_PINGPONG
--end
function on_message(self, message_id, message, sender)
if message_id == hash("add_score") then
self.score = self.score + message.score
local scorenode = gui.get_node("score")
gui.set_text(scorenode, "SCORE: " .. self.score)
--elseif message_id == hash("take_damage") then
--self.health = self.health-message.damage
--node =gui.get_node("health".. healths)
--gui.set_color(node, vmath.vector4(1, 1, 1, 1))
--gui.animate(node, gui.PROP_COLOR, vmath.vector4(1,1,1,0), gui.EASING_OUTCIRC, 1.0)
--elseif message_id== hash("healing") then
--self.health = self.health+message.health
--node=gui.get_node("health"..healths)
--gui.set_color("health", 0,0,0,1)
--gui.animate(node, gui.PROP_COLOR, vmath.vector4(1,1,1,1), gui.EASING_OUTCIRC, 1.0)
--elseif message_id == hash("show") then
--hide(self, true)
--elseif message_id == hash("hide") then
--show_game_over(self)
elseif message_id == hash("gameover") then
show_game_over(self)
msg.post("controller:/controller","show_menu")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment