semi-working game

This commit is contained in:
Bruno Rybársky 2022-03-03 13:13:17 +01:00
parent 022858330a
commit 1a0e6ec71e
6 changed files with 139 additions and 42 deletions

@ -1,21 +1,25 @@
--load peachy --load peachy
peachy = require("libs/peachy") peachy = require("libs/peachy")
--load globals
local globals = require("modules/globals")
game = globals.game
--load player --load player
player = require("modules/player") player = require("modules/player")
--load hump camera --load floors
Camera = require("libs/hump/camera") floors = require("modules/floors")
camera = Camera(player.x, player.y)
--load content_loader --load content_loader
local content_loader = require("modules/content_loader") local content_loader = require("modules/content_loader")
spritesheets = content_loader.spritesheets spritesheets = content_loader.spritesheets
sound_effects = content_loader.sound_effects sound_effects = content_loader.sound_effects
--load globals player:init(sound_effects, spritesheets, game)
local globals = require("modules/globals") --load hump camera
local game = globals.game Camera = require("libs/hump/camera")
camera = Camera(player.object.body:getX(), player.object.body:getY())
--load draw_callback --load draw_callback
draw_mod = require("modules/draw_callback") draw_mod = require("modules/draw_callback")
@ -27,9 +31,10 @@ update_mod = require("modules/update_callback")
local update_callback = update_mod.update local update_callback = update_mod.update
local updateinit = update_mod.updateinit local updateinit = update_mod.updateinit
drawerinit(spritesheets, player, game, camera) floors:addFloor("Bottom", game, 0, (game:getcurlevel().height /4) - 160, game:getcurlevel().width, 160, 255, 255, 255, 255)
updateinit(game, camera)
player:init(sound_effects, spritesheets, game) drawerinit(spritesheets, player, game, camera, floors)
updateinit(game, camera, floors)
--play music for level --play music for level
game:getcurlevel().music:play() game:getcurlevel().music:play()
@ -39,5 +44,5 @@ function love.draw()
end end
function love.update(dt) function love.update(dt)
update_callback(dt) update_callback(dt)
end end

@ -2,32 +2,40 @@ local spritesheets = {}
local player = {} local player = {}
local game = {} local game = {}
local camera = {} local camera = {}
local floors = {}
function drawerinit(spritesheetst, playert, gamex, camerax) function drawerinit(spritesheetst, playert, gamex, camerax, floorsx)
spritesheets = spritesheetst spritesheets = spritesheetst
player = playert player = playert
game = gamex game = gamex
camera = camerax camera = camerax
floors = floorsx
end end
local function draw() local function draw()
camera:attach() camera:attach()
--draw rectangle with bottom at 980y and top at 850y which stretches acress the entire modules/player
--draw background --draw background
love.graphics.draw(game.levels[game.level].background, 0, 0) love.graphics.draw(game:getcurlevel().background, 0, -(game:getcurlevel().height) + 280)
--draw floors
for i = 1, #floors, 1 do
love.graphics.setColor(love.math.colorFromBytes(floors[i].color[1], floors[i].color[2], floors[i].color[3], floors[i].color[4]))
love.graphics.rectangle("fill", floors[i].x, floors[i].y, floors[i].width, floors[i].height)
end
--draw player_animation --draw player_animation
player.animation:draw(player.x, player.y) player.animation:draw(player.object.body:getX(), player.object.body:getY())
if player.state == "idle" then if player.state == "idle" then
spritesheets["player_idle"]:draw(player.x, player.y) spritesheets["player_idle"]:draw(player.object.body:getX(), player.object.body:getY())
elseif player.state == "walk_left" then elseif player.state == "walk_left" then
spritesheets["player_walk_left"]:draw(player.x, player.y) spritesheets["player_walk_left"]:draw(player.object.body:getX(), player.object.body:getY())
elseif player.state == "walk_right" then elseif player.state == "walk_right" then
spritesheets["player_walk_right"]:draw(player.x, player.y) spritesheets["player_walk_right"]:draw(player.object.body:getX(), player.object.body:getY())
elseif player.state == "jump" then elseif player.state == "jump" then
spritesheets["player_jump"]:draw(player.x, player.y) spritesheets["player_jump"]:draw(player.object.body:getX(), player.object.body:getY())
elseif player.state == "die" then elseif player.state == "die" then
spritesheets["player_die"]:draw(player.x, player.y) spritesheets["player_die"]:draw(player.object.body:getX(), player.object.body:getY())
else else
spritesheets["player_default"]:draw(player.x, player.y) spritesheets["player_default"]:draw(player.object.body:getX(), player.object.body:getY())
end end
camera:detach() camera:detach()
end end

