Hexadecimal colors, particle data, cleaned a bit of entities, added player dash animation particles

This commit is contained in:
lustlion 2022-01-20 00:53:59 +01:00
parent d96d5dd56d
commit ccc059f40d
15 changed files with 149 additions and 456 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 B

View File

@ -3,8 +3,8 @@ require "data/scripts/enums"
-- support functions
require "data/scripts/math"
require "data/scripts/hex"
require "data/scripts/in_out"
json = require "data/scripts/json"
-- classes
require "data/scripts/entity"
require "data/scripts/animation"

View File

@ -2,12 +2,13 @@ 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
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
@ -44,18 +45,20 @@ end
-- to linearly animate
function Animation:Animate()
-- try to animate
self.subframe = self.subframe + current_dt
if self.speed ~= 0 then
-- 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
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
-- cycle
if self.frame >= self.frames+1 then
self.frame = self.frame - self.frames
end
end
end
-- to draw the current frame

View File

@ -16,7 +16,7 @@ Arrow = Entity:New(x,y)
-- animations
o.body = Animation:New(animation.kupo.arrow)
table.insert(LoadedEntities,o)
o.id = #LoadedEntities
@ -25,10 +25,6 @@ Arrow = Entity:New(x,y)
return o
end
function Arrow:Smart()
end
function Arrow:HandleAnimation()
self:Draw(self.body)
end

View File

@ -23,9 +23,6 @@ function Decoration:New(x,y,animation,lightRange)
return o
end
function Decoration:Smart()
end
function Decoration:HandleAnimation()
self.body:Animate()
self:Draw(self.body)

View File

@ -42,7 +42,14 @@ function Fairy:Smart()
self.vel.x = math.cos(angle)*self.speed*distance/(8*game.scale)
self.vel.y = math.sin(angle)*self.speed*distance/(8*game.scale)
end
Particle:New(self.pos.x,self.pos.y,animation.particle.fairy,angle-math.rad(180),self.speed*distance/(16*game.scale))
local particle_data = {
animation = animation.particle.fairy,
direction = angle-math.rad(180),
speed = self.speed*distance/(16*game.scale)
}
Particle:New(self.pos.x,self.pos.y,particle_data)
end
function Fairy:HandleAnimation()

View File

@ -1,33 +1,55 @@
Particle = Entity:New(x,y)
function Particle:New(x,y,animation,rotation,speed)
function Particle:New(x,y,particle_data)
local o = Entity:New(x,y)
o.pos = {x = x, y = y}
o.speed = speed or 0
o.sprite_rotation = rotation or 0
o.speed = particle_data.speed or 0
o.direction = particle_data.direction or o.direction
o.sprite_rotation = particle_data.sprite_rotation or o.sprite_rotation
o.sprite_offset = particle_data.sprite_offset or o.sprite_offset
o.sprite_scale = particle_data.sprite_scale or o.sprite_scale
o.sprite_tint = particle_data.sprite_tint or o.sprite_tint
o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha
o.sprite_flip = particle_data.sprite_flip or o.sprite_flip
o.animation_active = particle_data.animation_active or false
o.time = 0.5
o.timer = 0
o.vel = {
x = o.speed * math.cos(o.sprite_rotation),
y = o.speed * math.sin(o.sprite_rotation)
x = o.speed * math.cos(o.direction),
y = o.speed * math.sin(o.direction)
}
-- animations
o.body = Animation:New(animation)
table.insert(LoadedEntities,o)
o.id = #LoadedEntities
o.body = Animation:New(particle_data.animation)
o:centerOffset(o.body)
if not o.animation_active then
o.body.speed = 0
end
table.insert(LoadedParticles,o)
o.id = #LoadedParticles
setmetatable(o, self)
self.__index = self
return o
end
function Particle:Smart()
function Particle:Kill()
if self.id ~= nil then
for _, e in pairs(LoadedParticles) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(LoadedParticles,self.id)
end
self = nil
end
function Particle:HandleAnimation()

View File

@ -122,7 +122,19 @@ function Player:DoPhysics()
self.isOnGround = false
self.dashTimer = self.dashTimer - current_dt
-- DASH STATE
if self.dashTimer > 0 then
local particle_data = {
animation = self.body,
sprite_tint = HEX2RGB("#fed100"),
sprite_flip = {
x = self.sprite_flip.x,
y = self.sprite_flip.y
}
}
Particle:New(self.pos.x,self.pos.y,particle_data)
self.dashCooldownTimer = self.dashCooldownTime
self.vel.x = self.dashSpeed * math.cos(self.dashDirection)
self.vel.y = self.dashSpeed * math.sin(self.dashDirection)
@ -149,11 +161,6 @@ function Player:DoPhysics()
end
function Player:HandleAnimation()
if self.dashTimer > 0 then
self.sprite_tint = {1,1,0}
else
self.sprite_tint = {1,1,1}
end
-- flip sprite to look in the direction is moving
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end

