summaryrefslogtreecommitdiff
path: root/godot/scripts/machines/Cannon.gd
blob: 80b9edf838e0a6c17f959861e81a6105913d5d4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
extends "res://scripts/machines/NetworkedMachineGDS.gd"

var world_ballistics = null

var cooldown = 0
export var fire_rate = 1 #shot/s
export var ball_speed = 400 #m/s

var pitch: float = 0.0
var turn: float = 0.0 

remotesync var loaded: bool = false

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: Spatial = get_node("YawJoint/PitchJoint/Muzzle")
onready var status: Label3D = get_node("StatusNotifier")

remote func update_aim(pitch_z, yaw_y):
	$YawJoint/PitchJoint.rotation_degrees.z = pitch_z
	$YawJoint.rotation_degrees.y = yaw_y

func get_init_info():
	return {"pitch_rot" : $YawJoint/PitchJoint.rotation_degrees.z, "turn_rot" : $YawJoint.rotation_degrees.y, "in_use" : in_use, "loaded" : loaded}

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"]
	loaded = init_info["loaded"]

# Called when the node enters the scene tree for the first time.
func _ready():
	if get_parent().name != "MACHINES": #if cannon is aboard ship
		add_collision_exception_with(get_parent().get_parent())
		mode = RigidBody.MODE_KINEMATIC
	world_ballistics = world.get_node("BALLISTICS")
	
func on_new_control():
	if is_network_master(): world.cam.attach(self, "STATIC", "./YawJoint/PitchJoint/CameraPoint")

func _physics_process(delta):
	if in_use and is_network_master(): #aim
		$YawJoint/PitchJoint.rotation_degrees.z += pitch*pitch_speed*delta
		$YawJoint.rotation_degrees.y += turn*turn_speed*delta
		$YawJoint.rotation_degrees.y = clamp($YawJoint.rotation_degrees.y, min_yaw, max_yaw)
		$YawJoint/PitchJoint.rotation_degrees.z = clamp($YawJoint/PitchJoint.rotation_degrees.z, min_pitch, max_pitch)

		rpc("update_aim", $YawJoint/PitchJoint.rotation_degrees.z, $YawJoint.rotation_degrees.y)

func direction_input(fwd,bwd,left,right,_left,_right):
	pitch = fwd - bwd
	turn = right - left

func attack1():
	if !loaded:
		return
	rpc("fire")

remotesync func fire():
	loaded = false
	status.set_visible(true)
	$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_ballistics.add_child(cball, true)
	world.add_child(expl)
	expl.scale = Vector3(0.25,0.25,0.25)
	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
	cball.shooter = user.name + " (" + world.players_info[user.get_network_master()][0] + ")"
	cball.shooter_id = user.get_network_master()
	if mode == RigidBody.MODE_KINEMATIC:
		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:
		if is_network_master(): apply_impulse($YawJoint/PitchJoint.global_transform.origin - global_transform.origin, -1*cball.mass*ball_speed*muzzle.global_transform.basis.x)