28
modules/floors.lua Normal file

@ -0,0 +1,28 @@
local floors = {}
floors.addFloor = function(self, name, game, x, y, w, h, r, g, b, a)
local floor = {}
floor.name = name
floor.x = x
floor.y = y
floor.width = w
floor.height = h
floor.userdata = "floor"
floor.color = {r, g, b, a}
floor.body = love.physics.newBody(game.world, x + w / 2, y, "static")
floor.shape = love.physics.newRectangleShape(w, h)
floor.fixture = love.physics.newFixture(floor.body, floor.shape)
floor.fixture:setUserData(floor.userdata)
table.insert(self, floor)
game.objects.floors = self.floors
end
floors.listFloors = function(self)
allstr = ""
for i,v in pairs(self) do
--if v is not a function
if type(v) ~= "function" then
allstr = allstr .. (v.name..": \""..v.body:getX().."\"; y: \""..v.body:getY().."\";;;")
end
end
return allstr
end
return floors

@ -8,18 +8,26 @@ game.fullscreen = false
game.level = 1 game.level = 1
game.level_count = 1 game.level_count = 1
game.levels = {} game.levels = {}
game.objects = {}
--set up physics
love.physics.setMeter(64)
game.world = love.physics.newWorld(0, 9.81*64, true)
game.getcurlevel = function(self) game.getcurlevel = function(self)
return self.levels[self.level] return self.levels[self.level]
end end
for i = 1, game.level_count, 1 do for i = 1, game.level_count, 1 do
game.levels[i] = {} game.levels[i] = {}
game.levels[i].background = love.graphics.newImage("assets/images/backgrounds/level".. i .."_hires.png") game.levels[i].background = love.graphics.newImage("assets/images/backgrounds/level".. i .."_hires.png")
game.levels[i].width = game.levels[i].background:getWidth() game.levels[i].width = game.levels[i].background:getWidth()
game.levels[i].height = game.levels[i].background:getHeight() game.levels[i].height = game.levels[i].background:getHeight()
--print level dimensions
print("Level " .. i .. " dimensions: " .. game.levels[i].width .. "x" .. game.levels[i].height)
game.levels[i].music = love.audio.newSource("assets/audio/music/track".. i ..".ogg", "static") game.levels[i].music = love.audio.newSource("assets/audio/music/track".. i ..".ogg", "static")
game.levels[i].music:setLooping(true) game.levels[i].music:setLooping(true)
end end
return { return {
game = game game = game
} }

