diff options
| author | Anson Bridges <bridges.anson@gmail.com> | 2022-08-31 00:26:34 -0700 |
|---|---|---|
| committer | Anson Bridges <bridges.anson@gmail.com> | 2022-08-31 00:26:34 -0700 |
| commit | d3998186c9795f2a85148cd5bcfa1bd5b6226cfb (patch) | |
| tree | ba5cae524562818ee38da48dd09b0b42a733e1d8 /scripts/machines/Cannon.gd | |
Initialize repo
Diffstat (limited to 'scripts/machines/Cannon.gd')
| -rw-r--r-- | scripts/machines/Cannon.gd | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/scripts/machines/Cannon.gd b/scripts/machines/Cannon.gd new file mode 100644 index 0000000..d7c9a18 --- /dev/null +++ b/scripts/machines/Cannon.gd @@ -0,0 +1,92 @@ +extends RigidBody +#Basis +var in_use : bool = false +var user = null +var world = null + +var cooldown = 0 +export var fire_rate = 1 #shot/s +export var ball_speed = 500 #m/s + +var pitch :float = 0.0 +var turn :float = 0.0 + +export var turn_speed = 7.5 #deg/s +export var pitch_speed = 10 + +export var max_pitch = 50 +export var min_pitch = -10 +export var min_yaw = -15 +export var max_yaw = 15 + +onready var muzzle = get_node("YawJoint/PitchJoint/Muzzle") + +func get_init_info(): + return {"pitch_rot" : $YawJoint/PitchJoint.rotation_degrees.z, "turn_rot" : $YawJoint.rotation_degrees.y, "in_use" : in_use} + +func mp_init(init_info): + $YawJoint/PitchJoint.rotation_degrees.z = init_info["pitch_rot"] + $YawJoint.rotation_degrees.y = init_info["turn_rot"] + in_use = init_info["in_use"] + +# Called when the node enters the scene tree for the first time. +func _ready(): + if get_parent().name != "MACHINES": + add_collision_exception_with(get_parent()) + mode = RigidBody.MODE_STATIC + world = get_tree().get_root().find_node("BALLISTICS", true, false) + + +func is_in_use(): + return in_use + +func take_control(controller): + $YawJoint/PitchJoint/Camera.current = true + user = controller + in_use = true + return self + +func relinquish_control(): + in_use = false + user = null + +func _physics_process(delta): + if cooldown > 0: + cooldown -= delta + if in_use: + #aim + $YawJoint/PitchJoint.rotation_degrees.z += pitch*pitch_speed*delta + $YawJoint.rotation_degrees.y += turn*turn_speed*delta + if $YawJoint.rotation_degrees.y > max_yaw: + $YawJoint.rotation_degrees.y = max_yaw + elif $YawJoint.rotation_degrees.y < min_yaw: + $YawJoint.rotation_degrees.y = min_yaw + if $YawJoint/PitchJoint.rotation_degrees.z > max_pitch: + $YawJoint/PitchJoint.rotation_degrees.z = max_pitch + elif $YawJoint/PitchJoint.rotation_degrees.z < min_pitch: + $YawJoint/PitchJoint.rotation_degrees.z = min_pitch + +func direction_input(fwd,bwd,left,right,_left,_right): + pitch = fwd - bwd + turn = left - right + +func attack1(): + if cooldown > 0: + return + $YawJoint/PitchJoint/Muzzle/explosion_sound.play() + var expl = preload("res://particles/p_Explosion.tscn").instance() + var cball = preload("res://scenes/ballistics/Cannonball.tscn").instance() + world.add_child(cball) + world.add_child(expl) + expl.init(muzzle.global_transform.origin, Vector3.ZERO) + add_collision_exception_with(cball) + cball.global_transform.origin = muzzle.global_transform.origin + cball.linear_velocity = muzzle.global_transform.basis.x*ball_speed + cooldown = fire_rate + if mode == RigidBody.MODE_STATIC: + get_parent().apply_impulse($YawJoint/PitchJoint.global_transform.origin - get_parent().global_transform.origin, -1*cball.mass*ball_speed*muzzle.global_transform.basis.x) + else: + apply_impulse($YawJoint/PitchJoint.global_transform.origin - global_transform.origin, -1*cball.mass*ball_speed*muzzle.global_transform.basis.x) + +func attack2(): + pass |
