Display game board

This commit is contained in:
theo@manjaro 2022-03-03 20:24:10 +01:00
parent 59ec031e6e
commit 0be510ea83
17 changed files with 780 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 911 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 796 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 970 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 796 B

BIN
assets/circles/shadow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

BIN
assets/grid/blue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

BIN
assets/grid/pink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 685 B

BIN
assets/grid/tile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 636 B

258
game.lua Normal file
View File

@ -0,0 +1,258 @@
local Game = {}
local insert = table.insert
function Game:reinit()
self.gameloop = {}
self.objects = {}
self.objects.base = require "objects/base"
self.objects.grid = require "objects/grid"
self.objects.circle = require "objects/circle"
self.rect = require "lib/rect"
self.maxobjects = 0
self.camerax = 0
self.cameray = 0
self.floatcamerax = 0
self.floatcameray = 0
self.assets = {} -- Caching loaded images
self:initInputs()
end
function Game:changesize(width,height)
self.WIDTH = width
self.HEIGHT = height
end
function Game:movecamera(movex,movey)
self.floatcamerax = self.floatcamerax + movex
self.floatcameray = self.floatcameray + movey
self.camerax = math.floor(self.floatcamerax+0.5)
self.cameray = math.floor(self.floatcameray+0.5)
end
function Game:setcamera(x,y)
self.floatcamerax = x
self.floatcameray = y
self.camerax = math.floor(self.floatcamerax+0.5)
self.cameray = math.floor(self.floatcameray+0.5)
end
function Game:initInputs()
self.inputs = {}
self.inputs.deadzone = 0.4
self.inputs.keys = {}
self.inputs.buttons = {}
self.inputs.axis= {}
self.inputs.actions = {}
for i,v in pairs({"up","down","left","right","menu","valid"}) do
local action = {}
action.keys = {}
action.buttons = {}
action.axis = {}
self.inputs.actions[v] = action
action.timer = 0
action.lastpress = 99
end
self.inputs.actions.valid.keys = {"space"}
self.inputs.actions.valid.buttons = {"a"}
self.inputs.actions.up.keys = {"up","w"}
self.inputs.actions.up.buttons = {"dpup"}
self.inputs.actions.up.axis = {{"lefty",-1}}
self.inputs.actions.down.keys = {"down","s"}
self.inputs.actions.down.buttons = {"dpdown"}
self.inputs.actions.down.axis = {{"lefty",1}}
self.inputs.actions.left.keys = {"left","a"}
self.inputs.actions.left.buttons = {"dpleft"}
self.inputs.actions.left.axis = {{"leftx",-1}}
self.inputs.actions.right.keys = {"right","d"}
self.inputs.actions.right.buttons = {"dpright"}
self.inputs.actions.right.axis = {{"leftx",1}}
self.inputs.actions.menu.keys = {"escape","return"}
self.inputs.actions.menu.buttons = {"start","back"}
end
function Game:updateInputs(dt)
for i,action in pairs(self.inputs.actions) do
local pressed = false
for _,key in pairs(action.keys) do
if self.inputs.keys[key] then pressed = true end
end
for _,button in pairs(action.buttons) do
if self.inputs.buttons[button] then pressed = true end
end
for _,axis in pairs(action.axis) do
if self.inputs.axis[axis[1]]~=nil then
if self.inputs.axis[axis[1]]*axis[2]>self.inputs.deadzone then
pressed = true
end
end
end
if pressed then
action.timer = action.timer+1
if action.timer == 1 then
action.lastpress = 0
end
else
action.timer = 0
end
action.lastpress = action.lastpress + dt
end
end
function Game:summon(obj)
obj.game = self
obj.id = self.maxobjects
self.maxobjects = self.maxobjects + 1
insert(self.gameloop,obj)
end
function Game:findName(askedname)
local result = {}
for i,v in ipairs(self.gameloop) do
if v~=nil then
for j,name in pairs(v.classes) do
if name==askedname then
insert(result,v)
end
end
end
end
return result
end
function Game:delid(objid)
self.gameloop[objid] = nil
end
function Game:delname(objName)
for i,v in ipairs(Game:findName(objName)) do
if v~=nil then
self:delid(v.id)
end
end
end
function Game:step(dt)
self:updateInputs(dt)
for i,v in ipairs(self.gameloop) do
--Processing all the objects
if v~=nil then v:step(dt) end
end
end
function Game:newImage(path)
local image = nil
if self.assets[path]~=nil then -- If already loaded
image = self.assets[path]
else
image = love.graphics.newImage("assets/"..path) -- Load it and save it
self.assets[path] = image
end
return image
end
function Game:getSpriteDir(path,ext,func,folder)
if not ext then ext = ".png" end
if not func then func = self.newImage end
if not folder then folder = "assets/" end
local counter = 0
local result = {}
while love.filesystem.getInfo(folder..path..counter..ext) do
insert(result,func(self,path..counter..ext))
counter = counter + 1
end
return result
end
function Game:getLevel(i)
local level = nil
if self.levels[i]~=nil then
level = self.levels[i]
else
level = {}
level.folder = "maps/Level "..i.."/"
local module = level.folder.."data"
level.data = require(module)
level.backgrounds = {}
level.bgsizes = {}
-- Get backgrounds
local counter = 0
while love.filesystem.getInfo(level.folder.."background"..counter..".png") do
img = love.graphics.newImage(level.folder.."background"..counter..".png")
table.insert(level.backgrounds,img)
table.insert(level.bgsizes,{img:getWidth(),img:getHeight()})
counter = counter + 1
end
self.levels[i] = level
end
return level
end
function Game:draw(screen)
draworder = {}
for i,v in ipairs(self.gameloop) do
if v~=nil then
table.insert(draworder,v)
end
end
table.sort(draworder, function(a,b) return a.depth< b.depth end)
for i,v in ipairs(draworder) do
--Processing all the objects
if v~=nil then
if screen~= "bottom" then
if v.draw ~= nil then
local offset = 0
if screen=="left" then offset = -love.graphics.get3DDepth() end
if screen=="right" then offset = love.graphics.get3DDepth() end
self.camerax = self.camerax + offset
v:draw()
self.camerax = self.camerax - offset
end
else
if v.bottomdraw~=nil then v.bottomdraw() end
end
end
end
end
function Game:bint(bool) -- Convert Boolean to Integer
local result = 0
if bool then result = 1 end
return result
end
function Game:Timer(time)
local Timer = {}
Timer.maxcount = time
Timer.timer = time
Timer.loops = 0
function Timer:tick(advancement)
local result = false
self.timer = self.timer - advancement
if self.timer<0 then
result = true
self.timer = self.timer + self.maxcount
self.loops = self.loops + 1
end
return result
end
function Timer:getratio()
local result = self.timer/self.maxcount
if result<0 then result = 0 end
return result
end
function Timer:reset()
self.timer = self.maxcount
self.loops = 0
end
return Timer
end
Game:reinit()
return Game

