From c3759f1e1c86ea6999048ac4d6c80bc9c26e3642 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 21 May 2014 10:30:54 +0200 Subject: [PATCH] characters are now mere objects which happen to walk areound --- src/animator.lua | 6 +- src/assets/map.lua | 2 +- src/assets/map.tmx | 2 +- src/character.lua | 10 --- src/controllers/gridwalker.lua | 34 +++++----- src/{ => libs}/30log.lua | 0 src/{ => libs}/anim8.lua | 0 src/{ => libs}/hardoncollider/README | 0 src/{ => libs}/hardoncollider/class.lua | 0 src/{ => libs}/hardoncollider/gjk.lua | 0 src/{ => libs}/hardoncollider/init.lua | 0 src/{ => libs}/hardoncollider/polygon.lua | 0 src/{ => libs}/hardoncollider/shapes.lua | 0 src/{ => libs}/hardoncollider/spatialhash.lua | 0 .../hardoncollider/vector-light.lua | 0 src/{ => libs}/sti.lua | 0 src/main.lua | 64 +++++++++---------- src/object.lua | 10 +++ src/{characters => objects}/matty.lua | 4 +- src/{characters => objects}/player.lua | 4 +- src/ui.lua | 1 - 21 files changed, 68 insertions(+), 69 deletions(-) delete mode 100644 src/character.lua rename src/{ => libs}/30log.lua (100%) rename src/{ => libs}/anim8.lua (100%) rename src/{ => libs}/hardoncollider/README (100%) rename src/{ => libs}/hardoncollider/class.lua (100%) rename src/{ => libs}/hardoncollider/gjk.lua (100%) rename src/{ => libs}/hardoncollider/init.lua (100%) rename src/{ => libs}/hardoncollider/polygon.lua (100%) rename src/{ => libs}/hardoncollider/shapes.lua (100%) rename src/{ => libs}/hardoncollider/spatialhash.lua (100%) rename src/{ => libs}/hardoncollider/vector-light.lua (100%) rename src/{ => libs}/sti.lua (100%) create mode 100644 src/object.lua rename src/{characters => objects}/matty.lua (65%) rename src/{characters => objects}/player.lua (76%) diff --git a/src/animator.lua b/src/animator.lua index 575ba0c..2975dab 100644 --- a/src/animator.lua +++ b/src/animator.lua @@ -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) diff --git a/src/assets/map.lua b/src/assets/map.lua index 30773ba..42cc88b 100644 --- a/src/assets/map.lua +++ b/src/assets/map.lua @@ -182,7 +182,7 @@ return { }, { type = "objectgroup", - name = "characters", + name = "objects", visible = true, opacity = 1, properties = {}, diff --git a/src/assets/map.tmx b/src/assets/map.tmx index 9805198..ea47cd5 100644 --- a/src/assets/map.tmx +++ b/src/assets/map.tmx @@ -27,7 +27,7 @@ eJxjYBiaIJSPuuaVUtm8UTAKRgEquMxKXfM+U9k8XOAeFyo9ChAAANQNBIc= - + diff --git a/src/character.lua b/src/character.lua deleted file mode 100644 index 59acc39..0000000 --- a/src/character.lua +++ /dev/null @@ -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 diff --git a/src/controllers/gridwalker.lua b/src/controllers/gridwalker.lua index 6c52cc9..fe66607 100644 --- a/src/controllers/gridwalker.lua +++ b/src/controllers/gridwalker.lua @@ -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 diff --git a/src/30log.lua b/src/libs/30log.lua similarity index 100% rename from src/30log.lua rename to src/libs/30log.lua diff --git a/src/anim8.lua b/src/libs/anim8.lua similarity index 100% rename from src/anim8.lua rename to src/libs/anim8.lua diff --git a/src/hardoncollider/README b/src/libs/hardoncollider/README similarity index 100% rename from src/hardoncollider/README rename to src/libs/hardoncollider/README diff --git a/src/hardoncollider/class.lua b/src/libs/hardoncollider/class.lua similarity index 100% rename from src/hardoncollider/class.lua rename to src/libs/hardoncollider/class.lua diff --git a/src/hardoncollider/gjk.lua b/src/libs/hardoncollider/gjk.lua similarity index 100% rename from src/hardoncollider/gjk.lua rename to src/libs/hardoncollider/gjk.lua diff --git a/src/hardoncollider/init.lua b/src/libs/hardoncollider/init.lua similarity index 100% rename from src/hardoncollider/init.lua rename to src/libs/hardoncollider/init.lua diff --git a/src/hardoncollider/polygon.lua b/src/libs/hardoncollider/polygon.lua similarity index 100% rename from src/hardoncollider/polygon.lua rename to src/libs/hardoncollider/polygon.lua diff --git a/src/hardoncollider/shapes.lua b/src/libs/hardoncollider/shapes.lua similarity index 100% rename from src/hardoncollider/shapes.lua rename to src/libs/hardoncollider/shapes.lua diff --git a/src/hardoncollider/spatialhash.lua b/src/libs/hardoncollider/spatialhash.lua similarity index 100% rename from src/hardoncollider/spatialhash.lua rename to src/libs/hardoncollider/spatialhash.lua diff --git a/src/hardoncollider/vector-light.lua b/src/libs/hardoncollider/vector-light.lua similarity index 100% rename from src/hardoncollider/vector-light.lua rename to src/libs/hardoncollider/vector-light.lua diff --git a/src/sti.lua b/src/libs/sti.lua similarity index 100% rename from src/sti.lua rename to src/libs/sti.lua diff --git a/src/main.lua b/src/main.lua index 147a8b2..2f32394 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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 diff --git a/src/object.lua b/src/object.lua new file mode 100644 index 0000000..7fb9e33 --- /dev/null +++ b/src/object.lua @@ -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 diff --git a/src/characters/matty.lua b/src/objects/matty.lua similarity index 65% rename from src/characters/matty.lua rename to src/objects/matty.lua index f901bf0..91ee30a 100644 --- a/src/characters/matty.lua +++ b/src/objects/matty.lua @@ -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() } diff --git a/src/characters/player.lua b/src/objects/player.lua similarity index 76% rename from src/characters/player.lua rename to src/objects/player.lua index 34d1094..3a60487 100644 --- a/src/characters/player.lua +++ b/src/objects/player.lua @@ -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", diff --git a/src/ui.lua b/src/ui.lua index 95c89bd..604c16a 100644 --- a/src/ui.lua +++ b/src/ui.lua @@ -107,7 +107,6 @@ function ui:sendKey(key) if key == "return" then ui.active = false - print(ui.active) end end