View File

@ -5,12 +5,15 @@ function Entity:New(x,y)
o.pos = {x = x, y = y}
o.vel = {x = 0, y = 0}
o.direction = 0
o.boxCollision = {
from = {x = x, y = y},
to = {x = x, y = y},
}
o.target_offset = {x = 0, y = 0}
o.sprite_offset = {x = 0, y = 0}
o.sprite_scale = {x = 1, y = 1}
o.sprite_rotation = math.rad(0)
@ -25,6 +28,34 @@ function Entity:New(x,y)
return o
end
function Entity:Smart()
end
function Entity:Kill()
if self.id ~= nil then
for _, e in pairs(LoadedEntities) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(LoadedEntities,self.id)
end
self = nil
end
function Entity:Draw(animation)
local c1, c2, c3, a = love.graphics.getColor()
love.graphics.setColor(self.sprite_tint[1],self.sprite_tint[2],self.sprite_tint[3],self.sprite_alpha)
animation: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.setColor(c1,c2,c3,a)
end
function Entity:centerOffset(animation,x,y)
local x = x or 0
local y = y or 0
@ -43,19 +74,6 @@ function Entity:getBoundingBox(animation,left,right,top,bottom)
self.boxCollision.to.y = animation.imgs[1]:getHeight()/2 + bottom
end
function Entity:Draw(animation)
local c1, c2, c3, a = love.graphics.getColor()
love.graphics.setColor(self.sprite_tint[1],self.sprite_tint[2],self.sprite_tint[3],self.sprite_alpha)
animation: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.setColor(c1,c2,c3,a)
end
-- returns true if theres a collision at that point. also marks collisioned tile as collision true
function Entity:isCollidingAt(x,y,object)
local result = false
@ -90,18 +108,6 @@ function Entity:isCollidingAtAll(x,y)
return result
end
function Entity:Kill()
if self.id ~= nil then
for _, e in pairs(LoadedEntities) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(LoadedEntities,self.id)
end
self = nil
end
require "data/scripts/entities/kupo"
require "data/scripts/entities/arrow"
require "data/scripts/entities/decoration"

View File

