I'm kinda stuck on my player movement.
My player will be moving to and away from the mouse position by using W and S. When pressing A or D it should strafe according to the angle it's currently facing.
Function PlayerMovement()
Rem Save and update the X and Y positions of the player sprite.
Player.X = Sprite X(Player.SpriteNumber)
Player.Y = Sprite Y(Player.SpriteNumber)
Rem Save and update the mouse X and Y position.
MouseX#=MouseX()
MouseY#=MouseY()
Rem Calculate the angle difference between the player and the mouse.
Angle# = Atanfull(Player.X - MouseX# , Player.Y - MouseY#)
Rem Calculate the distance between the player and mouse position.
Distance_To_Mouse# = ((Player.X - MouseX#)^2+(Player.Y -MouseY#)^2)
Rem Rotate the player sprite, it's always facing the (mouse)cursor.
Rotate sprite Player.Spritenumber,-Angle#
Rem Print the distance variable to the screen.
Print "Distance to mouse ";Distance_To_Mouse#
Rem When "W" is pressed we move the player forwards.
If Keys(17,0) = 1
Player.X = NewXvalue2D(Player.X,Angle#,-Player.Speed)
Player.Y = NewYvalue2D(Player.Y,Angle#,-Player.Speed)
endif
Rem When "S" is pressed we move the player backwards.
If Keys(31,0) = 1
Player.X = NewXValue2D(Player.X,Angle#,Player.Speed)
Player.Y = NewYValue2D(Player.Y,Angle#,Player.Speed)
Endif
EndFunction
I've tried to :
If Keys(30,0) = 1
NewAngle# = Angle#
Player.X = etc..
Endif
This will update "NewAngle#" constantly. I'm not quite sure if I'm going in the right direction to create strafing. Should I save "Angle#" ONCE in a new variable, and strafe according to this new variable?
If so, how am I able to do this? Maybe a variable "Create_New_Angle" that changes between 1 and 0 to check if the "Angle#" has been stored once??
Thank you.
*Edit*
Here are the NewXValue2D and NewYValue2D functions:
Function NewXValue2D(X, Angle, MoveSpeed)
X = X + sin(Angle)*MoveSpeed
EndFunction X
Function NewYValue2D(Y, Angle, MoveSpeed)
Y = Y + cos(Angle)*MoveSpeed
EndFunction Y
And for anyone interested:
Make an array
Dim Keys(88,1)
Function UpdateKeystate()
For x = 1 to 88
Rem - If we press a key.
If Keystate(x)=1
If KeyS(x,0)=0
KeyS(x,1)=1
KeyS(x,0)=1
else
KeyS(x,1)=0
Endif
else
KeyS(x,0)=0
Endif
Next x
Endfunction
Keys(X,0)
X = Keystate number
0 = press and hold
1 = press once