semi-working game
This commit is contained in:
parent
022858330a
commit
1a0e6ec71e
23
main.lua
23
main.lua
@ -1,21 +1,25 @@
|
||||
--load peachy
|
||||
peachy = require("libs/peachy")
|
||||
|
||||
--load globals
|
||||
local globals = require("modules/globals")
|
||||
game = globals.game
|
||||
|
||||
--load player
|
||||
player = require("modules/player")
|
||||
|
||||
--load hump camera
|
||||
Camera = require("libs/hump/camera")
|
||||
camera = Camera(player.x, player.y)
|
||||
--load floors
|
||||
floors = require("modules/floors")
|
||||
|
||||
--load content_loader
|
||||
local content_loader = require("modules/content_loader")
|
||||
spritesheets = content_loader.spritesheets
|
||||
sound_effects = content_loader.sound_effects
|
||||
|
||||
--load globals
|
||||
local globals = require("modules/globals")
|
||||
local game = globals.game
|
||||
player:init(sound_effects, spritesheets, game)
|
||||
--load hump camera
|
||||
Camera = require("libs/hump/camera")
|
||||
camera = Camera(player.object.body:getX(), player.object.body:getY())
|
||||
|
||||
--load 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 updateinit = update_mod.updateinit
|
||||
|
||||
drawerinit(spritesheets, player, game, camera)
|
||||
updateinit(game, camera)
|
||||
player:init(sound_effects, spritesheets, game)
|
||||
floors:addFloor("Bottom", game, 0, (game:getcurlevel().height /4) - 160, game:getcurlevel().width, 160, 255, 255, 255, 255)
|
||||
|
||||
drawerinit(spritesheets, player, game, camera, floors)
|
||||
updateinit(game, camera, floors)
|
||||
|
||||
--play music for level
|
||||
game:getcurlevel().music:play()
|
||||
|
@ -2,32 +2,40 @@ local spritesheets = {}
|
||||
local player = {}
|
||||
local game = {}
|
||||
local camera = {}
|
||||
local floors = {}
|
||||
|
||||
function drawerinit(spritesheetst, playert, gamex, camerax)
|
||||
function drawerinit(spritesheetst, playert, gamex, camerax, floorsx)
|
||||
spritesheets = spritesheetst
|
||||
player = playert
|
||||
game = gamex
|
||||
camera = camerax
|
||||
floors = floorsx
|
||||
end
|
||||
|
||||
local function draw()
|
||||
camera:attach()
|
||||
--draw rectangle with bottom at 980y and top at 850y which stretches acress the entire modules/player
|
||||
--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
|
||||
player.animation:draw(player.x, player.y)
|
||||
player.animation:draw(player.object.body:getX(), player.object.body:getY())
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
spritesheets["player_die"]:draw(player.x, player.y)
|
||||
spritesheets["player_die"]:draw(player.object.body:getX(), player.object.body:getY())
|
||||
else
|
||||
spritesheets["player_default"]:draw(player.x, player.y)
|
||||
spritesheets["player_default"]:draw(player.object.body:getX(), player.object.body:getY())
|
||||
end
|
||||
camera:detach()
|
||||
end
|
||||
|
28
modules/floors.lua
Normal file
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_count = 1
|
||||
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)
|
||||
return self.levels[self.level]
|
||||
end
|
||||
|
||||
for i = 1, game.level_count, 1 do
|
||||
game.levels[i] = {}
|
||||
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].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:setLooping(true)
|
||||
end
|
||||
|
||||
|
||||
return {
|
||||
game = game
|
||||
}
|
@ -1,51 +1,70 @@
|
||||
local player = {}
|
||||
player.x = 0
|
||||
player.y = 500
|
||||
player.width = 140
|
||||
player.height = 160
|
||||
player.speed = 200
|
||||
player.speed_jumping = 400
|
||||
player.speed_walking = 200
|
||||
player.jump_height = 500
|
||||
player.speed = 320000
|
||||
player.speed_jumping = 640000
|
||||
player.speed_walking = 320000
|
||||
player.jump_height = 400
|
||||
player.state = "idle"
|
||||
player.direction = "right"
|
||||
player.alive = true
|
||||
player.object = {}
|
||||
player.init = function(self, sound_effects, spritesheets, game)
|
||||
self.sound_effects = sound_effects
|
||||
self.spritesheets = spritesheets
|
||||
self.game = game
|
||||
self.x = self.game.width / 2 + 8
|
||||
self.y = self.game.height + 8
|
||||
player.animation = spritesheets["player_idle"]
|
||||
self.animation = spritesheets["player_idle"]
|
||||
self.object.body = love.physics.newBody(game.world, player.width, player.height, "dynamic")
|
||||
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
|
||||
|
||||
player.walk_right = function(self, dt)
|
||||
if self.x < self.game:getcurlevel().width - self.width - self.game.width - 8 and self.alive then
|
||||
self.x = self.x + self.speed * dt
|
||||
if self.alive then
|
||||
self.object.body:applyForce(self.speed * dt, 0)
|
||||
self.state = "walk_right"
|
||||
self.direction = "right"
|
||||
self.animation = self.spritesheets["player_walk_right"]
|
||||
end
|
||||
end
|
||||
player.walk_left = function(self, dt)
|
||||
if self.x > self.game.width / 2 + 8 and self.alive then
|
||||
self.x = self.x - self.speed * dt
|
||||
if self.alive then
|
||||
self.object.body:applyForce(-(self.speed * dt), 0)
|
||||
self.state = "walk_left"
|
||||
self.direction = "left"
|
||||
self.animation = self.spritesheets["player_walk_left"]
|
||||
end
|
||||
end
|
||||
player.jump = function(self, dt)
|
||||
if self.y > self.game.height / 2 + 8 and self.alive then
|
||||
self.y = self.y - self.jump_height * dt
|
||||
--check if alive and if on floor
|
||||
if self.alive then
|
||||
--check all contacts
|
||||
for i,v in pairs(self.object.body:getContacts()) do
|
||||
--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
|
||||
player.down = function(self, dt)
|
||||
if self.y < self.game:getcurlevel().height - self.height - self.game.height / 2 - 8 and self.alive then
|
||||
self.y = self.y + self.jump_height * dt
|
||||
if self.alive then
|
||||
self.object.body:applyForce(0, self.jump_height * dt)
|
||||
self.state = "jump"
|
||||
self.animation = self.spritesheets["player_jump"]
|
||||
self.sound_effects["jump"]:play()
|
||||
@ -82,4 +101,17 @@ player.default = function(self, dt)
|
||||
self.animation = self.spritesheets["player_default"]
|
||||
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
|
@ -1,11 +1,14 @@
|
||||
local game = {}
|
||||
local camera = {}
|
||||
local function updateinit(gamex, camerax)
|
||||
local floors = {}
|
||||
local function updateinit(gamex, camerax, floorsx)
|
||||
game = gamex
|
||||
camera = camerax
|
||||
floors = floorsx
|
||||
end
|
||||
|
||||
local function update(dt)
|
||||
game.world:update(dt)
|
||||
--update player_animation
|
||||
if spritesheets["player_die"]:getFrame() ~= 16 then
|
||||
spritesheets["player_die"]:update(dt)
|
||||
@ -51,10 +54,23 @@ local function update(dt)
|
||||
player:revive(dt)
|
||||
end
|
||||
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
|
||||
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)
|
||||
end
|
||||
return {
|
||||
|
Loading…
Reference in New Issue
Block a user