From 8607399d16cd37d4499c0c867e0d73aefc79c32a Mon Sep 17 00:00:00 2001 From: lustlion Date: Fri, 29 Oct 2021 04:15:53 +0200 Subject: [PATCH] YAY - Mask functionality - Rehandled Animations (now consistent) --- data/scripts.lua | 1 + data/scripts/animation.lua | 160 +++++++++++-------------------- data/scripts/debug.lua | 9 +- data/scripts/entities/arrow.lua | 13 ++- data/scripts/entities/kupo.lua | 37 ++++--- data/scripts/entities/player.lua | 53 +++++++--- data/scripts/entity.lua | 9 +- data/scripts/enums.lua | 2 +- data/scripts/lights.lua | 3 - main.lua | 5 +- to_do.txt | 26 +++-- 11 files changed, 144 insertions(+), 174 deletions(-) diff --git a/data/scripts.lua b/data/scripts.lua index b284590..223faa5 100644 --- a/data/scripts.lua +++ b/data/scripts.lua @@ -7,6 +7,7 @@ require "data/scripts/in_out" json = require "data/scripts/json" -- classes require "data/scripts/entity" +require "data/scripts/animation" require "data/scripts/collision" require "data/scripts/level" -- data diff --git a/data/scripts/animation.lua b/data/scripts/animation.lua index a7dc5d2..5ebe669 100644 --- a/data/scripts/animation.lua +++ b/data/scripts/animation.lua @@ -1,54 +1,39 @@ -function Animation:Draw() - if self.sprite ~= nil then - local relative_position_x = self.pos.x - Camera.pos.x - local relative_position_y = self.pos.y - Camera.pos.y - local origin_compensation_x = - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) - local origin_compensation_y = - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) - local dimensions_x = self.sprite_scale.x * self.sprite_flip.x - local dimensions_y = self.sprite_scale.y * self.sprite_flip.y - love.graphics.draw( - self.sprite, - relative_position_x + origin_compensation_x * dimensions_x, - relative_position_y + origin_compensation_y * dimensions_y, - self.sprite_rotation, - self.sprite_scale.x * self.sprite_flip.x, - self.sprite_scale.y * self.sprite_flip.y - ) - if debug_collision then - love.graphics.setColor(1, 0, 0) - love.graphics.circle( "line", relative_position_x, relative_position_y, 2 ) - love.graphics.setColor(0, 1 ,0) - love.graphics.circle( "line", - relative_position_x + origin_compensation_x * dimensions_x, - relative_position_y + origin_compensation_y * dimensions_y, - 2 - ) - end - love.graphics.setColor(1, 1 ,1) +Animation = {} + +function Animation:New(anim_data) + local o = {} + o.path = anim_data.path + o.frames = anim_data.frames + o.speed = anim_data.speed + o.imgs = anim_data.imgs + o.subframe = 0 + o.frame = 1 + + setmetatable(o, self) + self.__index = self + return o +end + +function Animation:ChangeTo(anim_data) + if anim_data.path == self.path + then + return self + else + return Animation:New(anim_data) end end -function Animation:New(anim,frames,speed) - local anim_data = { - frame = 1, - subframe = 1, - path = anim.path, - frames = anim.frames, - speed = anim.speed, - imgs = anim.imgs - } - self.animations[#self.animations+1] = anim_data - - return self.animations[#self.animations] -end - -function DrawAnimationFrame(animation, frame, x, y, rotate, sx, sy) +-- to manually handle what frame +function Animation:DrawFrame(frame, x, y, rotate, sx, sy) + if frame > self.frames then + frame = self.frames + end local x = x or 0 local y = y or 0 local sx = sx or 1 local sy = sy or 1 love.graphics.draw( - animation.imgs[frame], + self.imgs[frame], x - Camera.pos.x, y - Camera.pos.y, rotate, @@ -57,71 +42,34 @@ function DrawAnimationFrame(animation, frame, x, y, rotate, sx, sy) ) end -function DrawAnimation(animation, x, y, rotate, sx, sy) +-- to linearly animate +function Animation:Animate() + -- try to animate + self.subframe = self.subframe + current_dt + + if self.subframe > self.speed then + self.frame = self.frame + 1 + self.subframe = self.subframe - self.speed + end + + -- cycle + if self.frame >= self.frames+1 then + self.frame = self.frame - self.frames + end +end + +-- to draw the current frame +function Animation:Draw(x, y, rotate, sx, sy) local x = x or 0 local y = y or 0 local sx = sx or 1 local sy = sy or 1 - if game_paused ~= true then - -- try to animate - animation.subframe = animation.subframe + current_dt - - if animation.subframe >= animation.speed then - animation.frame = animation.frame + 1 - animation.subframe = animation.subframe - animation.speed - end - - -- cycle - if animation.frame >= animation.frames+1 then - animation.frame = animation.frame - animation.frames - end - end - love.graphics.draw( - animation.imgs[animation.frame], - x - Camera.pos.x, - y - Camera.pos.y, - rotate, - sx, - sy - ) + love.graphics.draw( + self.imgs[self.frame], + x, + y, + rotate, + sx, + sy + ) end - -function Entity:Animate() - if game_paused ~= true and self.anim.path ~= nil then - -- try to animate - self.anim.subframe = self.anim.subframe + current_dt - - if self.anim.subframe >= self.anim.speed then - self.anim.frame = self.anim.frame + 1 - self.anim.subframe = self.anim.subframe - self.anim.speed - end - - -- cycle - if self.anim.frame >= self.anim.frames+1 then - self.anim.frame = self.anim.frame - self.anim.frames - end - - -- change - self.sprite = self.anim.imgs[self.anim.frame] - end -end - -function Entity:LoadAnimation(anim,frames,speed) - if self.anim.path ~= anim and self.anim.path ~= anim.path then - if frames ~= nil and speed ~= nil then - self.anim.path = anim or nil - self.anim.frames = frames or 4 - self.anim.speed = speed or frames - else - self.anim.path = anim.path - self.anim.frames = anim.frames - self.anim.speed = anim.speed - end - - self.anim.imgs = anim.imgs - end -end - -require "data/scripts/entities/kupo" -require "data/scripts/entities/arrow" -require "data/scripts/entities/player" diff --git a/data/scripts/debug.lua b/data/scripts/debug.lua index 8c0fa0a..7a48d0a 100644 --- a/data/scripts/debug.lua +++ b/data/scripts/debug.lua @@ -1,11 +1,11 @@ function DebugUI() -for _, light in pairs(Lights) do +for _, light in pairs(Lights) do love.graphics.print(light.pos.x,light.pos.x,light.pos.y) love.graphics.print(light.pos.y,light.pos.x,light.pos.y+20) love.graphics.print(light.pos.x,light.pos.x,light.pos.y+40) -end - +end + love.graphics.print("time: "..fps_total..", fps: "..fps_draw..", frametime: "..math.floor(current_dt* 1000).."ms", 10*textScale, 0*textScale, 0, textScale) love.graphics.setColor(1,1,1) @@ -14,8 +14,7 @@ love.graphics.print("time: "..fps_total..", fps: "..fps_draw..", frametime: "..m love.graphics.print("position: {"..main_Player.pos.x..", "..main_Player.pos.y.."}",10*textScale,60*textScale, 0, textScale) love.graphics.print("velocity: {"..main_Player.vel.x..", "..main_Player.vel.y.."}",10*textScale,80*textScale, 0, textScale) love.graphics.print("scale: {"..main_Player.sprite_scale.x..", "..main_Player.sprite_scale.y.."}",10*textScale,100*textScale, 0, textScale) - love.graphics.print("anim: "..tostring(main_Player.anim.path)..", anim.speed: "..main_Player.anim.speed,10*textScale,120*textScale, 0, textScale) - love.graphics.print("states: \"isOnGround\": "..tostring(main_Player.isOnGround),10*textScale,140*textScale, 0, textScale) + love.graphics.print("states: \"isOnGround\": "..tostring(main_Player.isOnGround),10*textScale,120*textScale, 0, textScale) love.graphics.print("[Camera]",10*textScale,160*textScale, 0, textScale) love.graphics.print("position: {"..Camera.pos.x..", "..Camera.pos.y.."}",10*textScale,180*textScale, 0, textScale) diff --git a/data/scripts/entities/arrow.lua b/data/scripts/entities/arrow.lua index 9e148ee..439a5e0 100644 --- a/data/scripts/entities/arrow.lua +++ b/data/scripts/entities/arrow.lua @@ -13,7 +13,10 @@ Arrow = Entity:New(x,y) o.sprite_offset = {x = 13, y = 1} o.stuck = false o.illuminated = true - + + -- animations + o.body = Animation:New(animation.kupo.arrow) + setmetatable(o, self) self.__index = self table.insert(LoadedEntities,o) @@ -25,7 +28,13 @@ function Arrow:Smart() end function Arrow:HandleAnimation() - self:LoadAnimation(animation.kupo.arrow) + self.body:Draw( + self.pos.x - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x, + self.pos.y - Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y, + self.sprite_rotation, + self.sprite_scale.x * self.sprite_flip.x, + self.sprite_scale.y * self.sprite_flip.y + ) end function Arrow:DoPhysics() diff --git a/data/scripts/entities/kupo.lua b/data/scripts/entities/kupo.lua index 5291d15..0ca1b62 100644 --- a/data/scripts/entities/kupo.lua +++ b/data/scripts/entities/kupo.lua @@ -8,8 +8,12 @@ Kupo = Entity:New(x,y) o.range = 200 o.target = {x = x, y = y} o.sprite_offset = {x = 8, y = 5} - -- kupo bow - o.bow = self:NewAnimation(animation.kupo.bow) + + -- animations + o.body = Animation:New(animation.kupo.body) + o.bow = Animation:New(animation.kupo.bow) + + -- bow o.bow_flip = 1 o.bow_rotation = 0 o.bow_frame = 1 @@ -19,7 +23,7 @@ Kupo = Entity:New(x,y) o.bow_frames = 6 o.bow_extraframes = 18 o.bow_aim_frames = 8 - o.hostile = false + o.hostile = true o.lightRange = o.range/10 o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange) @@ -131,29 +135,24 @@ end function Kupo:HandleAnimation() -- flip sprite to look in the direction is moving if self.vel.x ~= 0 then self.sprite_flip.x = math.sign(self.vel.x) end - self:LoadAnimation(animation.kupo.body) + + self.body:Animate() + self.body:Draw( + self.pos.x - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x, + self.pos.y - Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y, + self.sprite_rotation, + self.sprite_scale.x * self.sprite_flip.x, + self.sprite_scale.y * self.sprite_flip.y + ) + if self.draw_bow == true then - DrawAnimationFrame( - self.bow, + self.bow:DrawFrame( math.min(self.bow_frame,self.bow_frames), self.pos.x + ( 8 * math.sin(self.bow_rotation)), self.pos.y + (2 - 6 * math.cos(self.bow_rotation)), self.bow_rotation ) end -if debug_collision then - love.graphics.setColor(1,0,0) - love.graphics.line( - self.pos.x - Camera.pos.x, - self.pos.y - Camera.pos.y, - self.target.x - Camera.pos.x, - self.target.y - Camera.pos.y - ) - love.graphics.circle( "line", self.pos.x - Camera.pos.x, self.pos.y - Camera.pos.y, self.range ) - love.graphics.setColor(1,1,1) - love.graphics.print(self.bow_rotation, self.pos.x, self.pos.y+30) - love.graphics.print(self.angle, self.pos.x, self.pos.y+50) - end end function Kupo:DoPhysics() diff --git a/data/scripts/entities/player.lua b/data/scripts/entities/player.lua index 5d60d3c..82d0ace 100644 --- a/data/scripts/entities/player.lua +++ b/data/scripts/entities/player.lua @@ -13,7 +13,7 @@ } -- constants - o.acc = 45 + o.acc = 45 o.friction = 20 o.gravity = 9.81 o.climbHeight = 4 @@ -31,11 +31,12 @@ o.canJump = true o.canFall = true o.canFriction = true - - -- sprite + o.mask_type = animation.moth_mask + -- sprite o.sprite_offset = {x = 8, y = 16} - o.target_offset = {x = 0, y = 12} - + o.target_offset = {x = 0, y = 12} + o.body = Animation:New(animation.nancy.idle) + o.mask = Animation:New(animation.moth_mask.idle) -- lights @@ -85,27 +86,53 @@ function Player:Smart() end function Player:HandleAnimation() - -- flip sprite to look in the direction is moving if self.vel.x ~= 0 then self.sprite_flip.x = math.sign(self.vel.x) end -- animation manager if self.isOnLadder then - self:LoadAnimation(animation.nancy.jump) + self.body = self.body:ChangeTo(animation.nancy.jump) + self.mask = self.mask:ChangeTo(self.mask_type.jump) elseif self.isOnGround == 0 and self.isJumping and self.vel.y > 1.25 then - self:LoadAnimation(animation.nancy.fall) + self.body = self.body:ChangeTo(animation.nancy.fall) + self.mask = self.mask:ChangeTo(self.mask_type.fall) elseif self.isOnGround == 0 and self.vel.y < 0 then - self:LoadAnimation(animation.nancy.jump) + self.body = self.body:ChangeTo(animation.nancy.jump) + self.mask = self.mask:ChangeTo(self.mask_type.jump) elseif self.vel.x ~= 0 then - self:LoadAnimation(animation.nancy.run) + self.body = self.body:ChangeTo(animation.nancy.run) + self.mask = self.mask:ChangeTo(self.mask_type.run) else - self:LoadAnimation(animation.nancy.idle) + self.body = self.body:ChangeTo(animation.nancy.idle) + self.mask = self.mask:ChangeTo(self.mask_type.idle) end -- special case: idle animation gets slower by time - if self.anim_path == animation.nancy.idle.path then - if self.anim_speed < 0.5 then self.anim_speed = self.anim_speed + 0.001 end + if self.body.anim_path == animation.nancy.idle.path then + if self.body.anim_speed < 0.5 then self.body.anim_speed = self.body.anim_speed + 0.001 end end + + self.body:Animate() + self.body:Draw( + self.pos.x - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x, + self.pos.y - Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y, + self.sprite_rotation, + self.sprite_scale.x * self.sprite_flip.x, + self.sprite_scale.y * self.sprite_flip.y + ) + self.mask:Draw( + self.pos.x - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x, + self.pos.y - Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y, + self.sprite_rotation, + self.sprite_scale.x * self.sprite_flip.x, + self.sprite_scale.y * self.sprite_flip.y + ) + + --[[ + love.graphics.print(self.body.frame .. "/".. self.body.frames.." - "..self.body.subframe.."/"..self.body.speed.." ("..current_dt..")") + love.graphics.print(animation.nancy.idle.path,0,20) + love.graphics.print(self.body.path,0,30) + ]] end function Player:DoPhysics() diff --git a/data/scripts/entity.lua b/data/scripts/entity.lua index ad55084..d5e807f 100644 --- a/data/scripts/entity.lua +++ b/data/scripts/entity.lua @@ -6,11 +6,7 @@ function Entity:New(x,y) o.pos = {x = x, y = y} o.vel = {x = 0, y = 0} o.class = "Entity" - o.anim = {} - o.anim.subframe = 0 - o.anim.frame = 1 - o.anim.imgs = {} - o.animations = {} + o.sprite_offset = {x = 0, y = 0} o.sprite_scale = {x = 1, y = 1} o.sprite_rotation = math.rad(0) @@ -25,7 +21,7 @@ function Entity:Move(target, speed) -- target = {tx int, ty int} / speed = int end -function Entity:Draw() +--[[function Entity:Draw() if self.sprite ~= nil then local relative_position_x = self.pos.x - Camera.pos.x local relative_position_y = self.pos.y - Camera.pos.y @@ -148,6 +144,7 @@ function Entity:LoadAnimation(anim,frames,speed) self.anim.imgs = anim.imgs end end +]]-- require "data/scripts/entities/kupo" require "data/scripts/entities/arrow" diff --git a/data/scripts/enums.lua b/data/scripts/enums.lua index 913d5e3..6c0557e 100644 --- a/data/scripts/enums.lua +++ b/data/scripts/enums.lua @@ -17,7 +17,7 @@ animation = { speed = 1 } }, - nancy_moth_mask = { + moth_mask = { idle = { path = "assets/characters/nancy/moth_mask/idle", frames = 4, diff --git a/data/scripts/lights.lua b/data/scripts/lights.lua index b9b3c4f..e7c7660 100644 --- a/data/scripts/lights.lua +++ b/data/scripts/lights.lua @@ -52,9 +52,6 @@ function DoLights() )]] end for _, enty in pairs(LoadedEntities) do - if enty.illuminated == true then - enty:Draw() - end end love.graphics.setColor(0,0,0,0.5) for _, light in pairs(Lights) do diff --git a/main.lua b/main.lua index 4c73ff6..780a6c8 100644 --- a/main.lua +++ b/main.lua @@ -35,7 +35,6 @@ function love.load() table.insert(LoadedEntities,Kupo:New(700,150)) table.insert(LoadedEntities,Kupo:New(800,150)) main_Player.sprite = love.graphics.newImage("assets/characters/nancy/idle1.png") - main_Player:LoadAnimation(animation.nancy.idle) end function love.update(dt) @@ -123,8 +122,6 @@ function love.draw() LevelDisplayBackground() for _, enty in pairs(LoadedEntities) do enty:HandleAnimation() - enty:Animate() - enty:Draw() end LevelDisplayForeground() @@ -140,7 +137,7 @@ function love.draw() love.graphics.setColor(1,1,1,1) love.graphics.setCanvas() love.graphics.scale(1,1) - DrawDarkness() + --DrawDarkness() -- HUD -- Scale control textScale = 0.5 diff --git a/to_do.txt b/to_do.txt index 5f05ffb..75cc1f8 100644 --- a/to_do.txt +++ b/to_do.txt @@ -1,15 +1,11 @@ -MAKE LIGHTING SYSTEM - Updates: - LOVE HAS CANVASES!!! - -MAKE ARROW CANVAS SO THAT ARROWS WHEN STUCK CAN BE STORED AS IMAGE AND ARE NOT LAGGY!!! - -DRAW GIANT ANIMALS AS POWER SOURCE - -DO FAIRY - -DO FLIES - -DO DAMAGE TO PLAYER - -DO UI \ No newline at end of file +(x) MAKE LIGHTING SYSTEM +(X) REHANDLE ANIMATIONS +(X) 3 COLOR PALETTE (GOLD) +(X) MASKS FUNCTIONALITY +( ) MAKE ARROW CANVAS SO THAT ARROWS WHEN STUCK CAN BE STORED AS IMAGE AND ARE NOT LAGGY!!! +( ) DO GODS +( ) DO DAMAGE TO PLAYER +( ) DO FAIRY +( ) DO FLIES +( ) DO UI +( ) MAKE PHYSICS CONSISTENT