diff options
Diffstat (limited to 'Player.gd')
-rw-r--r-- | Player.gd | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Player.gd b/Player.gd new file mode 100644 index 0000000..99c6abf --- /dev/null +++ b/Player.gd @@ -0,0 +1,61 @@ +extends KinematicBody + +export var speed = 20 +export var ang_speed = PI +export var mouse_dpi = 0.2 +export var pitch_min = -0.8 +export var pitch_max = 0.8 + +func joystick(neg_x: String,pos_x: String,neg_y: String,pos_y: String): + var joystick = Input.get_vector(neg_x,pos_x,neg_y,pos_y) + var joystrength = abs(joystick.length()) + if joystick != Vector2.ZERO: + joystick = joystick.normalized() + return Vector3(joystick.x,joystick.y,joystrength) + +func _physics_process(delta): + var d_ang_speed = ang_speed * delta + + var rotation = Transform(self.rotation, Vector3.ZERO) + var up = rotation * Vector3.UP + var forward = rotation * Vector3.FORWARD + var right = rotation * Vector3.RIGHT + + var stick = joystick("move_left", "move_right", "move_back", "move_forward") + var movement = Vector3.ZERO + movement += right * stick.x * stick.z * speed + movement += forward * stick.y * stick.z * speed + if Input.is_action_pressed("move_up"): + movement += up * speed + if Input.is_action_pressed("move_down"): + movement -= up * speed + movement = move_and_slide(movement, Vector3.UP) + + stick = joystick("look_right", "look_left", "look_down", "look_up") + var mouse_movement = MouseInput.get_mouse_movement() + if stick.z == 0.0: + stick.z = mouse_movement.length() * mouse_dpi + mouse_movement = -mouse_movement.normalized() + stick.x = mouse_movement.x + stick.y = mouse_movement.y + var pitch = forward.dot(Vector3.UP) + if (pitch >= pitch_min and stick.y < 0) or (pitch <= pitch_max and stick.y > 0): + rotate(right, stick.y * stick.z * d_ang_speed) + rotate(Vector3.UP, stick.x * stick.z * d_ang_speed) + +func _ready(): + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + +func _notification(what): + if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT: + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) + +func _input(event): + if event.is_action_pressed("ui_cancel"): + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) + if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE: + if event.is_action_pressed("click"): + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + else: + if event is InputEventMouseMotion: + MouseInput.add_mouse_movement(event.relative) |