@ -1,51 +1,70 @@
local player = {} local player = {}
player.x = 0
player.y = 500
player.width = 140 player.width = 140
player.height = 160 player.height = 160
player.speed = 200 player.speed = 320000
player.speed_jumping = 400 player.speed_jumping = 640000
player.speed_walking = 200 player.speed_walking = 320000
player.jump_height = 500 player.jump_height = 400
player.state = "idle" player.state = "idle"
player.direction = "right" player.direction = "right"
player.alive = true player.alive = true
player.object = {}
player.init = function(self, sound_effects, spritesheets, game) player.init = function(self, sound_effects, spritesheets, game)
self.sound_effects = sound_effects self.sound_effects = sound_effects
self.spritesheets = spritesheets self.spritesheets = spritesheets
self.game = game self.game = game
self.x = self.game.width / 2 + 8 self.animation = spritesheets["player_idle"]
self.y = self.game.height + 8 self.object.body = love.physics.newBody(game.world, player.width, player.height, "dynamic")
player.animation = spritesheets["player_idle"] self.object.shape = love.physics.newRectangleShape(player.width, player.height)
self.object.fixture = love.physics.newFixture(self.object.body, self.object.shape)
self.object.fixture:setUserData("player")
self.spawnx, self.spawny = game:getcurlevel().width / 2, game:getcurlevel().height / 4 - self.height * 2
self.object.body:setPosition(self.spawnx, self.spawny)
self.game.player = self
end end
player.walk_right = function(self, dt) player.walk_right = function(self, dt)
if self.x < self.game:getcurlevel().width - self.width - self.game.width - 8 and self.alive then if self.alive then
self.x = self.x + self.speed * dt self.object.body:applyForce(self.speed * dt, 0)
self.state = "walk_right" self.state = "walk_right"
self.direction = "right" self.direction = "right"
self.animation = self.spritesheets["player_walk_right"] self.animation = self.spritesheets["player_walk_right"]
end end
end end
player.walk_left = function(self, dt) player.walk_left = function(self, dt)
if self.x > self.game.width / 2 + 8 and self.alive then if self.alive then
self.x = self.x - self.speed * dt self.object.body:applyForce(-(self.speed * dt), 0)
self.state = "walk_left" self.state = "walk_left"
self.direction = "left" self.direction = "left"
self.animation = self.spritesheets["player_walk_left"] self.animation = self.spritesheets["player_walk_left"]
end end
end end
player.jump = function(self, dt) player.jump = function(self, dt)
if self.y > self.game.height / 2 + 8 and self.alive then --check if alive and if on floor
self.y = self.y - self.jump_height * dt if self.alive then
self.state = "jump" --check all contacts
self.animation = self.spritesheets["player_jump"] for i,v in pairs(self.object.body:getContacts()) do
self.sound_effects["jump"]:play() --get fixtures
local fixtureA, fixtureB = v:getFixtures( )
--get userdata
local userdataA = fixtureA:getUserData()
local userdataB = fixtureB:getUserData()
--check if on floor
if userdataA == "floor" or userdataB == "floor" then
--jump
self.object.body:applyLinearImpulse(0, -self.jump_height)
self.state = "jump"
self.animation = self.spritesheets["player_jump"]
self.sound_effects["jump"]:play()
break
end
end
end end
end end
player.down = function(self, dt) player.down = function(self, dt)
if self.y < self.game:getcurlevel().height - self.height - self.game.height / 2 - 8 and self.alive then if self.alive then
self.y = self.y + self.jump_height * dt self.object.body:applyForce(0, self.jump_height * dt)
self.state = "jump" self.state = "jump"
self.animation = self.spritesheets["player_jump"] self.animation = self.spritesheets["player_jump"]
self.sound_effects["jump"]:play() self.sound_effects["jump"]:play()
@ -82,4 +101,17 @@ player.default = function(self, dt)
self.animation = self.spritesheets["player_default"] self.animation = self.spritesheets["player_default"]
end end
end end
player.update = function(self, dt)
if self.alive then
--check if below map
if self.object.body:getY() > self.game:getcurlevel().height then
self:die()
--reset position, rotation and velocity
self.object.body:setPosition(self.spawnx, self.spawny)
self.object.body:setAngle(0)
self.object.body:setLinearVelocity(0, 0)
self:revive()
end
end
end
return player return player

@ -1,11 +1,14 @@
local game = {} local game = {}
local camera = {} local camera = {}
local function updateinit(gamex, camerax) local floors = {}
local function updateinit(gamex, camerax, floorsx)
game = gamex game = gamex
camera = camerax camera = camerax
floors = floorsx
end end
local function update(dt) local function update(dt)
game.world:update(dt)
--update player_animation --update player_animation
if spritesheets["player_die"]:getFrame() ~= 16 then if spritesheets["player_die"]:getFrame() ~= 16 then
spritesheets["player_die"]:update(dt) spritesheets["player_die"]:update(dt)
@ -51,10 +54,23 @@ local function update(dt)
player:revive(dt) player:revive(dt)
end end
if key("x") then if key("x") then
print("x: \""..player.x.."\"; y: \""..player.y.."\"") --print coordinates of all objects
local allstr = floors:listFloors()
allstr = allstr .. ("Player: \""..game.player.object.body:getX().."\"; y: \""..game.player.object.body:getY().."\";;;")
print(allstr)
end
if key("c") then
--reset player position
player.object.body:setX(player.spawnx)
player.object.body:setY(player.spawny)
--and velocity
player.object.body:setLinearVelocity(0, 0)
--and rotation
player.object.body:setAngle(0)
end end
end end
local dx,dy = (player.x - camera.x) / 2, (player.y - camera.y) / 2 player:update(dt)
local dx,dy = (player.object.body:getX() - camera.x) / 2, (player.object.body:getY() - camera.y) / 2
camera:move(dx, dy) camera:move(dx, dy)
end end
return { return {