characters are now mere objects which happen to walk areound

This commit is contained in:
Sebastian Hugentobler 2014-05-21 10:30:54 +02:00
parent 40a71e3855
commit c3759f1e1c
21 changed files with 68 additions and 69 deletions

View File

@ -1,10 +1,10 @@
local anim8 = require 'anim8'
local character = require 'character'
local anim8 = require 'libs/anim8'
local object = require 'object'
Animator = class()
function Animator:__init(fullWidth, fullHeight)
self.animationGrid = anim8.newGrid(character.width, character.height, fullWidth, fullHeight)
self.animationGrid = anim8.newGrid(object.width, object.height, fullWidth, fullHeight)
self.walk_up = anim8.newAnimation(self.animationGrid('1-9', 9), 0.1)
self.walk_left = anim8.newAnimation(self.animationGrid('1-9', 10), 0.1)

View File

@ -182,7 +182,7 @@ return {
},
{
type = "objectgroup",
name = "characters",
name = "objects",
visible = true,
opacity = 1,
properties = {},

View File

@ -27,7 +27,7 @@
eJxjYBiaIJSPuuaVUtm8UTAKRgEquMxKXfM+U9k8XOAeFyo9ChAAANQNBIc=
</data>
</layer>
<objectgroup name="characters">
<objectgroup name="objects">
<object name="matty" x="416" y="96" width="32" height="64"/>
<object name="player" x="320" y="480" width="32" height="64"/>
</objectgroup>

View File

@ -1,10 +0,0 @@
local character = {}
character.width = 64
character.height = 64
function character.sortNorthSouth(a, b)
return a.info.y < b.info.y
end
return character

View File

@ -1,14 +1,14 @@
local character = require '../character'
local object = require '../object'
local animator = require '../animator'
Gridwalker = class()
Gridwalker.speed = 2
Gridwalker.collisionTestSize = 12
Gridwalker.neighbourOffsetX = (character.width - Gridwalker.collisionTestSize) / 2
Gridwalker.neighbourOffsetY = character.width - Gridwalker.collisionTestSize * 1.5
Gridwalker.neighbourOffsetX = (object.width - Gridwalker.collisionTestSize) / 2
Gridwalker.neighbourOffsetY = object.width - Gridwalker.collisionTestSize * 1.5
Gridwalker.testShape = nil
Gridwalker.charinfo = nil
Gridwalker.objectinfo = nil
Gridwalker.animation = nil
function Gridwalker:sendKey(key)
@ -34,8 +34,8 @@ function Gridwalker:stopAnimation()
end
function Gridwalker:up()
local newX = self.charinfo.x
local newY = self.charinfo.y - self.speed
local newX = self.objectinfo.x
local newY = self.objectinfo.y - self.speed
self.animation = self.animator.walk_up
@ -43,8 +43,8 @@ function Gridwalker:up()
end
function Gridwalker:down()
local newX = self.charinfo.x
local newY = self.charinfo.y + self.speed
local newX = self.objectinfo.x
local newY = self.objectinfo.y + self.speed
self.animation = self.animator.walk_down
@ -52,8 +52,8 @@ function Gridwalker:down()
end
function Gridwalker:right()
local newX = self.charinfo.x + self.speed
local newY = self.charinfo.y
local newX = self.objectinfo.x + self.speed
local newY = self.objectinfo.y
self.animation = self.animator.walk_right
@ -61,8 +61,8 @@ function Gridwalker:right()
end
function Gridwalker:left()
local newX = self.charinfo.x - self.speed
local newY = self.charinfo.y
local newX = self.objectinfo.x - self.speed
local newY = self.objectinfo.y
self.animation = self.animator.walk_left
@ -70,10 +70,10 @@ function Gridwalker:left()
end
function Gridwalker:init()
self.testShape = collider:addRectangle(self.charinfo.x + self.neighbourOffsetX, self.charinfo.y + self.neighbourOffsetY, self.collisionTestSize, self.collisionTestSize)
self.testShape = collider:addRectangle(self.objectinfo.x + self.neighbourOffsetX, self.objectinfo.y + self.neighbourOffsetY, self.collisionTestSize, self.collisionTestSize)
collider:setPassive(self.testShape)
self.animator = Animator:new(self.charinfo.image:getWidth(), self.charinfo.image:getHeight())
self.animator = Animator:new(self.objectinfo.image:getWidth(), self.objectinfo.image:getHeight())
self.animation = self.animator.walk_up
self.animation:pauseAtStart()
end
@ -101,9 +101,9 @@ function Gridwalker:move(x, y)
if noCollision then
self.animation:resume()
self.charinfo.x = x
self.charinfo.y = y
self.objectinfo.x = x
self.objectinfo.y = y
else
self:setTestShape(self.charinfo.x, self.charinfo.y)
self:setTestShape(self.objectinfo.x, self.objectinfo.y)
end
end

View File

@ -1,15 +1,15 @@
local HC = require 'hardoncollider'
local HC = require 'libs/hardoncollider'
-- has to come before the sti initialization so I can add the collision tiles
collider = HC(100)
class = require '30log'
class = require 'libs/30log'
local sti = require "sti"
local sti = require "libs/sti"
local character = require "character"
local object = require "object"
local ui = require "ui"
local characters = {}
local objects = {}
function love.load(arg)
if arg[#arg] == "-debug" then require("mobdebug").start() end -- debugging in ZeroBraineStudio
@ -20,19 +20,19 @@ function love.load(arg)
map = sti.new("assets/map")
collision = map:getCollisionMap("collision")
initCharacters()
initObjects()
ui:showMessage("Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?")
end
function love.update(dt)
if not ui.active then
for _, char in ipairs(characters) do
char.controller.animation:update(dt)
for _, object in ipairs(objects) do
object.controller.animation:update(dt)
if char.relevantKeys then
for _, relevantKey in ipairs(char.relevantKeys) do
if object.relevantKeys then
for _, relevantKey in ipairs(object.relevantKeys) do
if love.keyboard.isDown(relevantKey) then
char.controller:sendKey(relevantKey)
object.controller:sendKey(relevantKey)
end
end
end
@ -50,11 +50,11 @@ function love.update(dt)
end
function love.keyreleased(key)
for _, char in ipairs(characters) do
if char.relevantKeys then
for _, relevantKey in ipairs(char.relevantKeys) do
for _, object in ipairs(objects) do
if object.relevantKeys then
for _, relevantKey in ipairs(object.relevantKeys) do
if key == relevantKey then
char.controller:stopAnimation()
object.controller:stopAnimation()
end
end
end
@ -85,30 +85,30 @@ function debugDrawing()
map:drawCollisionMap(collision)
end
function initCharacters()
map:addCustomLayer("character layer", 4)
function initObjects()
map:addCustomLayer("object layer", 4)
local characterLayer = map.layers["character layer"]
characterLayer.sprites = {}
local objectLayer = map.layers["object layer"]
objectLayer.sprites = {}
for _, char in pairs(map.layers["characters"].objects) do
local filename = "characters/" .. char.name .. ".lua"
local charinfo = love.filesystem.load(filename)()
charinfo.x = char.x - character.width / 4
charinfo.y = char.y
for _, obj in pairs(map.layers["objects"].objects) do
local filename = "objects/" .. obj.name .. ".lua"
local objectinfo = love.filesystem.load(filename)()
objectinfo.x = obj.x - object.width / 4
objectinfo.y = obj.y
charinfo.controller.charinfo = charinfo
objectinfo.controller.objectinfo = objectinfo
charinfo.image = love.graphics.newImage(charinfo.spritesheet)
charinfo.controller:init()
objectinfo.image = love.graphics.newImage(objectinfo.spritesheet)
objectinfo.controller:init()
table.insert(characterLayer.sprites, {info = charinfo})
table.insert(characters, charinfo)
table.insert(objectLayer.sprites, {info = objectinfo})
table.insert(objects, objectinfo)
end
-- Draw callback for Custom Layer
function characterLayer:draw()
table.sort(self.sprites, character.sortNorthSouth)
function objectLayer:draw()
table.sort(self.sprites, object.sortNorthSouth)
for _, sprite in pairs(self.sprites) do
local x = math.floor(sprite.info.x)
@ -118,5 +118,5 @@ function initCharacters()
end
end
map.layers["characters"].visible = false
map.layers["objects"].visible = false
end

10
src/object.lua Normal file
View File

@ -0,0 +1,10 @@
local object = {}
object.width = 64
object.height = 64
function object.sortNorthSouth(a, b)
return a.info.y < b.info.y
end
return object

View File

@ -1,8 +1,8 @@
local character = require "../character"
local object = require "../object"
local gridwalker = require "../controllers/gridwalker"
return {
spritesheet = "assets/characters/matty.png",
pose = character.walking_down,
pose = object.walking_down,
controller = Gridwalker()
}

View File

@ -1,9 +1,9 @@
local character = require "../character"
local object = require "../object"
local gridwalker = require "../controllers/gridwalker"
return {
spritesheet = "assets/characters/player.png",
pose = character.walking_up,
pose = object.walking_up,
controller = Gridwalker(),
relevantKeys = {
"w", "up",

View File

@ -107,7 +107,6 @@ function ui:sendKey(key)
if key == "return" then
ui.active = false
print(ui.active)
end
end