Skip to content

Commit

Permalink
Fade effect, fixing a minor physics bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nullenvk committed Apr 10, 2022
1 parent 3c07a6b commit b156edc
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
55 changes: 55 additions & 0 deletions effect_fade.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require("sprite")

local TEXTURE_PATH_FADE = "res/fader.png"
local FADE_TIME = 0.75

FadeEffect = Sprite:new({
reversed = false,
enabled = false,
startTime = nil,
})

function FadeEffect.preload()
FadeEffect:loadTexture(TEXTURE_PATH_FADE)
end

function FadeEffect.free()
FadeEffect.texture = nil
end

function FadeEffect:new(o, reversed)
o = o or Sprite:new(o)
setmetatable(o, self)
self.__index = self

o.reversed = reversed or false
return o
end

function FadeEffect:draw()
if self.enabled then
-- Fade sprite
local progress = math.min(1, (love.timer.getTime() - self.startTime) / FADE_TIME)

if self.reversed then progress = 1 - progress end

self.screenPos.x = progress * (800 + self.spriteSize.w) - self.spriteSize.w
Sprite.draw(self)

love.graphics.setColor(0,0,0)
love.graphics.rectangle("fill", 0, 0, self.screenPos.x, 600)
end
end

function FadeEffect:update()
Sprite.update(self)
end

function FadeEffect:start()
self.enabled = true
self.startTime = love.timer.getTime()
end

function FadeEffect:reset()
self.enabled = false
end
12 changes: 7 additions & 5 deletions player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ function Player:new(o)
setmetatable(o, self)
self.__index = self

local tw, th = Player.texture:getDimensions()
o.size = {w = tw, h = th}
o.size = self.spriteSize

return o
end
Expand Down Expand Up @@ -143,9 +142,12 @@ local function colTestNarrow(r1, r2, dPos)
return true, timeStart, vNormal
end

local function isTileHitPossible(tilemap, tx, ty, normVec)
local function isTileEmpty(tilemap, tx, ty, normVec)
-- just some special cases
local tdat = tilemap.dat[tx + normVec.x][ty + normVec.y]
local tdatx = tilemap.dat[tx + normVec.x]
if tdatx == nil then return false end

local tdat = tdatx[ty + normVec.y]
return tdat == "0" or tdat == nil
end

Expand All @@ -164,7 +166,7 @@ function Player:runColTests(tilemap, dPos)
tilerect.y = (ty-1)*tileH

local didHit, whenHit, normVec = colTestNarrow(plyRect, tilerect, dPos)
if didHit and tilemap.dat[tx][ty] ~= "0" and isTileHitPossible(tilemap, tx, ty, normVec) then -- second option should normally be handled by broad phase
if didHit and tilemap.dat[tx][ty] ~= "0" and isTileEmpty(tilemap, tx, ty, normVec) then -- second option should normally be handled by broad phase
local hType = (normVec.x ~= 0) and 1 or 2
finHitTime[hType] = math.min(finHitTime[hType], whenHit)
end
Expand Down
Binary file modified res/fader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions sprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Sprite = GameObj:new({
spriteFlipX = false,
spriteFlipY = false,

texture = nil
-- read only
texture = nil,
spriteSize = {w = 0, h = 0},
})

function Sprite:new(o)
Expand All @@ -16,6 +18,7 @@ end

function Sprite:loadTexture(path)
self.texture = love.graphics.newImage(path)
self.spriteSize.w, self.spriteSize.h = self.texture:getDimensions()
end

function Sprite:update(dt)
Expand All @@ -24,8 +27,6 @@ end
function Sprite:draw()
if self.texture == nil then error("Tried to draw a sprite without a loaded texture") end

local posScale

local flipScaleX = self.spriteFlipX and -1 or 1
local flipScaleY = self.spriteFlipY and -1 or 1
local offsetX = self.spriteFlipX and self.texture:getWidth() or 0
Expand All @@ -34,3 +35,4 @@ function Sprite:draw()
love.graphics.setColor(1,1,1)
love.graphics.draw(self.texture, self.screenPos.x + offsetX, self.screenPos.y + offsetY, 0, flipScaleX, flipScaleY)
end

29 changes: 16 additions & 13 deletions state_game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('gamestate')
require('sprite')
require('player')
require('tilemap')
require('effect_fade')

