Moved maze constants to class or globals

This commit is contained in:
2024-11-29 17:46:40 -06:00
parent 0e4c5ed9ab
commit 738baa3d69
5 changed files with 55 additions and 46 deletions

View File

@@ -1 +1,8 @@
VIRTUAL_WIDTH, VIRTUAL_HEIGHT = 640, 360
TILE_SIZE = 16
Tile = {}
Tile.passage = 0
Tile.wall = 1
Tile.entrance = 2
Tile.exit = 3

View File

@@ -8,14 +8,13 @@ local Siren = require 'src.entities.enemies.siren'
local Effects = require 'src.display.shaders.effects'
local mazeutils = require 'src.maze_generation.utils'
local Maze = require 'src.maze_generation.oshift'
require 'src.maze_generation'
local Game = {}
Game.__index = Game
local function initMaze(world)
local function initMaze(world, mazeWidth, mazeHeight)
local walls = {}
local function createWall(x, y)
@@ -32,7 +31,7 @@ local function initMaze(world)
return wall
end
local maze = Maze.new(MAZE_SIZE, MAZE_SIZE)
local maze = Maze.new(mazeWidth, mazeHeight)
maze:shuffle()
local grid = maze:toGrid()
@@ -46,7 +45,7 @@ local function initMaze(world)
for _, exitTile in ipairs(exitTiles) do
local exitX, exitY = mazeutils.tileToWorld(exitTile.x, exitTile.y)
local exit = { x = exitX, y = exitY, isExit = true }
grid[exitTile.y][exitTile.x] = MAZE_EXIT
grid[exitTile.y][exitTile.x] = Tile.exit
table.insert(exits, exit)
world:add(
exit,
@@ -59,7 +58,7 @@ local function initMaze(world)
for y = 1, #grid do
for x = 1, #grid[1] do
if grid[y][x] == MAZE_WALL then
if grid[y][x] == Tile.wall then
local wall = createWall(x - 1, y - 1)
world:add(
@@ -76,16 +75,20 @@ local function initMaze(world)
return walls, grid, entrance, exits
end
function Game.new(core)
function Game.new(core, mazeWidth, mazeHeight)
setmetatable(Game, { __index = LoveNode })
local self = setmetatable(LoveNode.new(), Game)
self.core = core
self.camera = Camera.new(WORLD_WIDTH, WORLD_HEIGHT)
self.camera = Camera.new(
(1 + 2 * mazeWidth) * TILE_SIZE,
(1 + 2 * mazeHeight) * TILE_SIZE
)
self.input = baton.new(input.player)
self.env = {}
self.env.mazeWidth, self.env.mazeHeight = mazeWidth, mazeHeight
self.env.entrance = {}
self.env.exits = {}
@@ -95,7 +98,11 @@ end
function Game:enter()
-- Init environment
self.world = bump.newWorld(TILE_SIZE)
self.env.walls, self.env.maze, self.env.entrance, self.env.exits = initMaze(self.world)
self.env.walls, self.env.maze, self.env.entrance, self.env.exits = initMaze(
self.world,
self.env.mazeWidth,
self.env.mazeHeight
)
self.player = Player.new(self.env.entrance.x, self.env.entrance.y, self)
self.effect = Effects.new(self)
@@ -154,4 +161,12 @@ function Game:resize(w, h)
self.effect:resize(w, h)
end
function Game:failLevel()
self.core:switchState(self.core.states.mainmenu)
end
function Game:nextLevel()
self.core:switchState(self.core.states.game)
end
return Game

View File

@@ -1,11 +0,0 @@
TILE_SIZE = 16
MAZE_SIZE = 8
WORLD_SIZE = (1 + 2 * MAZE_SIZE) * TILE_SIZE
WORLD_WIDTH, WORLD_HEIGHT = WORLD_SIZE, WORLD_SIZE
MAZE_PASSAGE = 0
MAZE_WALL = 1
MAZE_START = 2
MAZE_EXIT = 3

View File

@@ -1,4 +1,3 @@
require 'src.maze_generation'
local mazeutils = require 'src.maze_generation.utils'
-- Define the Node class
@@ -94,7 +93,7 @@ function Maze:shuffle(n)
end
function Maze:toGrid()
local grid = mazeutils.initializeGrid(1 + 2 * self.width, 1 + 2 * self.height, MAZE_WALL)
local grid = mazeutils.initializeGrid(1 + 2 * self.width, 1 + 2 * self.height, Tile.wall)
for y = 1, self.height do
for x = 1, self.width do
@@ -102,13 +101,13 @@ function Maze:toGrid()
local xPos = 2 * x
local yPos = 2 * y
grid[yPos][xPos] = MAZE_PASSAGE
grid[yPos][xPos] = Tile.passage
if node.direction.x ~= 0 or node.direction.y ~= 0 then
local xNeighbor = xPos + node.direction.x
local yNeighbor = yPos + node.direction.y
grid[yNeighbor][xNeighbor] = MAZE_PASSAGE
grid[yNeighbor][xNeighbor] = Tile.passage
end
end
end

View File

@@ -1,4 +1,3 @@
require 'src.maze_generation'
local lume = require 'packages.lume'
--- @alias Maze integer[][]
@@ -21,48 +20,48 @@ function mazeutils.initializeGrid(width, height, defaultValue)
return grid
end
--- Find the first position in the maze with the MAZE_START value
--- Find the first position in the maze with the Tile.entrance value
--- @param maze Maze
--- @return integer, integer
function mazeutils.getEntrance(maze)
local height, width = #maze, #maze[1]
-- Check for MAZE_START tile in the maze
-- Check for Tile.entrance tile in the maze
for y = 1, height do
for x = 1, width do
if maze[y][x] == MAZE_START then
if maze[y][x] == Tile.entrance then
return x, y
end
end
end
-- Default close to center
local midPoint = MAZE_SIZE + 1
local defaultX, defaultY = midPoint, midPoint
-- When the maze is 'even', opt for the top right corner of the center tiles
if MAZE_SIZE % 2 == 0 then
defaultX, defaultY = MAZE_SIZE, MAZE_SIZE
end
return defaultX, defaultY
local nodeWidth, nodeHeight = (width - 1) / 2, (height - 1) / 2
local midNodeX, midNodeY = math.ceil(nodeWidth / 2), math.ceil(nodeHeight / 2)
return midNodeX * 2, midNodeY * 2
end
function mazeutils.getExits(maze, numExits)
-- Look for existing exit tiles
local height, width = #maze, #maze[1]
for y = height, 1, -1 do
for x = width, 1, -1 do
if maze[y][x] == MAZE_EXIT then
if maze[y][x] == Tile.exit then
return { { x = x, y = y } }
end
end
end
-- Generate `numExits` exits around the edge of the maze by default
local potentialExits = {}
for i = 1, MAZE_SIZE do
table.insert(potentialExits, { x = i * 2, y = 1 }) -- Top slots
table.insert(potentialExits, { x = i * 2, y = MAZE_SIZE * 2 + 1 }) -- Bottom slots
table.insert(potentialExits, { x = MAZE_SIZE * 2 + 1, y = i * 2 }) -- Right slots
table.insert(potentialExits, { x = 1, y = i * 2 }) -- Left slots
local nodeHeight, nodeWidth = (height - 1) / 2, (width - 1) / 2
for x = 1, nodeWidth do
table.insert(potentialExits, { x = x * 2, y = 1 }) -- Top slots
table.insert(potentialExits, { x = x * 2, y = height }) -- Bottom slots
end
for y = 1, nodeHeight do
table.insert(potentialExits, { x = width, y = y * 2 }) -- Right slots
table.insert(potentialExits, { x = 1, y = y * 2 }) -- Left slots
end
return lume.bagrandom(potentialExits, numExits)
@@ -113,16 +112,16 @@ function mazeutils.drawMaze(maze, l, t, w, h)
local function countNeighboringWalls(x, y)
local bit = 0
if maze[y - 1] ~= nil and maze[y - 1][x] == MAZE_WALL then
if maze[y - 1] ~= nil and maze[y - 1][x] == Tile.wall then
bit = bit + 1
end
if maze[y][x - 1] == MAZE_WALL then
if maze[y][x - 1] == Tile.wall then
bit = bit + 2
end
if maze[y][x + 1] == MAZE_WALL then
if maze[y][x + 1] == Tile.wall then
bit = bit + 4
end
if maze[y + 1] ~= nil and maze[y + 1][x] == MAZE_WALL then
if maze[y + 1] ~= nil and maze[y + 1][x] == Tile.wall then
bit = bit + 8
end
@@ -136,7 +135,7 @@ function mazeutils.drawMaze(maze, l, t, w, h)
local xPos, yPos = mazeutils.tileToWorld(x, y)
if xPos >= l - padding and xPos <= l + w + padding and yPos >= t - padding and yPos <= t + h + padding then
local tileIndex = 0 -- Ground by default
if maze[y][x] == MAZE_WALL then
if maze[y][x] == Tile.wall then
tileIndex = countNeighboringWalls(x, y)
end