From 8a51c55cfd5c22ff035f320c9128d6ec49009bf2 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 15 May 2016 22:13:13 +0200 Subject: ZU WARUDO! --- camera.lua | 3 +- cloud.lua | 24 ++++++++++++-- main.lua | 108 ++++++++++++++++--------------------------------------------- player.lua | 3 +- world.lua | 84 ++++++++++++++++++++++++++++++++++++++++------- 5 files changed, 126 insertions(+), 96 deletions(-) diff --git a/camera.lua b/camera.lua index 8313dac..79e680b 100644 --- a/camera.lua +++ b/camera.lua @@ -33,7 +33,8 @@ end -- Move follow function Camera:moveFollow () local x,y,i = 105, 120, 1 - for k,point in pairs(Nauts) do + -- w.Nauts [!] temporary + for k,point in pairs(w.Nauts) do i = i + 1 x = math.max(math.min(point.body:getX(),290),0) + x y = math.max(math.min(point.body:getY(),180),20) + y diff --git a/cloud.lua b/cloud.lua index bb48eb6..04ee31b 100644 --- a/cloud.lua +++ b/cloud.lua @@ -7,15 +7,33 @@ Cloud = { x = 0, y = 0, - sprite = love.graphics.newImage("assets/clouds.png") + t = 1, -- Type of the cloud (quad number) + sprite = love.graphics.newImage("assets/clouds.png"), + quads = { + [1] = love.graphics.newQuad( 1,159, 158,47, 480,49), + [2] = love.graphics.newQuad(161,319, 158,47, 480,49), + [3] = love.graphics.newQuad(321,479, 158,47, 480,49) + } } -- Constructor of `Cloud` -function Cloud:new() +function Cloud:new(x, y, t) -- Meta local o = {} setmetatable(o, self) self.__index = self - -- Misc + -- Init + o.x = x or self.x + o.y = y or self.y + o.t = t or self.t return o +end + +-- Update of `Cloud`, returns x for world to delete cloud after reaching right corner +function Cloud:update(dt) + return x +end + +-- Draw `Cloud` +function Cloud:draw() end \ No newline at end of file diff --git a/main.lua b/main.lua index 7e3d71e..c450734 100644 --- a/main.lua +++ b/main.lua @@ -1,112 +1,60 @@ -- "NOTNAUTS" -- WHOLE CODE HAS FLAG OF "need a cleanup" +require "world" require "ground" require "player" require "camera" +require "cloud" +-- Temporary debug debug = false +-- Load function love.load () -- Graphics love.graphics.setBackgroundColor(189, 95, 93) love.graphics.setDefaultFilter("nearest", "nearest") - -- World physics - love.physics.setMeter(64) - world = love.physics.newWorld(0, 9.81*64, true) - world:setCallbacks(beginContact, endContact) - - -- Platforms (`Ground`) - Platforms = {} - table.insert(Platforms, Ground:new(world, 290/2, 180/2, {-91,1, 90,1, 90,10, 5,76, -5,76, -91,10}, "assets/platform_big.png")) - table.insert(Platforms, Ground:new(world, 290/2+140, 180/2+50, {-26,1, 26,1, 26,30, -26,30}, "assets/platform_small.png")) - table.insert(Platforms, Ground:new(world, 290/2-140, 180/2+50, {-26,1, 26,1, 26,30, -26,30}, "assets/platform_small.png")) - table.insert(Platforms, Ground:new(world, 290/2, 180/2-50, {-17,1, 17,1, 17,17, -17,17}, "assets/platform_top.png")) - - -- Nauts (`Player`) - Nauts = {} - table.insert(Nauts, Player:new(world, 290/2-10, 180/2 - 80, "assets/leon.png")) - table.insert(Nauts, Player:new(world, 290/2+10, 180/2 - 80, "assets/lonestar.png")) - + -- ZU WARUDO! + w = World:new() + w:createPlatform(290/2, 180/2, {-91,1, 90,1, 90,10, 5,76, -5,76, -91,10}, "assets/platform_big.png") + w:createPlatform(290/2+140, 180/2+50, {-26,1, 26,1, 26,30, -26,30}, "assets/platform_small.png") + w:createPlatform(290/2-140, 180/2+50, {-26,1, 26,1, 26,30, -26,30}, "assets/platform_small.png") + w:createPlatform(290/2, 180/2-50, {-17,1, 17,1, 17,17, -17,17}, "assets/platform_top.png") + w:createNaut(290/2-10, 180/2 - 80, "assets/leon.png") + w:createNaut(290/2+10, 180/2 - 80, "assets/lonestar.png") + -- Temporary settings for second player - Nauts[2].name = "Player2" - Nauts[2].key_left = "a" - Nauts[2].key_right = "d" - Nauts[2].key_up = "w" - Nauts[2].key_down = "s" - Nauts[2].key_jump = "h" - Nauts[2].key_hit = "g" - - -- Camera - camera = Camera:new() + w.Nauts[2].name = "Player2" + w.Nauts[2].key_left = "a" + w.Nauts[2].key_right = "d" + w.Nauts[2].key_up = "w" + w.Nauts[2].key_down = "s" + w.Nauts[2].key_jump = "h" + w.Nauts[2].key_hit = "g" end +-- Update function love.update (dt) - -- Put world in motion! - world:update(dt) - camera:moveFollow() - -- Players - for k,naut in pairs(Nauts) do - naut:update(dt) - end + w:update(dt) end +-- KeyPressed function love.keypressed (key) + w:keypressed(key) -- Switch hitbox display on/off if key == "x" then debug = not debug end - -- - if key == "z" then - camera.scale = (camera.scale % 4) + 1 - end - -- Players - for k,naut in pairs(Nauts) do - naut:keypressed(key) - end end +-- KeyReleased function love.keyreleased(key) - -- Players - for k,naut in pairs(Nauts) do - naut:keyreleased(key) - end + w:keyreleased(key) end +-- Draw function love.draw () - -- Draw SOME background - -- I'm already bored with solid color! - love.graphics.setColor(193, 100, 99, 255) - love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()*0.25) - love.graphics.setColor(179, 82, 80, 255) - love.graphics.rectangle("fill", 0, love.graphics.getHeight()*0.8, love.graphics.getWidth(), love.graphics.getHeight()*0.2) - - -- Get camera (like this, for now ;_; pass camera object to draw function?) - local offset_x, offset_y = camera:getOffsets() - local scale = camera.scale - - -- Draw ground - for k,platform in pairs(Platforms) do - platform:draw(offset_x, offset_y, scale, debug) - end - - -- Draw player - for k,naut in pairs(Nauts) do - naut:draw(offset_x, offset_y, scale, debug) - end -end - -function beginContact (a, b, coll) - local x,y = coll:getNormal() - if y == -1 then - print(b:getUserData().name .. " is not in air") - b:getUserData().inAir = false - b:getUserData().jumpdouble = true - end -end - -function endContact (a, b, coll) - print(b:getUserData().name .. " is in air") - b:getUserData().inAir = true + w:draw() end \ No newline at end of file diff --git a/player.lua b/player.lua index 099001f..c40f4fe 100644 --- a/player.lua +++ b/player.lua @@ -224,7 +224,8 @@ function Player:hit (horizontal, vertical) self:changeAnimation("attack") self.body:applyLinearImpulse(10*self.facing, 0) end - for k,n in pairs(Nauts) do + -- w.Nauts [!] temporary + for k,n in pairs(w.Nauts) do if n ~= self then local didHit = false if n.fixture:testPoint(self.body:getX()+12*horizontal,self.body:getY()-2) then diff --git a/world.lua b/world.lua index e4301e4..963ae44 100644 --- a/world.lua +++ b/world.lua @@ -7,10 +7,10 @@ -- nils initialized in constructor World = { world = nil, - nauts = nil, - platforms = nil, - clouds = nil, - camera = nil -- not sure yet? menu will need scaling too + Nauts = nil, + Platforms = nil, + Clouds = nil, + camera = nil } -- Constructor of `World` ZA WARUDO! @@ -25,30 +25,92 @@ function World:new() o.world:setCallbacks(o.beginContact, o.endContact) -- Empty tables for objects local c = {} - o.clouds = c + o.Clouds = c local n = {} - o.nauts = n + o.Nauts = n local p = {} - o.platforms = {} + o.Platforms = {} + -- Create camera + o.camera = Camera:new() return o end -- Add new platform to the world function World:createPlatform(x, y, polygon, sprite) - table.insert(self.platforms, Ground:new(self.world, x, y, polygon, sprite)) + table.insert(self.Platforms, Ground:new(self.world, x, y, polygon, sprite)) end -- Add new naut to the world function World:createNaut(x, y, sprite) - table.insert(self.nauts, Player:new(self.world, x, y, sprite)) + table.insert(self.Nauts, Player:new(self.world, x, y, sprite)) end -- Add new cloud to the world +function World:createCloud(x, y, t) + table.insert(self.Clouds, Cloud:new(x, y, t)) +end + +-- Update ZU WARUDO +function World:update(dt) + -- Physical world + self.world:update(dt) + -- Camera + self.camera:moveFollow() + -- Nauts + for _,naut in pairs(self.Nauts) do + naut:update(dt) + end +end --- Update -- Keypressed +function World:keypressed(key) + for _,naut in pairs(self.Nauts) do + naut:keypressed(key) + end +end + -- Keyreleased +function World:keyreleased(key) + for _,naut in pairs(self.Nauts) do + naut:keyreleased(key) + end +end + -- Draw +function World:draw() + -- Hard-coded background (for now) + love.graphics.setColor(193, 100, 99, 255) + love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()*0.25) + love.graphics.setColor(179, 82, 80, 255) + love.graphics.rectangle("fill", 0, love.graphics.getHeight()*0.8, love.graphics.getWidth(), love.graphics.getHeight()*0.2) + + -- Camera stuff + local offset_x, offset_y = self.camera:getOffsets() + local scale = self.camera.scale + + -- Draw ground + for _,platform in pairs(self.Platforms) do + platform:draw(offset_x, offset_y, scale, false) + end + + -- Draw player + for _,naut in pairs(self.Nauts) do + naut:draw(offset_x, offset_y, scale, false) + end +end -- beginContact --- endContact \ No newline at end of file +function World.beginContact(a, b, coll) + local x,y = coll:getNormal() + if y == -1 then + print(b:getUserData().name .. " is not in air") + b:getUserData().inAir = false + b:getUserData().jumpdouble = true + end +end + +-- endContact +function World.endContact(a, b, coll) + print(b:getUserData().name .. " is in air") + b:getUserData().inAir = true +end \ No newline at end of file -- cgit v1.1