Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added enemies that walk patterns

  • Loading branch information...
commit 0075be7cf7aafa64a32b580dda5441e3093fe52c 1 parent 2222d4a
@randrews authored
View
5 create_entities.lua
@@ -1,3 +1,5 @@
+local Enemy = require 'enemy'
+
module(..., package.seeall)
local SIZE = 32
@@ -111,6 +113,9 @@ function methods:makeSpecials(specials)
if c >= '0' and c <= '9' then
self.messages[p] = specials[c]
+
+ elseif c >= 'A' and c <= 'Z' then
+ table.insert(self.enemies, Enemy.new(p, self, specials[c]))
end
end
end
View
9 drawing.lua
@@ -61,6 +61,15 @@ function methods:draw()
end
end
+ -- Enemies
+
+ -- Draw player
+ for _, enemy in ipairs(self.enemies) do
+ g.setColor(80, 70, 20)
+ g.circle('fill',
+ enemy.entity.body:getX(), enemy.entity.body:getY(), 15)
+ end
+
self.effect_manager:draw()
end
View
48 enemy.lua
@@ -0,0 +1,48 @@
+module(..., package.seeall)
+
+local SIZE = 32
+local instance = {}
+
+function new(start, level, opts)
+ local e = {}
+ setmetatable(e, {__index=instance})
+
+ e.path = {}
+ e.destination_idx = 1
+ e.speed = SIZE * 2
+
+ assert(#opts%2==0)
+ for n = 1, #opts do
+ if n%2==1 then
+ table.insert(e.path, point(opts[n]*SIZE, opts[n+1]*SIZE) + point(SIZE/2, SIZE/2))
+ end
+ end
+
+ local b = love.physics.newBody(level.world,
+ SIZE*(start.x + 0.5), SIZE*(start.y + 0.5),
+ 'kinematic')
+ local s = love.physics.newCircleShape(15)
+ local _, entity = level.manager:add(b, s, 'enemy')
+ e.entity = entity
+
+ return e
+end
+
+function instance:update(dt)
+ local loc = point(self.entity.body:getPosition())
+ local dest = self.path[self.destination_idx]
+ local delta = dest - loc
+ local dir = delta:normal()
+
+ if delta:magnitude() <= self.speed * dt then
+ self.location = dest
+ self.destination_idx = self.destination_idx + 1
+ if self.destination_idx > #self.path then
+ self.destination_idx = 1
+ end
+ else
+ loc = loc + dir * (self.speed * dt)
+ end
+
+ self.entity.body:setPosition(loc())
+end
View
217 inspect.lua
@@ -0,0 +1,217 @@
+-----------------------------------------------------------------------------------------------------------------------
+-- inspect.lua - v1.1.1 (2011-01)
+-- Enrique García Cota - enrique.garcia.cota [AT] gmail [DOT] com
+-- human-readable representations of tables.
+-- inspired by http://lua-users.org/wiki/TableSerialization
+-----------------------------------------------------------------------------------------------------------------------
+
+-- Apostrophizes the string if it has quotes, but not aphostrophes
+-- Otherwise, it returns a regular quoted string
+local function smartQuote(str)
+ if string.match( string.gsub(str,"[^'\"]",""), '^"+$' ) then
+ return "'" .. str .. "'"
+ end
+ return string.format("%q", str )
+end
+
+local controlCharsTranslation = {
+ ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
+ ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\\"] = "\\\\"
+}
+
+local function unescapeChar(c) return controlCharsTranslation[c] end
+
+local function unescape(str)
+ local result, _ = string.gsub( str, "(%c)", unescapeChar )
+ return result
+end
+
+local function isIdentifier(str)
+ return string.match( str, "^[_%a][_%a%d]*$" )
+end
+
+local function isArrayKey(k, length)
+ return type(k)=='number' and 1 <= k and k <= length
+end
+
+local function isDictionaryKey(k, length)
+ return not isArrayKey(k, length)
+end
+
+local sortOrdersByType = {
+ ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
+ ['function'] = 5, ['userdata'] = 6, ['thread'] = 7
+}
+
+local function sortKeys(a,b)
+ local ta, tb = type(a), type(b)
+ if ta ~= tb then return sortOrdersByType[ta] < sortOrdersByType[tb] end
+ if ta == 'string' or ta == 'number' then return a < b end
+ return false
+end
+
+local function getDictionaryKeys(t)
+ local length = #t
+ local keys = {}
+ for k,_ in pairs(t) do
+ if isDictionaryKey(k, length) then table.insert(keys,k) end
+ end
+ table.sort(keys, sortKeys)
+ return keys
+end
+
+local function getToStringResultSafely(t, mt)
+ local __tostring = type(mt) == 'table' and mt.__tostring
+ local string, status
+ if type(__tostring) == 'function' then
+ status, string = pcall(__tostring, t)
+ string = status and string or 'error: ' .. tostring(string)
+ end
+ return string
+end
+
+local Inspector = {}
+
+function Inspector:new(v, depth)
+ local inspector = {
+ buffer = {},
+ depth = depth,
+ level = 0,
+ counters = {
+ ['function'] = 0,
+ ['userdata'] = 0,
+ ['thread'] = 0,
+ ['table'] = 0
+ },
+ pools = {
+ ['function'] = setmetatable({}, {__mode = "kv"}),
+ ['userdata'] = setmetatable({}, {__mode = "kv"}),
+ ['thread'] = setmetatable({}, {__mode = "kv"}),
+ ['table'] = setmetatable({}, {__mode = "kv"})
+ }
+ }
+
+ setmetatable( inspector, {
+ __index = Inspector,
+ __tostring = function(instance) return table.concat(instance.buffer) end
+ } )
+ return inspector:putValue(v)
+end
+
+function Inspector:puts(...)
+ local args = {...}
+ for i=1, #args do
+ table.insert(self.buffer, tostring(args[i]))
+ end
+ return self
+end
+
+function Inspector:tabify()
+ self:puts("\n", string.rep(" ", self.level))
+ return self
+end
+
+function Inspector:up()
+ self.level = self.level - 1
+end
+
+function Inspector:down()
+ self.level = self.level + 1
+end
+
+function Inspector:putComma(comma)
+ if comma then self:puts(',') end
+ return true
+end
+
+function Inspector:putTable(t)
+ if self:alreadySeen(t) then
+ self:puts('<table ', self:getOrCreateCounter(t), '>')
+ elseif self.level >= self.depth then
+ self:puts('{...}')
+ else
+ self:puts('<',self:getOrCreateCounter(t),'>{')
+ self:down()
+
+ local length = #t
+ local mt = getmetatable(t)
+
+ local string = getToStringResultSafely(t, mt)
+ if type(string) == 'string' and #string > 0 then
+ self:puts(' -- ', unescape(string))
+ if length >= 1 then self:tabify() end -- tabify the array values
+ end
+
+ local comma = false
+ for i=1, length do
+ comma = self:putComma(comma)
+ self:puts(' '):putValue(t[i])
+ end
+
+ local dictKeys = getDictionaryKeys(t)
+
+ for _,k in ipairs(dictKeys) do
+ comma = self:putComma(comma)
+ self:tabify():putKey(k):puts(' = '):putValue(t[k])
+ end
+
+ if mt then
+ comma = self:putComma(comma)
+ self:tabify():puts('<metatable> = '):putValue(mt)
+ end
+ self:up()
+
+ if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
+ self:tabify()
+ elseif length > 0 then -- array tables have one extra space before closing }
+ self:puts(' ')
+ end
+ self:puts('}')
+ end
+ return self
+end
+
+function Inspector:alreadySeen(v)
+ local tv = type(v)
+ return self.pools[tv][v] ~= nil
+end
+
+function Inspector:getOrCreateCounter(v)
+ local tv = type(v)
+ local current = self.pools[tv][v]
+ if not current then
+ current = self.counters[tv] + 1
+ self.counters[tv] = current
+ self.pools[tv][v] = current
+ end
+ return current
+end
+
+function Inspector:putValue(v)
+ local tv = type(v)
+
+ if tv == 'string' then
+ self:puts(smartQuote(unescape(v)))
+ elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
+ self:puts(tostring(v))
+ elseif tv == 'table' then
+ self:putTable(v)
+ else
+ self:puts('<',tv,' ',self:getOrCreateCounter(v),'>')
+ end
+ return self
+end
+
+function Inspector:putKey(k)
+ if type(k) == "string" and isIdentifier(k) then
+ return self:puts(k)
+ end
+ return self:puts( "[" ):putValue(k):puts("]")
+end
+
+local function inspect(t, depth)
+ depth = depth or 4
+ return tostring(Inspector:new(t, depth))
+end
+
+return inspect
View
2  level.lua
@@ -49,6 +49,8 @@ function methods:init(specials)
self.messages = {} -- Map from point to msg structure
-- A message looks like: {"text", dx=1, dy=2}
+ self.enemies = {} -- Array of Enemies
+
self:makeEdges()
self:makeWalls()
self:makeCrates()
View
10 main.lua
@@ -1,5 +1,6 @@
assert(love, "Run this inside Love")
+inspect = dofile 'inspect.lua'
local Level = require('level')
local point = require('point')
@@ -19,7 +20,7 @@ local level = Level.new{
".....#...#.......##.#....",
"...#...#...#........#....",
".....#...#...............",
- "....................2....",
+ "...A................2....",
".*********...............",
".........................",
@@ -27,7 +28,12 @@ local level = Level.new{
dx=50, dy=0}, -- Delta from center of marker to top-left of box.
["2"]={"This is also a message",
- dx=-200, dy=0}
+ dx=-200, dy=0},
+
+ A={
+ 6, 13, -- Waypoint coords
+ 3, 13
+ }
}
function love.load()
View
4 physics.lua
@@ -44,6 +44,10 @@ function methods:update(dt)
nudgeToSquare(c.body, sq, SIZE)
end
+ for _, e in ipairs(self.enemies) do
+ e:update(dt)
+ end
+
max_speed(p.body, SIZE * 10)
self.manager:cull()
self.world:update(dt)
View
21 point.lua
@@ -56,6 +56,18 @@ function methods:dist(pt2, max)
end
end
+-- Length of a line from (0,0) to self
+function methods:length()
+ return math.sqrt((self.x * self.x) + (self.y * self.y))
+end
+
+methods.magnitude = methods.length
+
+-- Return a point with the same direction as self, but length 1
+function methods:normal()
+ return self / self:length()
+end
+
function instance.__add(pt1, pt2)
assert(pt1 and pt2)
return new(pt1.x+pt2.x, pt1.y+pt2.y)
@@ -75,6 +87,15 @@ function instance.__mul(pt1, pt2)
end
end
+function instance.__div(pt1, pt2)
+ assert(pt1 and pt2)
+ if type(pt2) == 'number' then
+ return new(pt1.x / pt2, pt1.y / pt2)
+ else
+ return new(pt1.x/pt2.x, pt1.y/pt2.y)
+ end
+end
+
methods.translate = instance.__add
function instance.__tostring(pt)
Please sign in to comment.
Something went wrong with that request. Please try again.