Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Smooth out the offsets, and now he can fall off the ground.

  • Loading branch information...
commit 6eb0440478ccb491280b800cf0d4b93dfde6b5a8 1 parent 4fc934a
Ben Matasar authored
Showing with 43 additions and 15 deletions.
  1. +18 −4 main.lua
  2. +9 −5 src/ground.lua
  3. +16 −6 src/player.lua
22 main.lua
View
@@ -26,6 +26,7 @@ function love.load()
player = Player.create(300, 485)
ground = Ground.create()
+ ground:isOnGround(player)
love.graphics.setBackgroundColor(skyBlue)
end
@@ -46,14 +47,27 @@ function keyDownForAction(action)
return false
end
+function withColor(color, fn)
+ local r,g,b,a = love.graphics.getColor()
+ love.graphics.setColor(color)
+ fn()
+ love.graphics.setColor(r,g,b,a)
+end
+
+function updateWorldOffset(newOffset)
+ worldOffset = math.min(0,newOffset)
+end
+
function love.draw()
ground:draw()
player:draw()
local yellow = {204, 255, 51, 255}
- local r,g,b,a = love.graphics.getColor()
- love.graphics.setColor(yellow)
- love.graphics.arc("fill", 0, 0, 100, 0, math.pi / 2, 20)
- love.graphics.setColor(r,g,b,a)
+ withColor(yellow, function()
+ love.graphics.arc("fill", 0, 0, 100, 0, math.pi / 2, 20)
+ end)
+ withColor({0,0,0}, function()
+ love.graphics.print("FPS: " .. love.timer.getFPS(), 750, 0)
+ end)
end
function love.update(dt)
14 src/ground.lua
View
@@ -4,8 +4,8 @@ Ground.__index = Ground
function Ground.create()
local instance = {
image = love.graphics.newImage("planetcute/Grass Block.png"),
- height = 171,
- width = 101
+ imageHeight = 171,
+ imageWidth = 101
}
setmetatable(instance, Ground)
return instance
@@ -14,16 +14,20 @@ end
function Ground:draw()
for i = 1,12 do
local xPos = ((i - 1) * 100) + worldOffset
- love.graphics.draw(self.image, xPos, self:getHeight())
+ love.graphics.draw(self.image, xPos, self:getHeight() - 100)
end
local x,y,width,height = self:getBox()
love.graphics.rectangle("line", x,y,width,height)
end
function Ground:getHeight()
- return love.graphics.getHeight() - self.height
+ return love.graphics.getHeight() - self.imageHeight + 100
end
function Ground:getBox()
- return 0, 0, love.graphics.getWidth(), self:getHeight() + 115
+ return 0, self:getHeight(), 1200 + worldOffset, love.graphics.getHeight()
+end
+
+function Ground:isOnGround(player)
+ return player.x - worldOffset < 1200
end
22 src/player.lua
View
@@ -24,11 +24,11 @@ end
function Player:draw()
love.graphics.draw(self.image, self.x, self.y, self.orientation, 1, 1, 50, 85)
- local x,y,width,height = self:getHitBox()
+ local x,y,width,height = self:getBox()
love.graphics.rectangle("line", x,y,width,height)
end
-function Player:getHitBox()
+function Player:getBox()
return self.x - 40, self.y - 30, 80, 85
end
@@ -64,11 +64,13 @@ function Player:update(dt)
if self:isJumping() then
self.y = self.y - self.upVelocity * dt
self.upVelocity = self.upVelocity - (self.gravity * dt)
- if self.y > 485 then
+ if self.y > 485 and ground:isOnGround(self) then
self.upVelocity = 0
self.upAcceleration = 0
self.y = 485
end
+ elseif not ground:isOnGround(self) then
+ self.upVelocity = self.upVelocity - (self.gravity * dt)
end
if self:isMovingLeft() then
@@ -76,7 +78,7 @@ function Player:update(dt)
self.x = self.x - dt * self.xVelocity
self.decelDirection = -1
if self.x < playerMinX then
- worldOffset = worldOffset + dt * self.xVelocity
+ updateWorldOffset(worldOffset + dt * self.xVelocity)
self.x = playerMinX
end
self.orientation = -1 * math.pi / 20
@@ -84,14 +86,22 @@ function Player:update(dt)
self.xVelocity = math.min(self.xVelocity + (dt * self.xAcceleration), self.maxXVelocity)
self.x = self.x + dt * self.xVelocity
if self.x > playerMaxX then
- worldOffset = worldOffset - dt * self.xVelocity
+ updateWorldOffset(worldOffset - dt * self.xVelocity)
self.x = playerMaxX
end
self.decelDirection = 1
self.orientation = math.pi / 20
else -- decel
self.xVelocity = math.max(math.abs(self.xVelocity) - (dt * self.xDeceleration), 0) * self.decelDirection
- self.x = self.x + dt * self.xVelocity
+ if self.x >= playerMaxX then
+ updateWorldOffset(worldOffset - dt * self.xVelocity)
+ self.x = playerMaxX
+ elseif self.x <= playerMinX then
+ updateWorldOffset(worldOffset - dt * self.xVelocity)
+ self.x = playerMinX
+ else
+ self.x = self.x + dt * self.xVelocity
+ end
self.orientation = 0
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.