characters are now mere objects which happen to walk areound
This commit is contained in:
parent
40a71e3855
commit
c3759f1e1c
@ -1,10 +1,10 @@
|
|||||||
local anim8 = require 'anim8'
|
local anim8 = require 'libs/anim8'
|
||||||
local character = require 'character'
|
local object = require 'object'
|
||||||
|
|
||||||
Animator = class()
|
Animator = class()
|
||||||
|
|
||||||
function Animator:__init(fullWidth, fullHeight)
|
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_up = anim8.newAnimation(self.animationGrid('1-9', 9), 0.1)
|
||||||
self.walk_left = anim8.newAnimation(self.animationGrid('1-9', 10), 0.1)
|
self.walk_left = anim8.newAnimation(self.animationGrid('1-9', 10), 0.1)
|
||||||
|
@ -182,7 +182,7 @@ return {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
type = "objectgroup",
|
type = "objectgroup",
|
||||||
name = "characters",
|
name = "objects",
|
||||||
visible = true,
|
visible = true,
|
||||||
opacity = 1,
|
opacity = 1,
|
||||||
properties = {},
|
properties = {},
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
eJxjYBiaIJSPuuaVUtm8UTAKRgEquMxKXfM+U9k8XOAeFyo9ChAAANQNBIc=
|
eJxjYBiaIJSPuuaVUtm8UTAKRgEquMxKXfM+U9k8XOAeFyo9ChAAANQNBIc=
|
||||||
</data>
|
</data>
|
||||||
</layer>
|
</layer>
|
||||||
<objectgroup name="characters">
|
<objectgroup name="objects">
|
||||||
<object name="matty" x="416" y="96" width="32" height="64"/>
|
<object name="matty" x="416" y="96" width="32" height="64"/>
|
||||||
<object name="player" x="320" y="480" width="32" height="64"/>
|
<object name="player" x="320" y="480" width="32" height="64"/>
|
||||||
</objectgroup>
|
</objectgroup>
|
||||||
|
@ -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
|
|
@ -1,14 +1,14 @@
|
|||||||
local character = require '../character'
|
local object = require '../object'
|
||||||
local animator = require '../animator'
|
local animator = require '../animator'
|
||||||
|
|
||||||
Gridwalker = class()
|
Gridwalker = class()
|
||||||
|
|
||||||
Gridwalker.speed = 2
|
Gridwalker.speed = 2
|
||||||
Gridwalker.collisionTestSize = 12
|
Gridwalker.collisionTestSize = 12
|
||||||
Gridwalker.neighbourOffsetX = (character.width - Gridwalker.collisionTestSize) / 2
|
Gridwalker.neighbourOffsetX = (object.width - Gridwalker.collisionTestSize) / 2
|
||||||
Gridwalker.neighbourOffsetY = character.width - Gridwalker.collisionTestSize * 1.5
|
Gridwalker.neighbourOffsetY = object.width - Gridwalker.collisionTestSize * 1.5
|
||||||
Gridwalker.testShape = nil
|
Gridwalker.testShape = nil
|
||||||
Gridwalker.charinfo = nil
|
Gridwalker.objectinfo = nil
|
||||||
Gridwalker.animation = nil
|
Gridwalker.animation = nil
|
||||||
|
|
||||||
function Gridwalker:sendKey(key)
|
function Gridwalker:sendKey(key)
|
||||||
@ -34,8 +34,8 @@ function Gridwalker:stopAnimation()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Gridwalker:up()
|
function Gridwalker:up()
|
||||||
local newX = self.charinfo.x
|
local newX = self.objectinfo.x
|
||||||
local newY = self.charinfo.y - self.speed
|
local newY = self.objectinfo.y - self.speed
|
||||||
|
|
||||||
self.animation = self.animator.walk_up
|
self.animation = self.animator.walk_up
|
||||||
|
|
||||||
@ -43,8 +43,8 @@ function Gridwalker:up()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Gridwalker:down()
|
function Gridwalker:down()
|
||||||
local newX = self.charinfo.x
|
local newX = self.objectinfo.x
|
||||||
local newY = self.charinfo.y + self.speed
|
local newY = self.objectinfo.y + self.speed
|
||||||
|
|
||||||
self.animation = self.animator.walk_down
|
self.animation = self.animator.walk_down
|
||||||
|
|
||||||
@ -52,8 +52,8 @@ function Gridwalker:down()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Gridwalker:right()
|
function Gridwalker:right()
|
||||||
local newX = self.charinfo.x + self.speed
|
local newX = self.objectinfo.x + self.speed
|
||||||
local newY = self.charinfo.y
|
local newY = self.objectinfo.y
|
||||||
|
|
||||||
self.animation = self.animator.walk_right
|
self.animation = self.animator.walk_right
|
||||||
|
|
||||||
@ -61,8 +61,8 @@ function Gridwalker:right()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Gridwalker:left()
|
function Gridwalker:left()
|
||||||
local newX = self.charinfo.x - self.speed
|
local newX = self.objectinfo.x - self.speed
|
||||||
local newY = self.charinfo.y
|
local newY = self.objectinfo.y
|
||||||
|
|
||||||
self.animation = self.animator.walk_left
|
self.animation = self.animator.walk_left
|
||||||
|
|
||||||
@ -70,10 +70,10 @@ function Gridwalker:left()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Gridwalker:init()
|
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)
|
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 = self.animator.walk_up
|
||||||
self.animation:pauseAtStart()
|
self.animation:pauseAtStart()
|
||||||
end
|
end
|
||||||
@ -101,9 +101,9 @@ function Gridwalker:move(x, y)
|
|||||||
|
|
||||||
if noCollision then
|
if noCollision then
|
||||||
self.animation:resume()
|
self.animation:resume()
|
||||||
self.charinfo.x = x
|
self.objectinfo.x = x
|
||||||
self.charinfo.y = y
|
self.objectinfo.y = y
|
||||||
else
|
else
|
||||||
self:setTestShape(self.charinfo.x, self.charinfo.y)
|
self:setTestShape(self.objectinfo.x, self.objectinfo.y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
64
src/main.lua
64
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
|
-- has to come before the sti initialization so I can add the collision tiles
|
||||||
collider = HC(100)
|
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 ui = require "ui"
|
||||||
|
|
||||||
local characters = {}
|
local objects = {}
|
||||||
|
|
||||||
function love.load(arg)
|
function love.load(arg)
|
||||||
if arg[#arg] == "-debug" then require("mobdebug").start() end -- debugging in ZeroBraineStudio
|
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")
|
map = sti.new("assets/map")
|
||||||
collision = map:getCollisionMap("collision")
|
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?")
|
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
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
if not ui.active then
|
if not ui.active then
|
||||||
for _, char in ipairs(characters) do
|
for _, object in ipairs(objects) do
|
||||||
char.controller.animation:update(dt)
|
object.controller.animation:update(dt)
|
||||||
|
|
||||||
if char.relevantKeys then
|
if object.relevantKeys then
|
||||||
for _, relevantKey in ipairs(char.relevantKeys) do
|
for _, relevantKey in ipairs(object.relevantKeys) do
|
||||||
if love.keyboard.isDown(relevantKey) then
|
if love.keyboard.isDown(relevantKey) then
|
||||||
char.controller:sendKey(relevantKey)
|
object.controller:sendKey(relevantKey)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -50,11 +50,11 @@ function love.update(dt)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function love.keyreleased(key)
|
function love.keyreleased(key)
|
||||||
for _, char in ipairs(characters) do
|
for _, object in ipairs(objects) do
|
||||||
if char.relevantKeys then
|
if object.relevantKeys then
|
||||||
for _, relevantKey in ipairs(char.relevantKeys) do
|
for _, relevantKey in ipairs(object.relevantKeys) do
|
||||||
if key == relevantKey then
|
if key == relevantKey then
|
||||||
char.controller:stopAnimation()
|
object.controller:stopAnimation()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -85,30 +85,30 @@ function debugDrawing()
|
|||||||
map:drawCollisionMap(collision)
|
map:drawCollisionMap(collision)
|
||||||
end
|
end
|
||||||
|
|
||||||
function initCharacters()
|
function initObjects()
|
||||||
map:addCustomLayer("character layer", 4)
|
map:addCustomLayer("object layer", 4)
|
||||||
|
|
||||||
local characterLayer = map.layers["character layer"]
|
local objectLayer = map.layers["object layer"]
|
||||||
characterLayer.sprites = {}
|
objectLayer.sprites = {}
|
||||||
|
|
||||||
for _, char in pairs(map.layers["characters"].objects) do
|
for _, obj in pairs(map.layers["objects"].objects) do
|
||||||
local filename = "characters/" .. char.name .. ".lua"
|
local filename = "objects/" .. obj.name .. ".lua"
|
||||||
local charinfo = love.filesystem.load(filename)()
|
local objectinfo = love.filesystem.load(filename)()
|
||||||
charinfo.x = char.x - character.width / 4
|
objectinfo.x = obj.x - object.width / 4
|
||||||
charinfo.y = char.y
|
objectinfo.y = obj.y
|
||||||
|
|
||||||
charinfo.controller.charinfo = charinfo
|
objectinfo.controller.objectinfo = objectinfo
|
||||||
|
|
||||||
charinfo.image = love.graphics.newImage(charinfo.spritesheet)
|
objectinfo.image = love.graphics.newImage(objectinfo.spritesheet)
|
||||||
charinfo.controller:init()
|
objectinfo.controller:init()
|
||||||
|
|
||||||
table.insert(characterLayer.sprites, {info = charinfo})
|
table.insert(objectLayer.sprites, {info = objectinfo})
|
||||||
table.insert(characters, charinfo)
|
table.insert(objects, objectinfo)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Draw callback for Custom Layer
|
-- Draw callback for Custom Layer
|
||||||
function characterLayer:draw()
|
function objectLayer:draw()
|
||||||
table.sort(self.sprites, character.sortNorthSouth)
|
table.sort(self.sprites, object.sortNorthSouth)
|
||||||
|
|
||||||
for _, sprite in pairs(self.sprites) do
|
for _, sprite in pairs(self.sprites) do
|
||||||
local x = math.floor(sprite.info.x)
|
local x = math.floor(sprite.info.x)
|
||||||
@ -118,5 +118,5 @@ function initCharacters()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
map.layers["characters"].visible = false
|
map.layers["objects"].visible = false
|
||||||
end
|
end
|
||||||
|
10
src/object.lua
Normal file
10
src/object.lua
Normal 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
|
@ -1,8 +1,8 @@
|
|||||||
local character = require "../character"
|
local object = require "../object"
|
||||||
local gridwalker = require "../controllers/gridwalker"
|
local gridwalker = require "../controllers/gridwalker"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
spritesheet = "assets/characters/matty.png",
|
spritesheet = "assets/characters/matty.png",
|
||||||
pose = character.walking_down,
|
pose = object.walking_down,
|
||||||
controller = Gridwalker()
|
controller = Gridwalker()
|
||||||
}
|
}
|
@ -1,9 +1,9 @@
|
|||||||
local character = require "../character"
|
local object = require "../object"
|
||||||
local gridwalker = require "../controllers/gridwalker"
|
local gridwalker = require "../controllers/gridwalker"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
spritesheet = "assets/characters/player.png",
|
spritesheet = "assets/characters/player.png",
|
||||||
pose = character.walking_up,
|
pose = object.walking_up,
|
||||||
controller = Gridwalker(),
|
controller = Gridwalker(),
|
||||||
relevantKeys = {
|
relevantKeys = {
|
||||||
"w", "up",
|
"w", "up",
|
@ -107,7 +107,6 @@ function ui:sendKey(key)
|
|||||||
|
|
||||||
if key == "return" then
|
if key == "return" then
|
||||||
ui.active = false
|
ui.active = false
|
||||||
print(ui.active)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user