Somehow the code seems not to work. The jump isn't performed at all. Where did i go wrong?
Code: Select all
HC = require"libs"
--function to deal with collides (duh)
function on_collide (dt, shape_a, shape_b)
ground_touched = false
--Who is who?
local ground
if shape_a == player then
ground = shape_b
ground_touched = true
else ground = shape_a
end
local bx, by = player:center()
local px,py = ground:center()
player.velocity.y = 0
player.velocity.x = 0 --not sure if that might interfere :/
end
function love.load()
Collider = HC(100, on_collide)
-- credit to tentus
player_box = {
x = 400,
y = 0,
w = 25,
h = 75,
v = false -- not just 0 thrust, but NO thrust at all
}
ground_box = {
x = 0,
y = 535,
w = 800,
h = 20
}
initial = -200 -- start with an upward for of 200 px per second
falloff = -50 -- we lose 50 px of upward thrust per second
player = Collider:addRectangle(player_box.x,player_box.y, player_box.w, player_box.h)
ground = Collider:addRectangle(ground_box.x, ground_box.y, ground_box.w, ground_box.h)
player.velocity = {x=0, y=300}
Tileset = love.graphics.newImage('images/foresttiles01.png')
TileW, TileH = 60,60
local tilesetW, tilesetH = Tileset:getWidth(),Tileset:getHeight()
love.graphics.setBackgroundColor( 54, 54, 54)
local quadInfo = {
{ 'a', 0, 300 }, -- air
{ '#', 60, 0 }, -- grass
{ '*', 60, 120 }, -- flowers
{ '^', 120, 120 }, -- other flowers
{ 'g', 0, 0 } --other grass
}
Quads={}
for _,info in ipairs(quadInfo) do
-- info[1] = character, info[2]= x, info[3] = y
Quads[info[1]] = love.graphics.newQuad(info[2], info[3], TileW, TileH, tilesetW, tilesetH)
end
--Map Tile Layout
local tileString = [[
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
*^^***^^**^^**
#g#g#g#g#g#g#g
]]
-- Translate the characters into the right tile parts of the Tileset
TileTable = {}
local width = #(tileString:match("[^\n]+"))
for x = 1,width,1 do TileTable[x] = {} end
local rowIndex,columnIndex = 1,1
for row in tileString:gmatch("[^\n]+") do
assert(#row == width, 'Map is not aligned: width of row ' .. tostring(rowIndex) .. ' should be ' .. tostring(width) .. ', but it is ' .. tostring(#row))
columnIndex = 1
for character in row:gmatch(".") do
TileTable[columnIndex][rowIndex] = character
columnIndex = columnIndex + 1
end
rowIndex=rowIndex+1
end
end
function love.update(dt)
xc = love.mouse.getX( )
yc = love.mouse.getY( )
player:move(player.velocity.x * dt, player.velocity.y *dt)
if player_box.v then --movement per update
player_box.y = player_box.v - (falloff * dt)
player_box.v = player_box.y + (player_box.v * dt)
end
if player_box.y + player_box.h >= player_box.y then -- stop the box when touching ground
player_box.y = player_box.y - player_box.h
player_box.v = false
end
if love.keyboard.isDown('w') then
--player:move(0, -100 * dt)
if not player.v then -- can't jump if we're already jumping
player.v = initial
end
ground_touched = false
elseif love.keyboard.isDown('s') and ground_touched == false then
player:move(0, 100 * dt)
elseif love.keyboard.isDown('a') then
player:move(-10, 0 * dt)
ground_touched = false
elseif love.keyboard.isDown('d') then
player:move(10, 0 * dt)
ground_touched = false
end
Collider:update(dt)
end
function love.draw()
--Hadron Collider
player:draw('fill')
ground:draw('fill')
local lg = love.graphics
lg.print("FPS: "..love.timer.getFPS(), 8,8)
lg.print("Mouse X:" ..xc, 10, 18)
lg.print("Mouse Y" ..yc, 10, 28)
for columnIndex,column in ipairs(TileTable) do
for rowIndex,char in ipairs(column) do
local x,y = (columnIndex-1)*TileW, (rowIndex-1)*TileH
love.graphics.drawq(Tileset, Quads[char], x, y)
end
end
local lg = love.graphics
lg.setColor(255,255,255, 255)
end