Commits

benbeltran committed a56031f

Actor Factory and some actor changes

Comments (0)

Files changed (6)

actor_factory.lua

+local ActorFactory = {}
+
+function ActorFactory.create_actor(actor_type, x, y, properties)
+  local actor_check = loadstring("return "..actor_type)()
+  if actor_check then
+    local generated_actor = actor_check:new(x, y)
+    for k, v in pairs(properties) do
+      if k ~= "spawn" then
+        generated_actor:set_attribute(k,v)
+      end
+    end
+  end
+end
+
+return ActorFactory
   game.actor_manager:move_out(self)
 end
 
+function Actor:set_attribute(key, value)
+  self[key] = value
+end
+
 function Actor:draw()
   love.graphics.draw(self.image, self.x - self.image:getWidth() / 2, self.y - self.image:getHeight() / 2)
   self:draw_bounding_box()
   self.passable = true
 end
 
+function GrassActor:set_attribute(key, value)
+  Actor.set_attribute(self, key, value)
+
+  local image = ""
+
+  if key == "color" then
+    if self.color == "red" then
+      image = "assets/images/sprites/static/grass_red_16x16.png"
+    elseif self.color == "blue" then
+      image = "assets/images/sprites/static/grass_blu_16x16.png"
+    else
+      image = "assets/images/sprites/static/grass_grn_16x16.png"
+    end
+    self.image = love.graphics.newImage(image)
+  end
+
+end
+
 return GrassActor

assets/maps/cowtest.tmx

  <objectgroup name="Objects" width="100" height="50">
   <object name="Cat" type="SPAWN" x="304" y="368" width="16" height="16">
    <properties>
-    <property name="spawn" value="cat"/>
+    <property name="spawn" value="CatActor"/>
    </properties>
   </object>
   <object name="Player" type="SPAWN" x="64" y="256" width="16" height="16">
    <properties>
-    <property name="spawn" value="player"/>
+    <property name="spawn" value="Player"/>
    </properties>
   </object>
   <object name="Blue Grass" type="SPAWN" x="1008" y="336" width="16" height="16">
    <properties>
     <property name="color" value="blue"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Green Grass" type="SPAWN" x="768" y="144" width="16" height="16">
    <properties>
     <property name="color" value="green"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Red Grass" type="SPAWN" x="0" y="304" width="16" height="16">
    <properties>
     <property name="color" value="red"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Red Grass" type="SPAWN" x="16" y="304" width="16" height="16">
    <properties>
     <property name="color" value="red"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Red Grass" type="SPAWN" x="32" y="304" width="16" height="16">
    <properties>
     <property name="color" value="red"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Green Grass" type="SPAWN" x="784" y="144" width="16" height="16">
    <properties>
     <property name="color" value="green"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Green Grass" type="SPAWN" x="800" y="144" width="16" height="16">
    <properties>
     <property name="color" value="green"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Blue Grass" type="SPAWN" x="1024" y="336" width="16" height="16">
    <properties>
     <property name="color" value="blue"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Blue Grass" type="SPAWN" x="1040" y="336" width="16" height="16">
    <properties>
     <property name="color" value="blue"/>
-    <property name="spawn" value="grass"/>
+    <property name="spawn" value="GrassActor"/>
    </properties>
   </object>
   <object name="Moving Platform" type="SPAWN" x="272" y="272" width="16" height="16">
    <properties>
-    <property name="spawn" value="moving_platform"/>
+    <property name="spawn" value="MovingPlatformActor"/>
    </properties>
   </object>
  </objectgroup>
 MapManager = require('map_manager')
 ActorManager = require('actor_manager')
 
+ActorFactory = require('actor_factory')
 Actor = require('actors/actor')
 Player = require('actors/player')
 BovineHeadActor = require('actors/bovine_head')
       -- 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)
-      if v.properties.spawn == "player" then
-        local player = Player:new(v.x, v.y)
-        camera:setPosition(v.x, v.y)
-      end
-      if v.properties.spawn == "grass" then
-        local grass = GrassActor:new(v.x, v.y, v.properties.color)
-      end
-      if v.properties.spawn == "cat" then
-        local cat = CatActor(v.x, v.y)
-      end
-      if v.properties.spawn == "moving_platform" then
-        local moving_platform = MovingPlatformActor(v.x, v.y)
-      end
+      ActorFactory.create_actor(v.properties.spawn, v.x, v.y, v.properties)
     end
   end
 end