281
lib/push.lua Normal file
View File

@ -0,0 +1,281 @@
-- push.lua v0.4
-- Copyright (c) 2020 Ulysse Ramage
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local love11 = love.getVersion() == 11
local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale
local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings)
local _, _, flags = love.window.getMode()
for k, v in pairs(settings) do flags[k] = v end
love.window.setMode(width, height, flags)
end
local push = {
defaults = {
fullscreen = false,
resizable = false,
pixelperfect = false,
highdpi = true,
canvas = true,
stencil = true
}
}
setmetatable(push, push)
function push:applySettings(settings)
for k, v in pairs(settings) do
self["_" .. k] = v
end
end
function push:resetSettings() return self:applySettings(self.defaults) end
function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
settings = settings or {}
self._WWIDTH, self._WHEIGHT = WWIDTH, WHEIGHT
self._RWIDTH, self._RHEIGHT = RWIDTH, RHEIGHT
self:applySettings(self.defaults) --set defaults first
self:applySettings(settings) --then fill with custom settings
windowUpdateMode(self._RWIDTH, self._RHEIGHT, {
fullscreen = self._fullscreen,
resizable = self._resizable,
highdpi = self._highdpi
})
self:initValues()
if self._canvas then
self:setupCanvas({ "default" }) --setup canvas
end
self._borderColor = {0, 0, 0}
self._drawFunctions = {
["start"] = self.start,
["end"] = self.finish
}
return self
end
function push:setupCanvas(canvases)
table.insert(canvases, { name = "_render", private = true }) --final render
self._canvas = true
self.canvases = {}
for i = 1, #canvases do
push:addCanvas(canvases[i])
end
return self
end
function push:addCanvas(params)
table.insert(self.canvases, {
name = params.name,
private = params.private,
shader = params.shader,
canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT),
stencil = params.stencil or self._stencil
})
end
function push:setCanvas(name)
if not self._canvas then return true end
local canvasTable = self:getCanvasTable(name)
return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil })
end
function push:getCanvasTable(name)
for i = 1, #self.canvases do
if self.canvases[i].name == name then
return self.canvases[i]
end
end
end
function push:setShader(name, shader)
if not shader then
self:getCanvasTable("_render").shader = name
else
self:getCanvasTable(name).shader = shader
end
end
function push:initValues()
self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1
self._SCALE = {
x = self._RWIDTH/self._WWIDTH * self._PSCALE,
y = self._RHEIGHT/self._WHEIGHT * self._PSCALE
}
if self._stretched then --if stretched, no need to apply offset
self._OFFSET = {x = 0, y = 0}
else
local scale = math.min(self._SCALE.x, self._SCALE.y)
if self._pixelperfect then scale = math.floor(scale) end
self._OFFSET = {x = (self._SCALE.x - scale) * (self._WWIDTH/2), y = (self._SCALE.y - scale) * (self._WHEIGHT/2)}
self._SCALE.x, self._SCALE.y = scale, scale --apply same scale to X and Y
end
self._GWIDTH = self._RWIDTH * self._PSCALE - self._OFFSET.x * 2
self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2
end
function push:apply(operation, shader)
self._drawFunctions[operation](self, shader)
end
function push:start()
if self._canvas then
love.graphics.push()
love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil })
else
love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y)
love.graphics.push()
love.graphics.scale(self._SCALE.x, self._SCALE.y)
end
end
function push:applyShaders(canvas, shaders)
local _shader = love.graphics.getShader()
if #shaders <= 1 then
love.graphics.setShader(shaders[1])
love.graphics.draw(canvas)
else
local _canvas = love.graphics.getCanvas()
local _tmp = self:getCanvasTable("_tmp")
if not _tmp then --create temp canvas only if needed
self:addCanvas({ name = "_tmp", private = true, shader = nil })
_tmp = self:getCanvasTable("_tmp")
end
love.graphics.push()
love.graphics.origin()
local outputCanvas
for i = 1, #shaders do
local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas
outputCanvas = i % 2 == 0 and canvas or _tmp.canvas
love.graphics.setCanvas(outputCanvas)
love.graphics.clear()
love.graphics.setShader(shaders[i])
love.graphics.draw(inputCanvas)
love.graphics.setCanvas(inputCanvas)
end
love.graphics.pop()
love.graphics.setCanvas(_canvas)
love.graphics.draw(outputCanvas)
end
love.graphics.setShader(_shader)
end
function push:finish(shader)
love.graphics.setBackgroundColor(unpack(self._borderColor))
if self._canvas then
local _render = self:getCanvasTable("_render")
love.graphics.pop()
local white = love11 and 1 or 255
love.graphics.setColor(white, white, white)
--draw canvas
love.graphics.setCanvas(_render.canvas)
for i = 1, #self.canvases do --do not draw _render yet
local _table = self.canvases[i]
if not _table.private then
local _canvas = _table.canvas
local _shader = _table.shader
self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader })
end
end
love.graphics.setCanvas()
--draw render
love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
local shader = shader or _render.shader
love.graphics.push()
love.graphics.scale(self._SCALE.x, self._SCALE.y)
self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader })
love.graphics.pop()
--clear canvas
for i = 1, #self.canvases do
love.graphics.setCanvas(self.canvases[i].canvas)
love.graphics.clear()
end
love.graphics.setCanvas()
love.graphics.setShader()
else
love.graphics.pop()
love.graphics.setScissor()
end
end
function push:setBorderColor(color, g, b)
self._borderColor = g and {color, g, b} or color
end
function push:toGame(x, y)
x, y = x - self._OFFSET.x, y - self._OFFSET.y
local normalX, normalY = x / self._GWIDTH, y / self._GHEIGHT
x = (x >= 0 and x <= self._WWIDTH * self._SCALE.x) and normalX * self._WWIDTH or nil
y = (y >= 0 and y <= self._WHEIGHT * self._SCALE.y) and normalY * self._WHEIGHT or nil
return x, y
end
function push:toReal(x, y)
local realX = self._OFFSET.x + (self._GWIDTH * x)/self._WWIDTH
local realY = self._OFFSET.y + (self._GHEIGHT * y)/self._WHEIGHT
return realX, realY
end
function push:switchFullscreen(winw, winh)
self._fullscreen = not self._fullscreen
local windowWidth, windowHeight = love.window.getDesktopDimensions()
if self._fullscreen then --save windowed dimensions for later
self._WINWIDTH, self._WINHEIGHT = self._RWIDTH, self._RHEIGHT
elseif not self._WINWIDTH or not self._WINHEIGHT then
self._WINWIDTH, self._WINHEIGHT = windowWidth * .5, windowHeight * .5
end
self._RWIDTH = self._fullscreen and windowWidth or winw or self._WINWIDTH
self._RHEIGHT = self._fullscreen and windowHeight or winh or self._WINHEIGHT
self:initValues()
love.window.setFullscreen(self._fullscreen, "desktop")
if not self._fullscreen and (winw or winh) then
windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions
end
end
function push:resize(w, h)
if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end
self._RWIDTH = w
self._RHEIGHT = h
self:initValues()
end
function push:getWidth() return self._WWIDTH end
function push:getHeight() return self._WHEIGHT end
function push:getDimensions() return self._WWIDTH, self._WHEIGHT end
return push

