feat: add the gizmo system

This commit is contained in:
Kazhnuz 2021-03-20 17:23:14 +01:00
parent f7f1a772f7
commit 91fee0a856
4 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,4 @@
return {
isSolid = false,
needButton = true, -- "useButton" or "onContact"
}

View file

@ -0,0 +1,51 @@
local cwd = (...):gsub('%.gizmo$', '') .. "."
local Parent = require(cwd .. "parent")
local Gizmo = Parent:extend()
function Gizmo:new(world, x, y, w, h, overrides)
local w = w or 16;
local h = h or 16;
Gizmo.super.new(self, world, "gizmo", x, y, w, h, false)
self.overrides = overrides
end
function Gizmo:setProperties(properties)
self:setDefaultProperties()
self:replaceProperties(properties)
if (self.overrides ~= nil) then
self:replaceProperties(self.overrides)
end
self:applyProperties()
end
function Gizmo:applyProperties()
-- Apply properties to the internal item system
self.isSolid = self.properties.isSolid
self.mainHitbox.isSolid = self.properties.isSolid
if (self.isSolid) then
self:addHitbox("btnInput", "btnInput", -1, -1, self.w + 2, self.h + 2, false)
end
self.needButton = self.properties.needButton
end
function Gizmo:setDefaultProperties()
local default = require "game.utils.gizmo.properties"
self.properties = {}
for key, value in pairs(default) do
self.properties[key] = value
end
end
function Gizmo:replaceProperties(properties)
for key, _ in pairs(properties) do
if (properties[key] ~= nil) then
self.properties[key] = properties[key]
end
end
end
function Gizmo:doAction()
self.scene:showMessage("I AM ERROR " .. self.creationID)
end
return Gizmo;

View file

@ -3,11 +3,14 @@ local Obj = {}
-- On charge toutes les différentes types d'acteurs
local cwd = (...):gsub('%.init$', '') .. "."
Obj.Player = require(cwd .. "player")
Obj.Gizmo = require(cwd .. "gizmo")
Obj.index = {}
Obj.index["player"] = Obj.Player
Obj.index["gizmo"] = Obj.Gizmo
Obj.collisions = {}
Obj.collisions["wall"] = require(cwd .. "wall")
Obj.collisions["gizmo-collision"] = Obj.Gizmo
return Obj

View file

@ -21,6 +21,8 @@ function Player:new(world, x, y, id)
self.canChangeActive = true
self.tweens = TweenManager(self)
self.lastCollision = -1
self.haveCollided = false
end
function Player:isMoving()
@ -57,6 +59,38 @@ function Player:updateStart(dt)
self.tweens:update(dt)
self.world:getTileTypeAtPoint(self.x, self.y)
if (self.haveCollided == false) then
self.lastCollision = nil
end
self.haveCollided = false
end
function Player:collisionResponse(col)
if (not col.other.owner.isDestroyed) then
if (col.other.type == "gizmo") then
if (col.other.owner.needButton) then
if (self.keys["A"].isPressed) then
col.other.owner:doAction()
self.haveCollided = true
self.lastCollision = col.other.owner.creationID
end
else
if (self.lastCollision ~= col.other.owner.creationID) then
col.other.owner:doAction()
end
self.haveCollided = true
self.lastCollision = col.other.owner.creationID
end
end
if (col.other.type == "btnInput" and col.other.owner.needButton) then
if (self.keys["A"].isPressed) then
col.other.owner:doAction()
self.haveCollided = true
self.lastCollision = col.other.owner.creationID
end
end
end
end
function Player:timerResponse(response)