Added rotatable laser weapon
This commit is contained in:
parent
1ee0b529a8
commit
154da5104e
10 changed files with 99 additions and 13 deletions
|
|
@ -3,6 +3,15 @@ extends Node2D
|
|||
## Shortcut to get_parent()
|
||||
@onready var ship: Ship = get_parent()
|
||||
|
||||
var expected_rotation: float = 0.0:
|
||||
set(value):
|
||||
if value > 180:
|
||||
expected_rotation = value - 360
|
||||
elif value < -180:
|
||||
expected_rotation = 360 + value
|
||||
else:
|
||||
expected_rotation = value
|
||||
|
||||
func _physics_process(_delta) -> void:
|
||||
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
|
||||
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright")
|
||||
|
|
@ -12,4 +21,8 @@ func _physics_process(_delta) -> void:
|
|||
weapon.shoot_request = Input.get_action_strength("shootprimary")
|
||||
"secondary":
|
||||
weapon.shoot_request = Input.get_action_strength("shootsecondary")
|
||||
if "gun_rotation" in weapon:
|
||||
var angle_to_mouse = ship.hull.position.angle_to_point(get_global_mouse_position())
|
||||
expected_rotation = rad_to_deg(angle_to_mouse - ship.hull.rotation)
|
||||
weapon.gun_rotation = expected_rotation
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,13 @@ var destination_degrees: float = 0:
|
|||
destination_degrees = value
|
||||
## Delta to destination_degrees
|
||||
var destination_difference: float = 1.0
|
||||
|
||||
var current_destination_difference: float:
|
||||
set(value):
|
||||
if value > 180:
|
||||
current_destination_difference = value - 360
|
||||
else:
|
||||
current_destination_difference = value
|
||||
## available map bounds (use with absolute position)
|
||||
var available_bounds: Vector2
|
||||
# for testing purposes only
|
||||
|
|
@ -35,11 +42,9 @@ func enter(_message):
|
|||
|
||||
func process(_delta):
|
||||
# checking if need to apply torque
|
||||
var current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
|
||||
var other_destination_difference = ship.hull.global_rotation_degrees - destination_degrees
|
||||
var min_difference = min(abs(current_destination_difference), abs(other_destination_difference))
|
||||
if min_difference > destination_difference:
|
||||
ship.engine.rotation_axis = min_difference / 180
|
||||
current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
|
||||
if abs(current_destination_difference) > destination_difference:
|
||||
ship.engine.rotation_axis = clamp(current_destination_difference / 360 + 0.5 * sign(current_destination_difference), -1.0, 1.0)
|
||||
else:
|
||||
ship.engine.rotation_axis = 0.0
|
||||
# making ship always accelerate
|
||||
|
|
|
|||
15
scripts/Weapons/laser.gd
Normal file
15
scripts/Weapons/laser.gd
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
extends Weapon
|
||||
|
||||
## Reference to Gun node
|
||||
@onready var gun = $Gun
|
||||
## Maximum gun rotation angle
|
||||
@export var max_gun_rotation: float = 30.0
|
||||
## Current gun rotation angle. Clamps with max_gun_rotation
|
||||
var gun_rotation: float = 0.0:
|
||||
set(value):
|
||||
gun_rotation = clamp(value, -max_gun_rotation / 2, max_gun_rotation / 2)
|
||||
update_gun()
|
||||
|
||||
## Updates gun sprite (and projectile spawner) rotation
|
||||
func update_gun():
|
||||
gun.rotation_degrees = gun_rotation
|
||||
Loading…
Add table
Add a link
Reference in a new issue