75
lib/rect.lua Normal file
View File

@ -0,0 +1,75 @@
rectgen = {}
function rectgen:new(x,y,w,h)
Rect = {x,y,w,h}
function Rect:move(x,y)
self[1] = self[1] + x
self[2] = self[2] + y
self:reAdapt()
end
function Rect:move_ip(x,y)
self[1] = x
self[2] = y
self:reAdapt()
end
function Rect:setSize(w,h)
self[3] = w
self[4] = h
self:reAdapt()
end
function Rect:move_center(x,y)
self[1] = x-self.w/2
self[2] = y-self.h/2
self:reAdapt()
end
function Rect:reAdapt()
self.x = self[1]
self.y = self[2]
self.w = self[3]
self.h = self[4]
self.center = {self.x+self.w/2,self.y+self.h/2}
self.center.x = self.x+self.w/2
self.center.y = self.y+self.h/2
self.up = self.y
self.left = self.x
self.right = self.x+self.w
self.down = self.y+self.h
end
function Rect:colliderect(orect,xoffset,yoffset,semisolid) -- AABB collision check
local result = true
local offx = xoffset or 0
local offy = yoffset or 0
if self.right+offx <= orect.left or self.left+offx>=orect.right then result = false end
if self.down+offy <= orect.up or self.up+offy>=orect.down then result = false end
if semi and result then
if not self.bottom<orect.top then result = false end
end
return result
end
function Rect:collidepoint(x,y)
return self.x<x and x<self.x+self.w and self.y<y and y<self.y+self.h
end
function Rect:colliderects(recttable,xoffset,yoffset,semisolid)
local result = {}
for i,v in pairs(recttable) do
if self:colliderect(v,xoffset,yoffset,semisolid) then table.insert(result,v) end
end
return result
end
Rect:reAdapt()
return Rect
end
return rectgen

