chore: separate the weapon system

This commit is contained in:
Kazhnuz 2020-04-25 12:29:07 +02:00
parent a4a1bc94a3
commit baa59db9c2
3 changed files with 30 additions and 4 deletions

View file

@ -9,7 +9,7 @@ return {
bounce = false,
},
[2] = {
name = "basic",
name = "fire",
launch = {{0, 0, 0}},
zspeed = 360,
xspeed = 360,

View file

@ -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

View file

@ -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