From baa59db9c23dc7024d22c84e240ac70240f646cf Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 25 Apr 2020 12:29:07 +0200 Subject: [PATCH] chore: separate the weapon system --- .../datas/gamedata/weapons.lua | 2 +- .../actors/{player.lua => player/init.lua} | 7 +++--- .../modules/world/actors/player/weapons.lua | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) rename sonic-bluestreak.love/game/modules/world/actors/{player.lua => player/init.lua} (94%) create mode 100644 sonic-bluestreak.love/game/modules/world/actors/player/weapons.lua diff --git a/sonic-bluestreak.love/datas/gamedata/weapons.lua b/sonic-bluestreak.love/datas/gamedata/weapons.lua index 628ba7f..7c6ae89 100644 --- a/sonic-bluestreak.love/datas/gamedata/weapons.lua +++ b/sonic-bluestreak.love/datas/gamedata/weapons.lua @@ -9,7 +9,7 @@ return { bounce = false, }, [2] = { - name = "basic", + name = "fire", launch = {{0, 0, 0}}, zspeed = 360, xspeed = 360, diff --git a/sonic-bluestreak.love/game/modules/world/actors/player.lua b/sonic-bluestreak.love/game/modules/world/actors/player/init.lua similarity index 94% rename from sonic-bluestreak.love/game/modules/world/actors/player.lua rename to sonic-bluestreak.love/game/modules/world/actors/player/init.lua index a678862..afb873b 100644 --- a/sonic-bluestreak.love/game/modules/world/actors/player.lua +++ b/sonic-bluestreak.love/game/modules/world/actors/player/init.lua @@ -5,7 +5,7 @@ local Player = Parent:extend() local Frame = require("game.modules.gui.frame") local Statusbar = require("game.modules.gui.status") -local WeaponList = require("datas.gamedata.weapons") +local Weapons = require(cwd .. "player.weapons") function Player:new(world, x, y, z, id) Player.super.new(self, world, "player", x, y, 0, 16, 16, 24, false) @@ -15,6 +15,8 @@ function Player:new(world, x, y, z, id) self.frame = Frame() + self.weapons = Weapons(self) + self.action = "normal" self.rings = 0 @@ -27,7 +29,6 @@ function Player:init(id) self:setSprite(self.charName, 8, 10) self:cloneSprite() self.statusbar = Statusbar(self, "speed", 50) - self.statusbar:setWeapon(1) end function Player:updateStart(dt) @@ -40,7 +41,7 @@ function Player:updateStart(dt) end if self.keys["B"].isPressed then - self.obj.Weapon(self.world, self.x, self.y+1, self.z+8, 1, WeaponList[1]); + self.weapons:shoot(self.x, self.y+1, self.z+8, 1) end end diff --git a/sonic-bluestreak.love/game/modules/world/actors/player/weapons.lua b/sonic-bluestreak.love/game/modules/world/actors/player/weapons.lua new file mode 100644 index 0000000..8c972e4 --- /dev/null +++ b/sonic-bluestreak.love/game/modules/world/actors/player/weapons.lua @@ -0,0 +1,25 @@ +local WeaponManager = Object:extend() + +local weaponList = require("datas.gamedata.weapons") + +function WeaponManager:new(player) + self.player = player + self:switch(1) +end + +function WeaponManager:switch(newWeapon) + if (weaponList[newWeapon] ~= nil) then + self.currentWeapon = newWeapon; + self.player.statusbar:setWeapon(self.currentWeapon); + end +end + +function WeaponManager:shoot(x, y, z, dir) + weaponData = weaponList[self.currentWeapon] + + for i,coord in ipairs(weaponData.launch) do + self.player.obj.Weapon(self.player.world, x + coord[1], y + coord[2], z + coord[3], dir, weaponData) + end +end + +return WeaponManager