Advertisement
Guest User

controller.script

a guest
Jan 18th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. go.property("speed", 6)
  2.  
  3. local grid = 460
  4. local platform_heights = { 100, 200, 350 }
  5. local coins = 3
  6.  
  7. function init(self)
  8.     self.gridw = 0
  9.     self.spawns = {}
  10. end
  11.  
  12. function update(self, dt)
  13.     self.gridw = self.gridw + self.speed
  14.  
  15.     if self.gridw >= grid then
  16.         self.gridw = 0
  17.  
  18.         -- Maybe spawn a platform at random height
  19.         if math.random() > 0.2 then
  20.             local h = platform_heights[math.random(#platform_heights)]
  21.             local f = "#platform_factory"
  22.             local coins = coins
  23.             if math.random() > 0.5 then
  24.                f = "#platform_long_factory"
  25.                coins = coins * 2 -- Twice the number of coins on long platforms
  26.             end
  27.  
  28.             local p = factory.create(f, vmath.vector3(1600, h, 0), nil, {}, 0.6)
  29.             msg.post(p, "set_speed", { speed = self.speed })
  30.             msg.post(p, "create_coins", { coins = coins })
  31.             table.insert(self.spawns, p)
  32.         end
  33.     end
  34. end
  35.  
  36. function on_message(self, message_id, message, sender)
  37.     if message_id == hash("reset") then
  38.         -- Tell the hero to reset.
  39.         msg.post("hero#script", "reset")
  40.         -- Delete all platforms
  41.         for i,p in ipairs(self.spawns) do
  42.             go.delete(p)
  43.         end
  44.         self.spawns = {}
  45.     elseif message_id == hash("delete_spawn") then
  46.         for i,p in ipairs(self.spawns) do
  47.             if p == message.id then
  48.                 table.remove(self.spawns, i)
  49.                 go.delete(p)
  50.             end
  51.         end
  52.     end
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement