For the parent class:
Code: Select all
Npc = Class{}
function Npc:init(map, spritesheet, width, height, xPos, yPos)
self.walking_speed = 90
-- supports npc placement
self.x = xPos or math.floor(VIRTUAL_WIDTH / 2)
self.y = yPos or math.floor(VIRTUAL_HEIGHT / 2)
self.width = width or 25
self.height = height or 32
-- supporting flipping to change direction
self.xOffset = math.floor(self.width / 2)
self.yOffset = math.floor(self.width / 2)
-- link player to map
self.map = map
-- states
self.state = 'idle'
-- determines sprite flipping
self.direction = 'left'
-- x and y velocity
self.dx = 0
self.dy = 0
-- grab player spritesheet
self.texture = love.graphics.newImage(spritesheet)
-- animation handling
self.frames = {}
self.quadFrames = generateQuads(self.texture, self.width, self.height)
self.currentFrame = nil
self.animations = {
['idle'] = Animation({
texture = self.texture,
frames = {
self.quadFrames[1]
}
}),
['walking_down'] = Animation({
texture = self.texture,
frames = {
self.quadFrames[4],
self.quadFrames[1],
self.quadFrames[2],
self.quadFrames[3],
self.quadFrames[3]
},
interval = 0.2
}),
['walking_up'] = Animation({
texture = self.texture,
frames = {
self.quadFrames[5],
self.quadFrames[6],
self.quadFrames[7],
self.quadFrames[8],
self.quadFrames[8]
},
interval = 0.2
}),
['walking_sideways'] = Animation({
texture = self.texture,
frames = {
self.quadFrames[10],
self.quadFrames[9],
self.quadFrames[12],
self.quadFrames[11]
},
interval = 0.16
})
}
-- starting animation
self.animation = self.animations['idle']
self.currentFrame = self.animation:getCurrentFrame()
-- behaviour map that we can call based on movement and player state
self.behaviours = {
['idle'] = function(dt)
if self.dy > 0 then
self.dy = self.walking_speed
self.state = 'walking_down'
self.animations['walking_down']:restart()
self.animation = self.animations['walking_down']
elseif self.dy < 0 then
self.dy = -self.walking_speed
self.state = 'walking_up'
self.animations['walking_up']:restart()
self.animation = self.animations['walking_up']
elseif self.dx < 0 then
self.dx = -self.walking_speed
self.state = 'walking_sideways'
self.animations['walking_sideways']:restart()
self.animation = self.animations['walking_sideways']
self.direction = 'left'
elseif self.dx > 0 then
self.dx = self.walking_speed
self.state = 'walking_sideways'
self.animations['walking_sideways']:restart()
self.animation = self.animations['walking_sideways']
self.direction = 'right'
else
self.dy = 0
end
end,
['walking_down'] = function(dt)
if self.dy > 0 then
self.dy = self.walking_speed
else
self.dy = 0
self.state = 'idle'
self.animation = self.animations['idle']
end
end,
['walking_up'] = function(dt)
if self.dy < 0 then
self.dy = -self.walking_speed
else
self.dy = 0
self.state = 'idle'
self.animation = self.animations['idle']
end
end,
['walking_sideways'] = function(dt)
if self.dx < 0 then
self.dx = -self.walking_speed
self.direction = 'left'
elseif self.dx > 0 then
self.dx = self.walking_speed
self.direction = 'right'
else
self.dx = 0
self.state = 'idle'
self.animation = self.animations['idle']
end
end
}
end
---add methods here...
Code: Select all
require 'classes/Npc'
Player = Class{__includes = Npc}
function Player:init(map, spritesheet, width, height, xPos, yPos)
Npc:init(map, spritesheet, width, height, xPos, yPos)
self.hp = 100
self.mana = 100
end
function Player:directionChange()
if love.keyboard.isDown('down') then
self.dy = self.walking_speed
self.dx = 0
elseif love.keyboard.isDown('up') then
self.dy = -self.walking_speed
self.dx = 0
elseif love.keyboard.isDown('left') then
self.dx = -self.walking_speed
self.dy = 0
elseif love.keyboard.isDown('right') then
self.dx = self.walking_speed
self.dy = 0
else
self.dx = 0
self.dy = 0
end
end
-- updates the player over time
function Player:update(dt)
self:directionChange()
self.behaviours[self.state](dt)
self.animation:update(dt)
self.currentFrame = self.animation:getCurrentFrame()
self.x = self.x + self.dx * dt
self.y = self.y + self.dy * dt
end
Some background: I'm very new to programming/lua/love2d and am trying to learn OOP via game development, so I apologise if the terminology I used here was incorrect/didn't make any sense! I hope I still got my message across nonetheless, if you need clarification please ask. If I did use incorrect terminology, feel free to correct me: it will be a great learning experience.