72
main.lua Normal file
View File

@ -0,0 +1,72 @@
local game = require "game"
game.OS = love.system.getOS()
game.OSTYPE = "PC"
if game.OS == 'Horizon' then
game.OSTYPE = love._console_name -- 3DS or Switch
end
if game.OS == "Android" or game.OS == "iOS" then game.OSTYPE = "Mobile" end
lg = love.graphics
if game.OSTYPE=="PC" then push = require "lib/push" end
function love.load()
lg.setBackgroundColor(31/255,14/255,28/255)
if pcall (lg.set3D, true) == true then
lg.set3D(true)
end
game:changesize(400,240)
lg.setDefaultFilter("nearest")
if game.OS ~= "Horizon" then
game:changesize(416,234)
love.window.setTitle('Overflown')
w,h = love.window.getDesktopDimensions()
res = game.OSTYPE=="PC"
push:setupScreen(game.WIDTH, game.HEIGHT, w, h, {fullscreen = true,resizable = res, pixelperfect = true})
push:setBorderColor(0.161,0.157, 0.192,1)
end
local cellsize = 44
local x = (game.WIDTH-cellsize*5)/2
local y = (game.HEIGHT-cellsize*5)/2
local grid = game.objects.grid:new(game,x,y,cellsize)
game:summon(grid)
end
function love.draw(screen)
if game.OSTYPE=="PC" then push:start() end
game:draw(screen)
if game.OSTYPE=="PC" then push:finish() end
end
function love.update()
dt = love.timer.getDelta()
game:step(dt)
end
function love.keypressed(key,scancode)
game.inputs.keys[scancode] = true
end
function love.gamepadpressed(joystick, button)
game.inputs.buttons[button] = true
end
function love.keyreleased(key,scancode)
game.inputs.keys[scancode] = false
end
function love.gamepadreleased(joystick, button)
game.inputs.buttons[button] = false
end
function love.gamepadaxis( joystick, axis, value )
game.inputs.axis[axis] = value
end
function love.resize(w, h)
if game.OSTYPE~="3DS" then push:resize(w, h) end
end