@ -6,6 +6,11 @@ animation = {
path = "assets/entities/particle/fairy",
frames = 4,
speed = 1/4
},
player_dash = {
path = "assets/entities/particle/player_dash",
frames = 1,
speed = 0
}
},
fairy = {
@ -36,7 +41,7 @@ animation = {
arrow = {
path = "assets/entities/kupo/kupo_arrow",
frames = 1,
speed = 1
speed = 0
}
},
moth_mask = {

View File

@ -1,6 +1,10 @@
function GameStep()
if not do_pause then
SetCollisionFlags(main_Player)
for _, particle in pairs(LoadedParticles) do
particle:Smart()
particle:DoPhysics()
end
for _, enty in pairs(LoadedEntities) do
enty:Smart()
enty:DoPhysics()

View File

@ -11,6 +11,9 @@ function GameworldDraw()
love.graphics.setColor(1,1,1,1)
LevelDisplayBackground()
for _, particle in pairs(LoadedParticles) do
particle:HandleAnimation()
end
for _, enty in pairs(LoadedEntities) do
enty:HandleAnimation()
end

30
data/scripts/hex.lua Normal file
View File

@ -0,0 +1,30 @@
function HEX2RGB(color)
local r1 = HEX2DEX(color:sub(2,2))
local r2 = HEX2DEX(color:sub(3,3))
local g1 = HEX2DEX(color:sub(4,4))
local g2 = HEX2DEX(color:sub(5,5))
local b1 = HEX2DEX(color:sub(6,6))
local b2 = HEX2DEX(color:sub(7,7))
return {(r1*16 + r2)/255, (g1*16 + g2)/255, (b1*16 + b2)/255}
end
function HEX2DEX(hex)
if hex == "0" then return 0
elseif hex == "1" then return 1
elseif hex == "2" then return 2
elseif hex == "3" then return 3
elseif hex == "4" then return 4
elseif hex == "5" then return 5
elseif hex == "6" then return 6
elseif hex == "7" then return 7
elseif hex == "8" then return 8
elseif hex == "9" then return 9
elseif hex == "a" then return 10
elseif hex == "b" then return 11
elseif hex == "c" then return 12
elseif hex == "d" then return 13
elseif hex == "e" then return 14
elseif hex == "f" then return 15
end
end

View File

@ -1,388 +0,0 @@
--
-- json.lua
--
-- Copyright (c) 2020 rxi
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
local json = { _version = "0.1.2" }
-------------------------------------------------------------------------------
-- Encode
-------------------------------------------------------------------------------
local encode
local escape_char_map = {
[ "\\" ] = "\\",
[ "\"" ] = "\"",
[ "\b" ] = "b",
[ "\f" ] = "f",
[ "\n" ] = "n",
[ "\r" ] = "r",
[ "\t" ] = "t",
}
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
escape_char_map_inv[v] = k
end
local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end
local function encode_nil(val)
return "null"
end
local function encode_table(val, stack)
local res = {}
stack = stack or {}
-- Circular reference?
if stack[val] then error("circular reference") end
stack[val] = true
if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
local n = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
-- Encode
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
-- Treat as an object
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
end
end
local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end
local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
end
local type_func_map = {
[ "nil" ] = encode_nil,
[ "table" ] = encode_table,
[ "string" ] = encode_string,
[ "number" ] = encode_number,
[ "boolean" ] = tostring,
}
encode = function(val, stack)
local t = type(val)
local f = type_func_map[t]
if f then
return f(val, stack)
end
error("unexpected type '" .. t .. "'")
end
function json.encode(val)
return ( encode(val) )
end
-------------------------------------------------------------------------------
-- Decode
-------------------------------------------------------------------------------
local parse
local function create_set(...)
local res = {}
for i = 1, select("#", ...) do
res[ select(i, ...) ] = true
end
return res
end
local space_chars = create_set(" ", "\t", "\r", "\n")
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals = create_set("true", "false", "null")
local literal_map = {
[ "true" ] = true,
[ "false" ] = false,
[ "null" ] = nil,
}
local function next_char(str, idx, set, negate)
for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
end
end
return #str + 1
end
local function decode_error(str, idx, msg)
local line_count = 1
local col_count = 1
for i = 1, idx - 1 do
col_count = col_count + 1
if str:sub(i, i) == "\n" then
line_count = line_count + 1
col_count = 1
end
end
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end
local function codepoint_to_utf8(n)
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
local f = math.floor
if n <= 0x7f then
return string.char(n)
elseif n <= 0x7ff then
return string.char(f(n / 64) + 192, n % 64 + 128)
elseif n <= 0xffff then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair?
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
return codepoint_to_utf8(n1)
end
end
local function parse_string(str, i)
local res = ""
local j = i + 1
local k = j
while j <= #str do
local x = str:byte(j)
if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
end
k = j + 1
elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
end
j = j + 1
end
decode_error(str, i, "expected closing quote for string")
end
local function parse_number(str, i)
local x = next_char(str, i, delim_chars)
local s = str:sub(i, x - 1)
local n = tonumber(s)
if not n then
decode_error(str, i, "invalid number '" .. s .. "'")
end
return n, x
end
local function parse_literal(str, i)
local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
decode_error(str, i, "invalid literal '" .. word .. "'")
end
return literal_map[word], x
end
local function parse_array(str, i)
local res = {}
local n = 1
i = i + 1
while 1 do
local x
i = next_char(str, i, space_chars, true)
-- Empty / end of array?
if str:sub(i, i) == "]" then
i = i + 1
break
end
-- Read token
x, i = parse(str, i)
res[n] = x
n = n + 1
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "]" then break end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
end
return res, i
end
local function parse_object(str, i)
local res = {}
i = i + 1
while 1 do
local key, val
i = next_char(str, i, space_chars, true)
-- Empty / end of object?
if str:sub(i, i) == "}" then
i = i + 1
break
end
-- Read key
if str:sub(i, i) ~= '"' then
decode_error(str, i, "expected string for key")
end
key, i = parse(str, i)
-- Read ':' delimiter
i = next_char(str, i, space_chars, true)
if str:sub(i, i) ~= ":" then
decode_error(str, i, "expected ':' after key")
end
i = next_char(str, i + 1, space_chars, true)
-- Read value
val, i = parse(str, i)
-- Set
res[key] = val
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "}" then break end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
end
return res, i
end
local char_func_map = {
[ '"' ] = parse_string,
[ "0" ] = parse_number,
[ "1" ] = parse_number,
[ "2" ] = parse_number,
[ "3" ] = parse_number,
[ "4" ] = parse_number,
[ "5" ] = parse_number,
[ "6" ] = parse_number,
[ "7" ] = parse_number,
[ "8" ] = parse_number,
[ "9" ] = parse_number,
[ "-" ] = parse_number,
[ "t" ] = parse_literal,
[ "f" ] = parse_literal,
[ "n" ] = parse_literal,
[ "[" ] = parse_array,
[ "{" ] = parse_object,
}
parse = function(str, idx)
local chr = str:sub(idx, idx)
local f = char_func_map[chr]
if f then
return f(str, idx)
end
decode_error(str, idx, "unexpected character '" .. chr .. "'")
end
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
local res, idx = parse(str, next_char(str, 1, space_chars, true))
idx = next_char(str, idx, space_chars, true)
if idx <= #str then
decode_error(str, idx, "trailing garbage")
end
return res
end
return json

View File

@ -39,6 +39,7 @@ function love.load()
levelNum = 1
currLevel = levelList[levelNum]
LoadedEntities = {}
LoadedParticles = {}
LevelLoadTiles()
main_Player = Player:New(75,50)