godot-tank-test/tank-base.gd
2022-10-15 19:37:05 +02:00

85 lines
2.3 KiB
GDScript

extends VehicleBody
# Member variables
var shotscene = preload("res://tank-round.tscn")
const STEER_SPEED = .35
const STEER_LIMIT = .4
var steer_angle = 0
var steer_target = 0
var view_sensitivity = 0.1;
func _process(delta):
if (Input.is_action_pressed("ui_left")):
steer_target = STEER_LIMIT
print("left")
elif (Input.is_action_pressed("ui_right")):
steer_target = -STEER_LIMIT
print("right")
else:
steer_target = 0
if (Input.is_action_pressed("ui_up")):
set_engine_force(50)
print("accel")
else:
set_engine_force(0)
if (Input.is_action_pressed("ui_down")):
#set_brake(1)
set_engine_force(-50)
print("decel")
#if (Input.is_action_pressed("driver_break")):
# set_brake(1.0)
#else:
# set_brake(0.0)
if (steer_target < steer_angle):
steer_angle -= STEER_SPEED*delta
if (steer_target > steer_angle):
steer_angle = steer_target
elif (steer_target > steer_angle):
steer_angle += STEER_SPEED*delta
if (steer_target < steer_angle):
steer_angle = steer_target
set_steering(steer_angle)
func _enter_tree():
#Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED);
pass
func _ready():
set_process_input(true)
func _input(ie):
if ie is InputEventMouseMotion:
var turret = get_node("Turret")
var cannon = get_node("Turret/Cannon")
var plus_pitch = view_sensitivity * -ie.relative.y
var plus_yaw = fmod(view_sensitivity * -ie.relative.x,360)
var can_rot = cannon.get_rotation()
can_rot.z -= deg2rad(plus_pitch)
var can_elev = rad2deg(can_rot.z)
#if can_elev < 7 and can_elev > -30:
cannon.set_rotation(can_rot)
#print(cannon.get_global_transform().get_rotation())
turret.rotate(Vector3(0,1,0), deg2rad(plus_yaw))
elif ie is InputEventMouseButton:
if ie.button_index == BUTTON_LEFT and ie.is_pressed():
var shot = shotscene.instance()
get_parent().add_child(shot)
var shot_dir = get_node("Turret/Cannon/Aimpoint").get_global_transform().origin - get_node("Turret/Cannon").get_global_transform().origin
shot.set_transform(get_node("Turret/Cannon/Aimpoint").get_global_transform())
shot.set_linear_velocity(shot_dir.normalized() * 100.0)
#shot.set_linear_velocity(shot.get_rotation().normalized() * 10)
#shot.connect("body_enter", self, "shot_hit")
func shot_hit(obj):
print ("Own shot hit obj: " + obj.get_name())