character-rework #7
| @ -27,8 +27,8 @@ end | ||||
| function mapBehavior:followPath(path) | ||||
|     if path:is_empty() then return end | ||||
|     self.position = self.displayedPosition | ||||
|     self.owner:try(Tree.behaviors.animated, function(animated) | ||||
|         animated:play("run", true) | ||||
|     self.owner:try(Tree.behaviors.sprite, function(sprite) | ||||
|         sprite:play("run", true) | ||||
|     end) | ||||
|     self.path = path; | ||||
|     ---@type Vec3 | ||||
| @ -41,12 +41,12 @@ end | ||||
| function mapBehavior:runTo(target) | ||||
|     self.t0 = love.timer.getTime() | ||||
|     self.runTarget = target | ||||
|     self.owner:try(Tree.behaviors.animated, | ||||
|         function(animated) | ||||
|     self.owner:try(Tree.behaviors.sprite, | ||||
|         function(sprite) | ||||
|             if target.x < self.position.x then | ||||
|                 animated.side = Tree.behaviors.animated.LEFT | ||||
|                 sprite.side = Tree.behaviors.sprite.LEFT | ||||
|             elseif target.x > self.position.x then | ||||
|                 animated.side = Tree.behaviors.animated.RIGHT | ||||
|                 sprite.side = Tree.behaviors.sprite.RIGHT | ||||
|             end | ||||
|         end | ||||
|     ) | ||||
| @ -63,8 +63,8 @@ function mapBehavior:update(dt) | ||||
|                 self:runTo(self.path:peek_front()) | ||||
|                 self.path:pop_front() | ||||
|             else -- мы добежали до финальной цели | ||||
|                 self.owner:try(Tree.behaviors.animated, function(animated) | ||||
|                     animated:play("idle", true) | ||||
|                 self.owner:try(Tree.behaviors.sprite, function(sprite) | ||||
|                     sprite:play("idle", true) | ||||
|                 end) | ||||
|                 self.runTarget = nil | ||||
|             end | ||||
|  | ||||
| @ -1,21 +1,21 @@ | ||||
| local anim8 = require "lib.utils.anim8" | ||||
| 
 | ||||
| --- @class AnimatedBehavior : Behavior | ||||
| --- @class SpriteBehavior : Behavior | ||||
| --- @field animationTable table<string, table> | ||||
| --- @field animationGrid table | ||||
| --- @field state "idle"|"run"|"hurt"|"attack" | ||||
| --- @field side 1|-1 | ||||
| local animated = {} | ||||
| animated.__index = animated | ||||
| animated.id = "animated" | ||||
| animated.dependencies = { Tree.behaviors.map } | ||||
| animated.LEFT = -1 | ||||
| animated.RIGHT = 1 | ||||
| local sprite = {} | ||||
| sprite.__index = sprite | ||||
| sprite.id = "sprite" | ||||
| sprite.dependencies = { Tree.behaviors.map } | ||||
| sprite.LEFT = -1 | ||||
| sprite.RIGHT = 1 | ||||
| --- Скорость между кадрами в анимации | ||||
| animated.ANIMATION_SPEED = 0.1 | ||||
| sprite.ANIMATION_SPEED = 0.1 | ||||
| 
 | ||||
| function animated.new(spriteDir) | ||||
|     local anim = setmetatable({}, animated) | ||||
| function sprite.new(spriteDir) | ||||
|     local anim = setmetatable({}, sprite) | ||||
|     anim.animationTable = {} | ||||
|     anim.animationGrid = {} | ||||
| 
 | ||||
| @ -27,19 +27,19 @@ function animated.new(spriteDir) | ||||
|     end | ||||
| 
 | ||||
|     anim.state = "idle" | ||||
|     anim.side = animated.RIGHT | ||||
|     anim.side = sprite.RIGHT | ||||
|     anim:play("idle") | ||||
|     return anim | ||||
| end | ||||
| 
 | ||||
| function animated:update(dt) | ||||
| function sprite:update(dt) | ||||
|     local anim = self.animationTable[self.state] or self.animationTable["idle"] or nil | ||||
| 
 | ||||
|     if not anim then return end | ||||
|     anim:update(dt) | ||||
| end | ||||
| 
 | ||||
| function animated:draw() | ||||
| function sprite:draw() | ||||
|     if not self.animationTable[self.state] or not Tree.assets.files.sprites.character[self.state] then return end | ||||
| 
 | ||||
|     self.owner:try(Tree.behaviors.map, | ||||
| @ -57,9 +57,9 @@ end | ||||
| 
 | ||||
| --- @param state string | ||||
| --- @param loop boolean | nil | ||||
| function animated:play(state, loop) | ||||
| function sprite:play(state, loop) | ||||
|     if not self.animationGrid[state] then | ||||
|         return print("[AnimatedBehavior]: no animation for '" .. state .. "'") | ||||
|         return print("[SpriteBehavior]: no animation for '" .. state .. "'") | ||||
|     end | ||||
|     self.animationTable[state] = anim8.newAnimation(self.animationGrid[state], self.ANIMATION_SPEED, function() | ||||
|         if not loop then self:play("idle", true) end | ||||
| @ -67,4 +67,4 @@ function animated:play(state, loop) | ||||
|     self.state = state | ||||
| end | ||||
| 
 | ||||
| return animated | ||||
| return sprite | ||||
| @ -30,7 +30,7 @@ local function spawn(name, template, spriteDir, position, size, level) | ||||
| 
 | ||||
|     char:addBehavior { | ||||
|         Tree.behaviors.map.new(position, size), | ||||
|         Tree.behaviors.animated.new(spriteDir), | ||||
|         Tree.behaviors.sprite.new(spriteDir), | ||||
|         Tree.behaviors.spellcaster.new() | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -13,4 +13,4 @@ Tree.level                 = (require "lib.level.level").new("procedural", "flow | ||||
| Tree.behaviors             = {}                                                             --- @todo написать нормальную загрузку поведений | ||||
| Tree.behaviors.map         = require "lib.character.behaviors.map" | ||||
| Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster" | ||||
| Tree.behaviors.animated    = require "lib.character.behaviors.animated" | ||||
| Tree.behaviors.sprite      = require "lib.character.behaviors.sprite" | ||||
|  | ||||
							
								
								
									
										1
									
								
								main.lua
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								main.lua
									
									
									
									
									
								
							| @ -11,6 +11,7 @@ end | ||||
| 
 | ||||
| function love.load() | ||||
|     character.spawn("Hero", "warrior", Tree.assets.files.sprites.character) | ||||
|     character.spawn("Hero", "warrior", Tree.assets.files.sprites.character, Vec3 { 3, 3 }) | ||||
|     love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true }) | ||||
| end | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user