update grid🤡🤡🤡:clown:
Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
This commit is contained in:
parent
7587395d2c
commit
0cf74b0eff
@ -73,6 +73,16 @@ function camera:attach()
|
||||
love.graphics.translate(-self.position.x, -self.position.y)
|
||||
end
|
||||
|
||||
--- @param position Vec3
|
||||
function camera:toWorldPosition(position)
|
||||
local wx, wy = love.window.getMode()
|
||||
position = Vec3 {
|
||||
(position.x - wx / 2) / Tree.level.camera.pixelsPerMeter / Tree.level.camera.scale + Tree.level.camera.position.x,
|
||||
(position.y - wy / 2) / Tree.level.camera.pixelsPerMeter / Tree.level.camera.scale + Tree.level.camera.position.y
|
||||
}
|
||||
return position
|
||||
end
|
||||
|
||||
function camera:detach()
|
||||
love.graphics.pop()
|
||||
end
|
||||
|
||||
@ -4,7 +4,10 @@ require 'lib/vec3'
|
||||
--- Скорость между кадрами в анимации
|
||||
local ANIMATION_SPEED = 0.1
|
||||
|
||||
local characterId = 1
|
||||
|
||||
--- @class Character
|
||||
--- @field id integer
|
||||
--- @field name string
|
||||
--- @field animationTable table<string, table>
|
||||
--- @field state "idle"|"run"|"attack"|"hurt"
|
||||
@ -35,8 +38,11 @@ local function spawn(name, spriteDir, level)
|
||||
animationTable = {}
|
||||
}
|
||||
|
||||
char.id = characterId
|
||||
characterId = characterId + 1
|
||||
|
||||
char.position = Vec3({})
|
||||
char.size = Vec3({})
|
||||
char.size = Vec3({ 1, 1 })
|
||||
|
||||
char.state = "idle"
|
||||
|
||||
@ -58,10 +64,8 @@ local function spawn(name, spriteDir, level)
|
||||
char.stats.defence = 0
|
||||
char.stats.hp = 30
|
||||
|
||||
print(char)
|
||||
|
||||
char = setmetatable(char, character)
|
||||
table.insert(Tree.level.characters, char)
|
||||
Tree.level.characters[char.id] = char
|
||||
return char
|
||||
end
|
||||
|
||||
|
||||
@ -8,7 +8,6 @@ local function control(device, key)
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
local keymap = {
|
||||
cameraMoveUp = control("key", "w"),
|
||||
cameraMoveLeft = control("key", "a"),
|
||||
|
||||
47
lib/grid.lua
47
lib/grid.lua
@ -1,14 +1,47 @@
|
||||
local utils = require "lib/utils"
|
||||
|
||||
---Generates an empty grid
|
||||
---@param width number
|
||||
---@param height number
|
||||
--- @class Grid
|
||||
local grid = {}
|
||||
grid.__index = grid
|
||||
|
||||
--- Adds a character id to the grid
|
||||
--- @param character Character
|
||||
function grid:add(character)
|
||||
local centerX, centerY = character.position.x, character.position.y
|
||||
local sizeX, sizeY = character.size.x, character.size.y
|
||||
|
||||
for y = centerY, centerY + sizeY - 1 do
|
||||
for x = centerX, centerX + sizeX - 1 do
|
||||
grid[x][y] = character.id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Removes a character id from the grid
|
||||
--- @param character Character
|
||||
function grid:remove(character)
|
||||
local centerX, centerY = character.position.x, character.position.y
|
||||
local sizeX, sizeY = character.size.x, character.size.y
|
||||
|
||||
for y = centerY, centerY + sizeY - 1 do
|
||||
for x = centerX, centerX + sizeX - 1 do
|
||||
grid[x][y] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Generates an empty grid
|
||||
--- @param width number
|
||||
--- @param height number
|
||||
--- @return Grid
|
||||
local function generateGrid(width, height)
|
||||
local grid = utils.generateList(width, function(i)
|
||||
return utils.generateList(height, function(i)
|
||||
return {}
|
||||
local g = utils.generateList(width, function(_)
|
||||
return utils.generateList(height, function(_)
|
||||
return nil
|
||||
end)
|
||||
end)
|
||||
|
||||
return grid
|
||||
return setmetatable(g, grid)
|
||||
end
|
||||
|
||||
return { new = generateGrid }
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
local utils = require "lib/utils"
|
||||
local camera = require "lib/camera"
|
||||
|
||||
--- @class Level
|
||||
--- @field characters Character[]
|
||||
--- @field positionGrid Grid
|
||||
--- @field selected table
|
||||
--- @field camera Camera
|
||||
local level = {}
|
||||
level.__index = level
|
||||
@ -10,7 +11,8 @@ level.__index = level
|
||||
local function new()
|
||||
return setmetatable({
|
||||
characters = {},
|
||||
camera = camera.new()
|
||||
positionGrid = (require "lib/grid").new(30, 30), -- magic numbers for testing purposes only
|
||||
camera = (require "lib/camera").new()
|
||||
}, level)
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user