Skip to content
Browse files

Added more graphics including walk and jump animations

  • Loading branch information...
1 parent 72af938 commit 5645448f42175af1f4b2810bb40e6eb6e360f0fc @Middlerun committed
View
203 AnAL.lua
@@ -0,0 +1,203 @@
+--[[
+Copyright (c) 2009-2010 Bart Bes
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+]]
+
+local animation = {}
+animation.__index = animation
+
+--- Create a new animation
+-- Replaces love.graphics.newAnimation
+-- @param image The image that contains the frames
+-- @param fw The frame width
+-- @param fh The frame height
+-- @param delay The delay between two frames
+-- @param frames The number of frames, 0 for autodetect
+-- @return The created animation
+function newAnimation(image, fw, fh, delay, frames)
+ local a = {}
+ a.img = image
+ a.frames = {}
+ a.delays = {}
+ a.timer = 0
+ a.position = 1
+ a.fw = fw
+ a.fh = fh
+ a.playing = true
+ a.speed = 1
+ a.mode = 1
+ a.direction = 1
+ local imgw = image:getWidth()
+ local imgh = image:getHeight()
+ if frames == 0 then
+ frames = imgw / fw * imgh / fh
+ end
+ local rowsize = imgw/fw
+ for i = 1, frames do
+ local row = math.floor((i-1)/rowsize)
+ local column = (i-1)%rowsize
+ local frame = love.graphics.newQuad(column*fw, row*fh, fw, fh, imgw, imgh)
+ table.insert(a.frames, frame)
+ table.insert(a.delays, delay)
+ end
+ return setmetatable(a, animation)
+end
+
+--- Update the animation
+-- @param dt Time that has passed since last call
+function animation:update(dt)
+ if not self.playing then return end
+ self.timer = self.timer + dt * self.speed
+ if self.timer > self.delays[self.position] then
+ self.timer = self.timer - self.delays[self.position]
+ self.position = self.position + 1 * self.direction
+ if self.position > #self.frames then
+ if self.mode == 1 then
+ self.position = 1
+ elseif self.mode == 2 then
+ self.position = self.position - 1
+ self:stop()
+ elseif self.mode == 3 then
+ self.direction = -1
+ self.position = self.position - 1
+ end
+ elseif self.position < 1 and self.mode == 3 then
+ self.direction = 1
+ self.position = self.position + 1
+ end
+ end
+end
+
+--- Draw the animation
+-- @param x The X coordinate
+-- @param y The Y coordinate
+-- @param angle The angle to draw at (radians)
+-- @param sx The scale on the X axis
+-- @param sy The scale on the Y axis
+-- @param ox The X coordinate of the origin
+-- @param oy The Y coordinate of the origin
+function animation:draw(x, y, angle, sx, sy, ox, oy)
+ love.graphics.drawq(self.img, self.frames[self.position], x, y, angle, sx, sy, ox, oy)
+end
+
+--- Add a frame
+-- @param x The X coordinate of the frame on the original image
+-- @param y The Y coordinate of the frame on the original image
+-- @param w The width of the frame
+-- @param h The height of the frame
+-- @param delay The delay before the next frame is shown
+function animation:addFrame(x, y, w, h, delay)
+ local frame = love.graphics.newQuad(x, y, w, h, self.img:getWidth(), self.img:getHeight())
+ table.insert(self.frames, frame)
+ table.insert(self.delays, delay)
+end
+
+--- Play the animation
+-- Starts it if it was stopped.
+-- Basically makes sure it uses the delays
+-- to switch to the next frame.
+function animation:play()
+ self.playing = true
+end
+
+--- Stop the animation
+function animation:stop()
+ self.playing = false
+end
+
+--- Reset
+-- Go back to the first frame.
+function animation:reset()
+ self:seek(1)
+end
+
+--- Seek to a frame
+-- @param frame The frame to display now
+function animation:seek(frame)
+ self.position = frame
+ self.timer = 0
+end
+
+--- Get the currently shown frame
+-- @return The current frame
+function animation:getCurrentFrame()
+ return self.position
+end
+
+--- Get the number of frames
+-- @return The number of frames
+function animation:getSize()
+ return #self.frames
+end
+
+--- Set the delay between frames
+-- @param frame Which frame to set the delay for
+-- @param delay The delay
+function animation:setDelay(frame, delay)
+ self.delays[frame] = delay
+end
+
+--- Set the speed
+-- @param speed The speed to play at (1 is normal, 2 is double, etc)
+function animation:setSpeed(speed)
+ self.speed = speed
+end
+
+--- Get the width of the current frame
+-- @return The width of the current frame
+function animation:getWidth()
+ return self.frames[self.position]:getWidth()
+end
+
+--- Get the height of the current frame
+-- @return The height of the current frame
+function animation:getHeight()
+ return self.frames[self.position]:getHeight()
+end
+
+--- Set the play mode
+-- Could be "loop" to loop it, "once" to play it once, or "bounce" to play it, reverse it, and play it again (looping)
+-- @param mode The mode: one of the above
+function animation:setMode(mode)
+ if mode == "loop" then
+ self.mode = 1
+ elseif mode == "once" then
+ self.mode = 2
+ elseif mode == "bounce" then
+ self.mode = 3
+ end
+end
+
+--- Animations_legacy_support
+-- @usage Add legacy support, basically set love.graphics.newAnimation again, and allow you to use love.graphics.draw
+if Animations_legacy_support then
+ love.graphics.newAnimation = newAnimation
+ local oldLGDraw = love.graphics.draw
+ function love.graphics.draw(item, ...)
+ if type(item) == "table" and item.draw then
+ item:draw(...)
+ else
+ oldLGDraw(item, ...)
+ end
+ end
+end
View
2 collision.lua
@@ -82,6 +82,8 @@ function checkCollisions(terrain, player)
elseif hit == ceiling then
player.vy = 0
elseif hit == ground then
+ landTime = 0.01 * player.vy
+ if landTime < 0.1 then landTime = 0 end
player.vy = 0
player.y = math.ceil(player.y - 0.5)
player.falling = false
View
BIN gfx/rabbitJump1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN gfx/rabbitJump2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN gfx/rabbitLand.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN gfx/rabbitWalk.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN gfx/xcf/rabbitJump1.xcf
Binary file not shown.
View
BIN gfx/xcf/rabbitJump3.xcf
Binary file not shown.
View
BIN gfx/xcf/rabbitLand.xcf
Binary file not shown.
View
BIN gfx/xcf/rabbitStandSmall.xcf
Binary file not shown.
View
BIN gfx/xcf/rabbitWalkAnim.xcf
Binary file not shown.
View
BIN gfx/xcf/rabbitWalkSmall.xcf
Binary file not shown.
View
31 main.lua
@@ -5,6 +5,7 @@ love.filesystem.load("player.lua")()
love.filesystem.load("collision.lua")()
love.filesystem.load("common.lua")()
love.filesystem.load("loadgraphics.lua")()
+love.filesystem.load("AnAL.lua")()
love.filesystem.setIdentity("lovecraft")
view = {zoom = 32, x = 0, y = 0}
@@ -18,6 +19,7 @@ selected = 1
mineBlock = {r = nil, c = nil}
mineProgress = 0
placeTime = 0
+landTime = 0
entities = {}
@@ -53,18 +55,22 @@ function love.update(dt)
if love.keyboard.isDown("a") and not player.againstLeftWall then
player.vx = math.max(-8, player.vx - 16 * dt)
player.direction = -1
+ landTime = 0
elseif love.keyboard.isDown("d") and not player.againstRightWall then
player.vx = math.min( 8, player.vx + 16 * dt)
player.direction = 1
+ landTime = 0
end
end
if not first and not player.falling then
if love.keyboard.isDown("a") and not player.againstLeftWall then
player.vx = math.max(-8, player.vx - 36 * dt)
player.direction = -1
+ landTime = 0
elseif love.keyboard.isDown("d") and not player.againstRightWall then
player.vx = math.min( 8, player.vx + 36 * dt)
player.direction = 1
+ landTime = 0
elseif player.vx > 0 then player.vx = math.max(0, player.vx - 128 * dt)
elseif player.vx < 0 then player.vx = math.min(0, player.vx + 128 * dt)
end
@@ -72,6 +78,13 @@ function love.update(dt)
player.x = player.x + player.vx * dt
player.y = player.y + player.vy * dt
+ if not player.falling and math.abs(player.vx) > 0.5 and (love.keyboard.isDown("a") or love.keyboard.isDown("d")) then
+ if not player.walking then player.walk:seek(5) end
+ player.walking = true
+ else
+ player.walking = false
+ end
+
checkCollisions(terrain, player)
if love.keyboard.isDown("w") and not player.falling then
@@ -148,6 +161,8 @@ function love.update(dt)
end
placeTime = placeTime + dt
+ landTime = landTime - dt
+ player.walk:update(dt)
-- Generate new chunks
for r = math.floor((player.y - 48) / 32), math.floor((player.y + 48) / 32) do
@@ -169,7 +184,17 @@ function love.draw()
drawTerrain(terrain, view.zoom, view.x, view.y)
end
love.graphics.setColor(255, 255, 255, 255)
- love.graphics.draw(player.image, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103)
+ if player.walking then
+ player.walk:draw((player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103)
+ elseif player.falling and player.vy < 0 then
+ love.graphics.draw(player.jump1, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103)
+ elseif player.falling then
+ love.graphics.draw(player.jump2, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103)
+ elseif landTime > 0 then
+ love.graphics.draw(player.land, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103)
+ else
+ love.graphics.draw(player.stand, (player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y+0.1)*view.zoom+love.graphics.getHeight()/2, 0, player.direction * view.zoom/32, view.zoom/32, 34, 103)
+ end
-- Can't remember what this was for:
--love.graphics.line((player.x-view.x)*view.zoom + love.graphics.getWidth()/2, (player.y-view.y-1.5)*view.zoom+love.graphics.getHeight()/2, (cursor.x-view.x)*view.zoom + love.graphics.getWidth()/2, (cursor.y-view.y)*view.zoom+love.graphics.getHeight()/2)
@@ -224,10 +249,10 @@ end
function love.mousepressed(x, y, button)
if button == "wd" then
selected = selected + 1
- if selected == 4 then selected = 1 end
+ if selected == 5 then selected = 1 end
elseif button == "wu" then
selected = selected - 1
- if selected == 0 then selected = 3 end
+ if selected == 0 then selected = 4 end
end
end
View
15 player.lua
@@ -8,14 +8,23 @@ function Player:new(seed)
o.x = 0.5
o.y = -40
o.height = 1.8
- o.width = 0.8
+ o.width = 0.7
o.falling = true
o.againstRightWall = false
o.againstLeftWall = false
o.vx = 0
o.vy = 0
- o.image = love.graphics.newImage("gfx/rabbitStand.png")
- o.image:setFilter("linear", "nearest")
+ o.stand = love.graphics.newImage("gfx/rabbitStand.png")
+ o.stand:setFilter("linear", "nearest")
+ o.jump1 = love.graphics.newImage("gfx/rabbitJump1.png")
+ o.jump1:setFilter("linear", "nearest")
+ o.jump2 = love.graphics.newImage("gfx/rabbitJump2.png")
+ o.jump2:setFilter("linear", "nearest")
+ o.land = love.graphics.newImage("gfx/rabbitLand.png")
+ o.land:setFilter("linear", "nearest")
+ o.walk = newAnimation(love.graphics.newImage("gfx/rabbitWalk.png"), 64, 128, 0.05, 11)
+ o.walk:play()
+ o.walking = false
o.inventory = {}
o.oldX = o.x
o.oldY = o.y

0 comments on commit 5645448

Please sign in to comment.
Something went wrong with that request. Please try again.