ZLP1/scripts/entities/player.lua

144 lines
3.7 KiB
Lua
Raw Normal View History

2021-12-31 01:46:21 +00:00
Player = Entity:New(x,y)
2021-12-31 11:21:48 +00:00
-- Player Gun Types
2021-12-31 01:46:21 +00:00
Player.GunType = {
{
name = "Plasma Machine Gun",
shotCooldown = 0.05,
hits = 1,
damage = 1,
moveSpeed = 8,
animation = Animation.projectilePlasma,
friendly = true
},
{
name = "Piercing Blaster",
shotCooldown = 0.5,
hits = 3,
damage = 5,
moveSpeed = 5,
animation = Animation.projectilePiercing,
friendly = true
},
{
name = "Rocket Launcher",
shotCooldown = 1,
hits = 1,
damage = 10,
moveSpeed = 3,
animation = Animation.projectileRocket,
friendly = true
}
}
2021-12-31 11:21:48 +00:00
-- Player Builder
2021-12-31 01:46:21 +00:00
function Player:New(x,y)
local o = Entity:New(x,y)
o.moveSpeed = 3
2021-12-31 11:21:48 +00:00
o.moveSpeedFocused = 2
o.hitbox = 5
2021-12-31 01:46:21 +00:00
o.sprite = love.graphics.newImage("assets/heart.png")
o.draw_scale = 3
-- gun properties
o.shotTimer = 0
o.current_gun = 1
o.gunSwitchTimer = 0
o.gunSwitchTime = 0.2
o.max_health = 100
o.health = 100
-- lists
o:AddList(List.Simulate)
setmetatable(o, self)
self.__index = self
return o
end
2021-12-31 11:21:48 +00:00
-- Function to get the player input
2021-12-31 01:46:21 +00:00
function Player:Input()
2021-12-31 11:21:48 +00:00
-- Gun Switch
if love.keyboard.isDown("q") then
self.gunSwitchTimer = self.gunSwitchTimer - current_dt
elseif love.keyboard.isDown("e") then
self.gunSwitchTimer = self.gunSwitchTimer + current_dt
else
self.gunSwitchTimer = 0
end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
if math.abs(self.gunSwitchTimer) > self.gunSwitchTime then
self.current_gun = self.current_gun + math.sign(self.gunSwitchTimer)
self.gunSwitchTimer = 0
end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
if self.current_gun < 1 then self.current_gun = #self.GunType end
if self.current_gun > #self.GunType then self.current_gun = 1 end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
-- Player Intent to Movement
if love.keyboard.isDown("a") then self.target.x = self.pos.x - 1
elseif love.keyboard.isDown("d") then self.target.x = self.pos.x + 1
else self.target.x = self.pos.x end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
if love.keyboard.isDown("w") then self.target.y = self.pos.y - 1
elseif love.keyboard.isDown("s") then self.target.y = self.pos.y + 1
else self.target.y = self.pos.y end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
-- Variable moveSpeed when focusing
if love.keyboard.isDown("lshift") then
self:MoveTowards(self.target.x, self.target.y, self.moveSpeedFocused)
else
2021-12-31 01:46:21 +00:00
self:MoveTowards(self.target.x, self.target.y, self.moveSpeed)
2021-12-31 11:21:48 +00:00
end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
-- Actually moving the player
self.pos.x = math.min(math.max(self.pos.x + self.vel.x,self.hitbox),window_width - self.hitbox)
self.pos.y = math.min(math.max(self.pos.y + self.vel.y,self.hitbox),window_height - self.hitbox)
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
-- Get Ready to shoot if triggered
if self.shotTimer > 0 then self.shotTimer = self.shotTimer - current_dt end
if self.shotTimer < 0 then self.shotTimer = 0 end
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
-- Shoot if ready and triggered
if love.mouse.isDown(1) and self.shotTimer == 0 then
local x, y = love.mouse.getPosition()
Projectile:New(self.pos.x,self.pos.y,GetAngle(x - self.pos.x, y - self.pos.y), self.GunType[self.current_gun])
self.shotTimer = self.GunType[self.current_gun].shotCooldown
2021-12-31 01:46:21 +00:00
end
end
function Player:Step()
end
function Player:Draw()
2021-12-31 11:21:48 +00:00
love.graphics.setColor(1,1,1)
2021-12-31 01:46:21 +00:00
2021-12-31 11:21:48 +00:00
-- Draw Mouse
2021-12-31 01:46:21 +00:00
local x, y = love.mouse.getPosition()
love.graphics.circle("line", x, y, self.hitbox/2)
2021-12-31 11:21:48 +00:00
-- Draw Gun Name
2021-12-31 01:46:21 +00:00
love.graphics.print("Current Gun: "..self.GunType[self.current_gun].name, 10, 35)
2021-12-31 11:21:48 +00:00
-- Draw Self
2021-12-31 01:46:21 +00:00
if self.sprite ~= nil then
love.graphics.draw(
self.sprite,
self.pos.x-self.sprite:getPixelWidth()/2*self.draw_scale,
self.pos.y-self.sprite:getPixelHeight()/2*self.draw_scale,
0,
self.draw_scale
)
end
2021-12-31 11:21:48 +00:00
-- Draw Hitbox
love.graphics.setColor(1,0,0)
if love.keyboard.isDown("lshift") then
love.graphics.circle("fill", self.pos.x, self.pos.y, self.hitbox)
end
love.graphics.setColor(1,1,1)
2021-12-31 01:46:21 +00:00
end