Skip to content

Instantly share code, notes, and snippets.

@fkaa
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save fkaa/15e0dbc8eb2facfff66e to your computer and use it in GitHub Desktop.

Select an option

Save fkaa/15e0dbc8eb2facfff66e to your computer and use it in GitHub Desktop.
local anim8 = require 'lib/anim8'
Entity = {
x = 0, y = 0,
w = 0, h = 0,
vx = 0, vy = 0,
drawable = nil
}
local EntityMT = {__index = Entity}
function Entity:new(o)
local o = setmetatable({}, EntityMT)
return o
end
function Entity:draw()
end
function Entity:update(dt)
end
Bunny = {}
setmetatable(Bunny, {__index = Entity})
local BunnyMT = {__index = Bunny}
BunnyState = {
LEFT_STILL = 0,
RIGHT_STILL = 1,
LEFT_MOVE = 2,
RIGHT_MOVE = 3,
LEFT_JUMP = 4,
RIGHT_JUMP = 5
}
BunnyMood = {
NICE = 0,
EVIL = 1
}
function Bunny:new(img, x, y)
local o = setmetatable({}, BunnyMT)
o.img = img
o.x = x
o.y = y
o.width = 6
o.height = 9
o.animations = {
}
o.drawable = o.animations[BunnyState.LEFT_STILL]
o.mood = BunnyMood.NICE
return o
end
function Bunny:update(dt)
self.vx = self.vx * 0.98
self.vy = self.vy - 9.8 * dt
if love.keyboard.isDown('a') then
self.vx = math.min(self.vx + 0.05, 2.5)
self.drawable = self.animations[BunnyState.LEFT_MOVING]
elseif love.keyboard.isDown('d') then
self.vx = math.max(self.vx - 0.05, -2.5)
self.drawable = self.animations[BunnyState.RIGHT_MOVING]
else
self.drawable = self.animations[self.vx > 0
and BunnyState.RIGHT_STILL
or BunnyState.LEFT_STILL]
end
self.x = self.x + self.vx * dt
self.y = self.y + self.vy * dt
self.drawable:update(dt)
end
function Bunny:draw()
self.drawable:draw(self.img, self.x, self.y)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment