Hello everyone.
After having played with defold for nearly a month, I thought that I should move onwards to creating a complete game in Defold, for it is the journey that will teach me what playing around won’t.
So after thinking on it for a while, I finally decided to start with a space shooter, much like Star Pirates, I have been working on it for about 3 days and have definitely progressed about making asteroids, a malfunctioning player and thanks to @britzl 's swarm ai, enemies. But along this , there is problem which I am unable to solve.
This is currently my player controller:
go.property("MAX_SPEED", 300)
local UP = hash("up")
local DOWN = hash("down")
local LEFT = hash("left")
local RIGHT = hash("right")
local FIRE = hash("fire")
local MIN_SPEED = 50
local ROTATION_ACCELERATION = math.rad(380)
local MAX_ROTATION_SPEED = math.rad(300)
local TURN_DECELERATION = 50
local DECELERATION = 250
local ACCELERATION = 175
local DT = 0
local RATE_OF_FIRE = 0.15
local MAX_BULLET_SPEED = 300
local MIN_SPEED = 50
local UNITVECTOR_Z = vmath.vector3(0, 0, 1)
local UNITVECTOR_UP = vmath.vector3(0, 1, 0)
local function dampen(value, amount, dt)
local diff = math.abs(value)-math.abs(value*amount)
if value<0 then
return math.min(0, value+(diff*dt))
else
return math.max(0, value-(diff*dt))
end
end
function init(self)
-- Add initialization code here
-- Remove this function if not needed
self.speed = self.MAX_SPEED
msg.post(".", "acquire_input_focus")
self.direction = self.MAX_SPEED
self.rotation_speed = 0
self.acceleartion = 0
self.fire_timeout = 0
end
function final(self)
-- Add finalization code here
-- Remove this function if not needed
msg.post(".", "release_input_focus")
end
function update(self, dt)
-- Add update code here
-- Remove this function if not needed
DT = dt
local q = vmath.quat_axis_angle(UNITVECTOR_Z, self.rotation_speed * dt)
go.set_rotation(go.get_rotation() * q)
-- move forward and limit horizontally
local pos = go.get_position()
local distance = self.speed * dt
local direction = vmath.rotate(go.get_rotation(), UNITVECTOR_UP) * distance
pos = pos + direction
go.set_position(pos)
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action)
-- Add input-handling code here
-- Remove this function if not needed
if action_id == UP then
self.speed = math.min(self.speed + ACCELERATION * DT, self.MAX_SPEED)
else
self.speed = math.max(self.speed - DECELERATION * DT, MIN_SPEED)
end
if action_id == LEFT then
if self.rotation_speed < 0 then
self.rotation_speed = -self.rotation_speed * 0.8
end
self.rotation_speed = math.min(self.rotation_speed + ROTATION_ACCELERATION * DT, MAX_ROTATION_SPEED)
self.speed = math.max(self.speed - TURN_DECELERATION * DT, MIN_SPEED)
print("left")
elseif action_id == RIGHT then
if self.rotation_speed > 0 then
self.rotation_speed = -self.rotation_speed * 0.8
end
self.rotation_speed = math.max(self.rotation_speed - ROTATION_ACCELERATION * DT, -MAX_ROTATION_SPEED)
self.speed = math.max(self.speed - TURN_DECELERATION * DT, MIN_SPEED)
else
self.rotation_speed = self.rotation_speed * 0.9
end
if action_id == FIRE and action.pressed then
self.fire_timeout = self.fire_timeout - DT
if self.fire_timeout <= 0 then
factory.create("#factory", go.get_world_position(), go.get_rotation(), { speed = MAX_BULLET_SPEED })
self.fire_timeout = RATE_OF_FIRE
end
else
self.fire_timeout = 0
end
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
This is mostly based on masterpiece GBRausers and works equally as well, but when I release left or the right key, the plane continues to turn instead of stopping. How can I cure this?