diff --git a/lib/character.lua b/lib/character/character.lua similarity index 89% rename from lib/character.lua rename to lib/character/character.lua index e3102cd..18864b3 100644 --- a/lib/character.lua +++ b/lib/character/character.lua @@ -9,13 +9,10 @@ local characterId = 1 --- @todo Композиция лучше наследования, но не до такой же степени! Надо отрефакторить и избавиться от сотни полей в таблице --- @class Character --- @field id integer ---- @field name string --- @field animationTable table --- @field state "idle"|"run"|"attack"|"hurt" ---- @field stats table +--- @field info Info --- @field player table ---- @field skills table ---- @field class "warrior"|"mage" --- @field position Vec3 --- @field latestPosition Vec3 позиция, где character был один тик назад --- @field runTarget Vec3 точка, в которую в данный момент бежит персонаж @@ -25,9 +22,10 @@ character.__index = character --- Создаёт персонажа, которым будет управлять или игрок или компьютер --- @param name string +--- @param template ClassTemplate --- @param spriteDir table --- @param level? integer -local function spawn(name, spriteDir, level) +local function spawn(name, template, spriteDir, level) local animationGrid = {} -- n: name; i: image for n, i in pairs(spriteDir) do @@ -37,7 +35,6 @@ local function spawn(name, spriteDir, level) end local char = { - name = name, animationTable = {} } @@ -58,14 +55,7 @@ local function spawn(name, spriteDir, level) char.state = "idle" end) - char.stats = {} - --- @todo придумать формулу расчёта статов относительно уровня - char.stats.level = 1 - - char.stats.initiative = 10 - char.stats.damage = 5 - char.stats.defence = 0 - char.stats.hp = 30 + char.info = (require "lib/character/info").new(name, template) char = setmetatable(char, character) Tree.level.characters[char.id] = char diff --git a/lib/character/class.lua b/lib/character/class.lua new file mode 100644 index 0000000..0f3a6f5 --- /dev/null +++ b/lib/character/class.lua @@ -0,0 +1,17 @@ +--- @alias ClassTemplate "warrior"|"mage"|"archer" + +--- @class Class +--- @field skills table +--- @field stats Stats +local class = {} + +--- @param template ClassTemplate +--- @param level? integer +local function new(template, level) + return setmetatable({ + stats = (require "lib/character/stats").fromTemplate(template), + skills = {} + }, class) +end + +return { new = new } diff --git a/lib/character/info.lua b/lib/character/info.lua new file mode 100644 index 0000000..6b8ba68 --- /dev/null +++ b/lib/character/info.lua @@ -0,0 +1,16 @@ +--- @class Info +--- @field name string +--- @field class Class +local info = {} + +--- @param name string +--- @param classTemplate ClassTemplate +--- @param level? integer +local function new(name, classTemplate, level) + return setmetatable({ + name = name, + class = (require 'lib/character/class').new(classTemplate, level) + }, info) +end + +return { new = new } diff --git a/lib/character/stats.lua b/lib/character/stats.lua new file mode 100644 index 0000000..f535b28 --- /dev/null +++ b/lib/character/stats.lua @@ -0,0 +1,45 @@ +--- @class Stats +--- @field level integer +--- @field initiative integer +--- @field hp integer +--- @field damage integer +--- @field defence integer +local stats = {} + +--- @param level? integer +local function new(level) + +end + +--- creates stats from character template (like warrior etc etc) +--- +--- TODO: написать скалирование по уровню +--- @param template ClassTemplate +--- @param level? integer +local function fromTemplate(template, level) + local tempStats = {} + if template == "warrior" then + tempStats = { + hp = 30, + initiative = 10, + damage = 5, + defence = 10, + } + elseif template == "mage" then + tempStats = { + hp = 15, + initiative = 8, + damage = 8, + defence = 0, + } + elseif template == "archer" then + tempStats = { + hp = 20, + initiative = 12, + damage = 5, + defence = 5, + } + end +end + +return { new = new, fromTemplate = fromTemplate } diff --git a/main.lua b/main.lua index fb2d526..c126c4a 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,6 @@ -- CameraLoader = require 'lib/camera' -local character = require "lib/character" +local character = require "lib/character/character" require "lib/tree" function love.conf(t) @@ -8,7 +8,7 @@ function love.conf(t) end function love.load() - local char = character.spawn("Hero", Tree.assets.files.sprites.character) + local char = character.spawn("Hero", "warrior", Tree.assets.files.sprites.character) char:runTo(Vec3 { 5, 5 }) -- PlayerFaction.characters = { Hero1, Hero2 }