Game window can be resized in the menu (and in the game, but the ground won't resize)

This commit is contained in:
Erkki Seppälä 2014-10-26 20:36:43 +02:00
parent d0cb00bfa3
commit d70107214a
6 changed files with 53 additions and 20 deletions

View file

@ -3,9 +3,9 @@ require 'settings'
function love.conf(t)
t.window.title = "Fysplane"
t.window.width = WIDTH
t.window.height = HEIGHT
t.window.resizable = false
t.window.width = INITIAL_WIDTH
t.window.height = INITIAL_HEIGHT
t.window.resizable = true
t.window.vsync = true
-- LÖVE version

View file

@ -1,5 +1,6 @@
Class = require 'hump.class'
require 'entities/rectangle'
require 'utils'
Ground = Class{
__includes = Rectangle,
@ -7,10 +8,15 @@ Ground = Class{
init = function(self, level)
local groundImg = love.graphics.newImage('resources/graphics/ground.png')
Rectangle.init(self,
(love.window.getWidth() - 1600) / 2, love.window.getHeight(),
-200, levelHeight(),
level, "static",
0, 1600, 20, 0, groundImg)
0,
levelWidth() + 400, 20, 0, groundImg)
self.fixture:setRestitution(0)
self.fixture:setFriction(0.5)
end;
update = function(self, dt)
self.body:setY(levelHeight() - 10)
end;
}

View file

@ -19,12 +19,8 @@ Level = Class{
self.world = love.physics.newWorld(GRAVITY_X, GRAVITY_Y, true)
-- Draw background to canvas so we don't redraw it every time
self.background = love.graphics.newImage('resources/graphics/sky.png')
self.bgCanvas = love.graphics.newCanvas()
love.graphics.setCanvas(self.bgCanvas)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(self.background)
love.graphics.setCanvas()
self.oldWidth, self.oldHeight = 0, 0
self:updateBackground()
self.makePlanes = { [1] = function()
return Plane(100, 400, INITIAL_PLANE_SPEED, 0,
@ -32,16 +28,33 @@ Level = Class{
self)
end,
[2] = function()
return Plane(WIDTH - 100 - 100, 300, -INITIAL_PLANE_SPEED, 0,
return Plane(levelWidth() - 100 - 100, 300, -INITIAL_PLANE_SPEED, 0,
0, 255, 0, -- green
self)
end }
self.planes = { [1] = self.makePlanes[1](),
[2] = self.makePlanes[2]() }
Ground(self);
end;
updateBackground = function(self)
local w, h = levelWidth(), levelHeight()
if w ~= self.oldWidth or h ~= self.oldHeight then
local background = love.graphics.newImage('resources/graphics/sky.png')
self.bgCanvas = love.graphics.newCanvas()
love.graphics.setCanvas(self.bgCanvas)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(background, 0, 0, 0,
levelWidth() / background:getWidth(),
levelHeight() / background:getHeight())
love.graphics.setCanvas()
self.oldWidth, self.oldHeight = w, h
end
end;
respawnPlayer = function(self, playerIdx)
self.planes[playerIdx] = self.makePlanes[playerIdx]()
end;
@ -70,6 +83,7 @@ Level = Class{
end;
drawBackground = function(self)
self:updateBackground()
love.graphics.setColor(draw_base_color)
love.graphics.draw(self.bgCanvas)
end;
@ -81,11 +95,11 @@ Level = Class{
if entity:isinstance(PhysicsEntity) then
local jump_window = 70
local jump_amount = 50
while entity.body:getX() > WIDTH + jump_window do
entity.body:setX(entity.body:getX() - WIDTH - jump_window - jump_amount)
while entity.body:getX() > levelWidth() + jump_window do
entity.body:setX(entity.body:getX() - levelWidth() - jump_window - jump_amount)
end
while entity.body:getX() < -jump_window do
entity.body:setX(entity.body:getX() + WIDTH + jump_window + jump_amount)
entity.body:setX(entity.body:getX() + levelWidth() + jump_window + jump_amount)
end
end
entity:update(dt)

View file

@ -150,8 +150,8 @@ function level_state:update(dt)
local r = love.math.random()
if r <= POWERUP_POSSIBILITY * dt then
local x = love.math.random(WIDTH)
local y = love.math.random(HEIGHT)
local x = love.math.random(levelWidth())
local y = love.math.random(levelHeight())
POWERUPS[love.math.random(1, #POWERUPS)](x, y, current_level)
end

View file

@ -144,7 +144,10 @@ end
function menu_state:draw()
love.graphics.setColor(128, 128, 128, 255)
love.graphics.draw(background)
love.graphics.draw(background, 0, 0, 0,
levelWidth() / background:getWidth(),
levelHeight() / background:getHeight())
local seconds, subseconds = math.modf(menuTime)

View file

@ -8,8 +8,8 @@ PIXELS_PER_METER = 8
GRAVITY_X = 0
GRAVITY_Y = 9.82599 * PIXELS_PER_METER
WIDTH = 1280
HEIGHT = 768
INITIAL_WIDTH = 1280
INITIAL_HEIGHT = 768
KEYMAP = {
[1] = {
@ -57,6 +57,16 @@ if love.filesystem.isFile("fysplane.cfg") then
AXISMAP = m.AXISMAP
end
function levelWidth()
local width, height = love.window.getMode()
return width
end
function levelHeight()
local width, height = love.window.getMode()
return height
end
PLANE_HEALTH = 200
PLANE_DEFAULT_GUN = "vickers77"