function init(self)
msg.post(".", "acquire_input_focus")
self.fire_possible = true
self.movement_possible = true
-- these may be changed in testing
self.ammo_count = 5
self.default_ammo = 5
self.total_ammo = 40
self.speed = 0
--up to here
self.ai_remaining = 8
self.kill_count = 0
self.bandage_count = 2
self.health_level = 100
self.velocity = vmath.vector3()
--raycasting
self.to = vmath.vector3()
local rendercam = require "rendercam.rendercam"
self.sprinting = false
self.crouched = false
end
--function for changing the speed of the player when shift is pressed
function sprint(self)
local sprint_speed = 2
local normal_speed = 1
--this is run in on_input. if the player is already sprinting and they hit LShift, they slow down
if sprinting then
self.speed = self.speed * normal speed
sprinting = true
--this is same as above, but to start sprinting
else
self.speed = self.speed * sprint_speed
sprinting = true
end
end
--function for changing height of player, simulating crouching
function crouch(self,crouched)
local pos = go.get_position() --position of player, because crouching will move the player down
if crouched == true then
pos.y = pos.y + 0.5
crouched = false
elseif crouched == false then
pos.y = pos.y - 0.5
crouched = true
end
return crouched
end
function heal(self)
--checks the player has bandages
if bandage_count > 0 then
--prevents the player from firing/moving
self.fire_possible = false
self.movement_possible = false
--starts a timer for 5 seconds
--health is increased by 30, and bandage count is decreased by 1
self.health_level = health_level + 30
msg.post("go#hud", healed, health_level) --messages the HUD collection to update the health displayed on the gui
self.bandage_count = bandage_count - 1
msg.post("go#hud", bandage_sub, bandage_count)
--fire_possible and movement_possible are now true
self.fire_possible = true
self.movement_possible = true
else
--dealt with in gui_script with a popup saying out of bandages
msg.post("go#hud","no_heal")
end
end
function fire(self)
if (self.ammo_count > 0) then
if (self.fire_possible == TRUE)then
--add fire animation and sound
self.ammo_count = self.ammo_count - 1
msg.post("go#hud", "ammo","ammo_count")
--raycast
local from = go.get_position()--players position
local to = self.to --players viewing co-ords
local groups = {hash("enemy")} -- collision group to test against
local result = physics.raycast(from, to, player)
if result == true then
msg.post(enemy, dead)
self.kill_count = self.kill_count + 1
msg.post("go#hud", "kill","kill_count")
end
else
msg.post("go#hud", "no_fire")
end
end
end
function reload(self)
if self.total_ammo > 5 then
self.fire_possible = false
self.total_ammo = self.total_ammo - 5
self.ammo_count = self.default_ammo
self.fire_possible = true --allows the player to fire once the time has finished
local mag = self.total_ammo / 5 --number of magazines left
msg.post("go#hud", "reload","mag")
end
end
function update(self, dt)
local pos = go.get_world_position()
pos = pos + self.velocity * dt
go.set_position(pos)
self.velocity.x = 0
self.velocity.z = 0
end
function on_input(self, action_id, action)
local vel = 100 --originally 20, but it was way too slow
if action_id == hash("forward") then
self.velocity.z = -vel --it would be positive, but due to the way i oriented the map it needs to be negative.
end
if action_id == hash("back") then
self.velocity.z = vel
end
if action_id == hash("left") then
self.velocity.x = -vel
end
if action_id == hash("right") then
self.velocity.x = vel
end
if action_id == hash("reload") then --uses the reload() as the callback which starts when the 5 seconds are up
timer.delay(5, false, reload)
end
if action_id == hash("touch") then
timer.delay(3, false, fire)
end
if action_id == hash("crouch") then
crouch()
end
if action_id == hash("sprint") then
sprint()
end
if action_id == hash("heal") then
timer.delay(5, false, heal)
end
if action_id == hash("esc") then
msg.post("loader:/go#loader", "show_esc")
end
end
There you go