27
objects/base.lua Normal file
View File

@ -0,0 +1,27 @@
local basegen = {}
function basegen:new(game,x,y,w,h)
local Base = {}
Base.rect = game.rect:new(x or 0, y or 0, w or 0, h or 0)
Base.spriteoffx = 0
Base.spriteoffy = 0
Base.sprite = game:newImage("grid/tile.png")
Base.game = game
Base.depth = 0
Base.classes = {"Base"}
function Base:step(dt) end
function Base:register(name)
table.insert(self.classes,name)
end
function Base:draw()
lg.draw(self.sprite,self.rect[1]-self.game.camerax-self.spriteoffx,self.rect[2]-self.game.cameray-self.spriteoffy)
end
return Base
end
return basegen

25
objects/circle.lua Normal file
View File

@ -0,0 +1,25 @@
local gen= {}
function gen:new(game,x,y,color,grid)
local Circle = game.objects.base:new(game,x,y,grid.cellsize,grid.cellsize)
Circle.cellsize = grid.cellsize
Circle.grid = grid
Circle.shadowsprite = game:newImage("circles/shadow.png")
Circle.sprites = {game:newImage("circles/regular/"..color..".png"),game:newImage("circles/selected/"..color..".png")}
Circle:register("Circle")
Circle:register(color)
Circle.selected = false
function Circle:step(dt) end
function Circle:draw()
local spriteindex = 1
if self.selected then spriteindex = 2 end
lg.draw(self.sprites[spriteindex],self.rect[1]-self.game.camerax-self.spriteoffx,self.rect[2]-self.game.cameray-self.spriteoffy)
end
return Circle
end
return gen

42
objects/grid.lua Normal file
View File

@ -0,0 +1,42 @@
local gridgen = {}
function gridgen:new(game,x,y,cellsize)
local Grid = game.objects.base:new(game,x,y,cellsize*5,cellsize*5)
Grid.cellsize = cellsize
Grid.sprites = {game:newImage("grid/tile.png"),game:newImage("grid/pink.png"),game:newImage("grid/blue.png")}
Grid:register("Grid")
Grid.depth = -1
-- Spawn circles
local i
for i=1,5 do
local xspawn = x+(i-1)*cellsize
local pink = game.objects.circle:new(game,xspawn,y,"pink",self)
local blue = game.objects.circle:new(game,xspawn,y+4*cellsize,"blue",Grid)
game:summon(pink)
game:summon(blue)
end
local neutron = game.objects.circle:new(game,x+2*cellsize,y+2*cellsize,"neutron",Grid)
game:summon(neutron)
function Grid:step(dt) end
function Grid:draw()
local y
local x
for y=1,5 do
for x=1,5 do
local spriteindex = 1
if y==1 then spriteindex = 2 end
if y==5 then spriteindex = 3 end
lg.draw(self.sprites[spriteindex],self.rect[1]-self.game.camerax-self.spriteoffx+(x-1)*self.cellsize,self.rect[2]-self.game.cameray-self.spriteoffy+(y-1)*self.cellsize)
end
end
end
return Grid
end
return gridgen