Commits

benbeltran  committed 4b8f678

grass, factory stuff + map objects

  • Participants
  • Parent commits a56031f

Comments (0)

Files changed (8)

File actor_factory.lua

 
 function ActorFactory.create_actor(actor_type, x, y, properties)
   local actor_check = loadstring("return "..actor_type)()
-  if actor_check then
+  if actor_check and ActorFactory.should_load(actor_type, x, y) then
     local generated_actor = actor_check:new(x, y)
     for k, v in pairs(properties) do
       if k ~= "spawn" then
   end
 end
 
+function ActorFactory.should_load(actor_type, x, y)
+  if actor_type == "Player" then
+    if not ActorFactory.player_loaded then
+      ActorFactory.player_loaded = true
+      game.player_respawn = {map = "mapnameplz", x = x, y = y}
+      return true
+    else
+      return false
+    end
+  end
+
+  return true
+end
+
 return ActorFactory

File actors/actor.lua

   self.y_vel = 0.0            -- Current Air Velocity
   self.fall_time = 0.0        -- Time falling
 
+  self.is_actor = true        -- simplify some stuff.
+
   game.actor_manager:move_in(self)
 
   self:bind_events()
 
 function Actor:bind_events()
   self:bind("collision", function(self, ev)
-    if not ev.other.passable and not self.immutable then
+    if not ev.other.passable and not self.immutable and ev.other.is_actor then
       if ev.dx ~= 0 then
         if (ev.dx > 0 and self:can_pass("right")) or (ev.dx < 0 and self:can_pass("left")) then
           self.x = self.x + ev.dx - sign(ev.dx)
 function Actor:update(dt)
   --Most of the actor code goes here.
   self:physics(dt)
+
+  if self.dying then
+    self:destroy()
+  end
 end
 
 function Actor:physics(dt)
   return self.box.top+self.box.bottom
 end
 
+function Actor:die()
+  self.dying = true
+end
+
+function Actor:destroy()
+  game.actor_manager:move_out(self)
+end
+
 return Actor

File actors/bovine_head.lua

       if love.keyboard.isDown("down") and not self.eat_lock then
         self.eat_lock = true
         self:dispatch('ate_grass', {grass = ev.other})
+        ev.other:dispatch('eaten')
       end
     end
   end)

File actors/grass.lua

 
 end
 
+function GrassActor:bind_events()
+  self:bind('eaten', function (self, ev)
+    self:die()
+  end)
+end
+
 return GrassActor

File actors/player.lua

   self.head:bind('ate_grass', function(self, ev)
     player:eat_grass(ev.grass.color)
   end)
+
+  self:bind("collision", function(self, ev)
+    if ev.other.type == "CHECKPOINT" then
+      print("Set checkpoint.")
+    end
+  end)
 end
 
 function Player:eat_grass(color)
   end
 end
 
+function Player:die()
+  game.lives = game.lives - 1
+  if game.lives < 0 then
+    -- go to game over
+  else
+    Player:respawn()
+  end
+end
+
 return Player

File assets/maps/cowtest.tmx

     <property name="spawn" value="MovingPlatformActor"/>
    </properties>
   </object>
+  <object name="Checkpoint" type="CHECKPOINT" x="576" y="192" width="32" height="16">
+   <properties>
+    <property name="collides" value="1"/>
+   </properties>
+  </object>
  </objectgroup>
 </map>
   self.collider.initialize(16)
 
   function self.collider.collision(actor1, actor2, dx, dy)
-    actor1:dispatch('collision', { other = actor2, dx = dx, dy = dy })
-    actor2:dispatch('collision', { other = actor1, dx = -dx, dy = -dy })
+    if actor1.dispatch then
+      actor1:dispatch('collision', { other = actor2, dx = dx, dy = dy })
+    end
+    if actor2.dispatch then
+      actor2:dispatch('collision', { other = actor1, dx = -dx, dy = -dy })
+    end
   end
 
   function self.collider.endCollision(actor1, actor2)
-    actor1:dispatch('endCollision', { other = actor2})
-    actor2:dispatch('endCollision', { other = actor1})
+    if actor1.dispatch then
+      actor1:dispatch('endCollision', { other = actor2})
+    end
+    if actor2.dispatch then
+      actor2:dispatch('endCollision', { other = actor1})
+    end
   end
 
   function self.collider.getBBox(actor)
-    return actor.x - actor:box_side("left"), actor.y - actor:box_side("top"),
-           actor:box_width(), actor:box_height()
+    if actor.box_side then
+      return actor.x - actor:box_side("left"), actor.y - actor:box_side("top"),
+             actor:box_width(), actor:box_height()
+    else
+      return actor.x, actor.y, actor.width, actor.height
+    end
   end
 
   function self.collider.shouldCollide(actor1, actor2)

File map_manager.lua

 
 function MapManager:initialize_objects()
   for i, v in ipairs(self.map("Objects").objects) do
+    if v.properties.collides == 1 then
+      game.collider.add(v)
+    end
     if v.type == "SPAWN" then
-      -- TODO: Do this in a smarter manner. Parse and check existence.
-      -- Actually, abstract this to a factory. ActorFactory.spawn(v)
-      -- I don't know how to handle stuff like v.properties. I think
-      -- I'll have actors receive a properties table and just roll with that
-      -- send (v.x, v.y, v.properties)
       ActorFactory.create_actor(v.properties.spawn, v.x, v.y, v.properties)
     end
   end