practice/love/tommy/main.lua

145 lines
3.6 KiB
Lua

require "Tserial"
function move(accel, axis, direction)
-- with acceleration
if accel and (direction == "right" or direction == "down")then
if axis < player.speed then
axis = axis + (player.speed*player.accel)
end
--note the NEGATIVE player.speed
elseif accel and (direction == "left" or direction == "up") then
if axis > -player.speed then
axis = axis - (player.speed*player.accel)
end
elseif not accel and (direction == "right" or direction == "down") then
if axis > 0 then
axis = axis - (player.speed*player.accel)
end
elseif not accel and (direction == "left" or direction == "up") then
if axis < 0 then
axis = axis + (player.speed*player.accel)
end
end
return axis
end
function love.load()
love.filesystem.setIdentity("tommy")
saveFile = "test.lua"
-- Check if Save file exists
save_exists = love.filesystem.exists(saveFile)
player = {}
player.x = 100
player.y = 100
player.w = 25
player.h = 25
player.speed = 500
player.current_speed = {}
player.current_speed.left = 0
player.current_speed.right = 0
player.current_speed.up = 0
player.current_speed.down = 0
player.current_speed.x = 0
player.current_speed.y = 0
player.kill_engines = false
--Must be less than 1
--if I understand correctly, the second number should be how many frames
--it takes to get to top speed
player.accel = 1/120
end
function love.keyreleased(key)
if key == "q" then
love.event.quit()
end
-- save
if key == "s" then
love.filesystem.write(saveFile, Tserial.pack(player, false, true))
end
-- Load
if key == "l" then
if save_exists then
player = Tserial.unpack( love.filesystem.read( saveFile ) )
end
end
if key == "z" then
if player.kill_engines == false then
player.kill_engines = true
else
player.kill_engines = false
end
end
end
function love.update(dt)
--Move character left or right
if love.keyboard.isDown("right") then
player.kill_engines = false
player.current_speed.right = move(true, player.current_speed.right, "right")
print ("RIGHT ".. player.current_speed.right)
elseif love.keyboard.isDown("left") then
player.kill_engines = false
player.current_speed.left = move(true, player.current_speed.left, "left")
print ("LEFT ".. player.current_speed.left)
-- player.x = player.x - player.current_speed.left*dt
end
player.x = player.x + (player.current_speed.right + player.current_speed.left)*dt
--Also able to move or up down at the same time as one of the above
if love.keyboard.isDown("up") then
player.kill_engines = false
player.current_speed.up = move(true, player.current_speed.up, "up")
elseif love.keyboard.isDown ("down") then
player.killengines = false
player.current_speed.down = move(true, player.current_speed.down, "down")
end
player.y = player.y + (player.current_speed.up + player.current_speed.down)*dt
-- Decel
if not player.kill_engines then
if not love.keyboard.isDown("up") then
player.current_speed.up = move(false, player.current_speed.up, "up")
end
if not love.keyboard.isDown("down") then
player.current_speed.down = move(false, player.current_speed.down, "down")
end
if not love.keyboard.isDown("right") then
player.current_speed.right = move(false, player.current_speed.right, "right")
end
if not love.keyboard.isDown("left") then
player.current_speed.left = move(false, player.current_speed.left, "left")
end
end
end
function love.draw()
--draw the rectangle. Fill/line for the type,
--(x,y, width, height)
love.graphics.rectangle("line", player.x, player.y, player.w, player.h)
end