State_Game = GameState:new({})

Expand All @@ -12,8 +13,9 @@ local LAYERNUM_NUM = 2

local curScene = {}
local tmap = nil
local screenPos = nil
local tilescrPos = nil
local playerObj = nil
local introFade = nil

ObjTiles = GameObj:new({dat = nil})

Expand Down Expand Up @@ -54,8 +56,8 @@ function State_Game:switchScreen(dx, dy)
curScene = {}
for _=1,LAYERNUM_NUM do table.insert(curScene, {}) end

screenPos.x = screenPos.x + dx
screenPos.y = screenPos.y + dy
tilescrPos.x = tilescrPos.x + dx
tilescrPos.y = tilescrPos.y + dy

if playerObj == nil then
playerObj = Player:new()
Expand All @@ -66,15 +68,19 @@ function State_Game:switchScreen(dx, dy)
end

curScene[LAYERNUM_MAP]["player"] = playerObj
curScene[LAYERNUM_MAP]["tiles"] = ObjTiles:new(nil, screenPos.x, screenPos.y)
curScene[LAYERNUM_MAP]["tiles"] = ObjTiles:new(nil, tilescrPos.x, tilescrPos.y)
end

function State_Game:init()
tmap = TileMap:new(nil, "res/main.map")
FadeEffect.preload()
Player.preload()

screenPos = {x = 1, y = 1}
tilescrPos = {x = 1, y = 1}
self:switchScreen(0,0)

introFade = FadeEffect:new(nil, true)
introFade:start()
end

function State_Game:draw()
Expand All @@ -86,14 +92,16 @@ function State_Game:draw()
for _,v in pairs(sceneObjs) do v:runDraw() end
end

-- Effects
introFade:draw()
end

function State_Game:testScrSwitch()
local function unstuckPlayer(horz)
if horz then
playerObj.pos.y = playerObj.pos.y + (playerObj.gravFlip and -1 or 1)
else
--playerObj.pos.x = playerObj.pos.x + (playerObj.facingSide and -1 or 1)
playerObj.pos.x = playerObj.pos.x + (playerObj.facingSide and -1 or 1)
end
end

Expand Down Expand Up @@ -150,6 +158,7 @@ function State_Game:update(dt)

-- Inputs
if love.keyboard.isDown("escape") then newState = State_MainMenu end
if love.keyboard.isDown("space") then playerObj:doFlip() end

self:testScrSwitch()

Expand All @@ -163,17 +172,11 @@ function State_Game:fini()

curScene = nil
tmap = nil
screenPos = nil
tilescrPos = nil
playerObj = nil

Player.free()
end

function GameState:keypressed(key, _, isrepeat)
local player = curScene[LAYERNUM_MAP]["player"]
if player == nil then return end

if key == "space" and not isrepeat then
player:doFlip()
end
end
12 changes: 12 additions & 0 deletions state_mainmenu.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('gamestate')
require('state_game')
require('effect_fade')

State_MainMenu = GameState:new({})

Expand All @@ -14,12 +15,17 @@ local menuState
local newState = nil

function State_MainMenu:init()
FadeEffect.preload()

menuState = {}
menuState.bgText = love.graphics.newImage("res/menubg.png")
menuState.mainFont = love.graphics.newFont(MENUFONT_SIZE)
menuState.introFade = FadeEffect:new(nil, true)

menuState.mainTimer = 0
menuState.curMainOption = 1

menuState.introFade:start()
end

local function mainmenuNextOpt()
Expand All @@ -37,6 +43,8 @@ local function activateSelOption()
-- Launch game here
newState = State_Game

-- debug
elseif menuState.curMainOption == 2 then
elseif menuState.curMainOption == 3 then
love.event.quit()
end
Expand All @@ -59,9 +67,13 @@ function State_MainMenu:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(menuState.bgText, 0, 0)
love.graphics.printf(MENU_TEXTS[menuState.curMainOption], menuState.mainFont, 0, 300 - MENUFONT_SIZE/2, 800, "center")

menuState.introFade:draw()
end

function State_MainMenu:fini()
menuState = nil
newState = nil

FadeEffect.free()
end

0 comments on commit b156edc

Please sign in to comment.