spacevroom

A SkyRoads clone with only one boring "level" written in Godot Engine version 3.
git clone https://kaka.farm/~git/spacevroom
Log | Files | Refs | LICENSE

Player.gd (4371B)


      1 extends KinematicBody
      2 
      3 
      4 # Space Vroom, an unoriginal space racer.
      5 # Copyright (C) 2021  Yuval Langer
      6 #
      7 # This program is free software: you can redistribute it and/or modify
      8 # it under the terms of the GNU Affero General Public License as
      9 # published by the Free Software Foundation, either version 3 of the
     10 # License, or (at your option) any later version.
     11 #
     12 # This program is distributed in the hope that it will be useful,
     13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 # GNU Affero General Public License for more details.
     16 #
     17 # You should have received a copy of the GNU Affero General Public License
     18 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     19 
     20 
     21 onready var ray_cast1: RayCast = $RayCast1
     22 onready var ray_cast2: RayCast = $RayCast2
     23 onready var ray_cast3: RayCast = $RayCast3
     24 onready var ray_cast4: RayCast = $RayCast4
     25 
     26 
     27 var velocity: Vector3 = Vector3.ZERO
     28 var driving_speed: float = 0
     29 var direction: int = 0
     30 var last_pressed_direction: String = ""
     31 var on_floor_flag: bool = false
     32 
     33 
     34 signal speed_changed
     35 signal off_the_deep_end
     36 signal reached_goal
     37 
     38 
     39 onready var forward_speed_change_timer: Timer = $ForwardSpeedChangeTimer
     40 
     41 
     42 func _ready() -> void:
     43 	print_debug(forward_speed_change_timer.time_left)
     44 
     45 
     46 func is_near_floor(ray_cast: RayCast) -> bool:
     47 	if ray_cast.is_colliding():
     48 		var origin := ray_cast.global_transform.origin
     49 		var collision_point := ray_cast.get_collision_point()
     50 		var distance := origin.distance_to(collision_point)
     51 		return distance <= Constants.on_floor_maximum_distance
     52 	return false
     53 
     54 
     55 func is_near_goal(ray_cast: RayCast) -> bool:
     56 	var collider := ray_cast.get_collider()
     57 	if collider:
     58 		return collider.is_in_group("goal")
     59 	return false
     60 
     61 
     62 func _physics_process(delta: float) -> void:
     63 	var near_floor := (is_near_floor(ray_cast1)
     64 		or is_near_floor(ray_cast2)
     65 		or is_near_floor(ray_cast3)
     66 		or is_near_floor(ray_cast4))
     67 	var near_goal := (is_near_goal(ray_cast1)
     68 		or is_near_goal(ray_cast2)
     69 		or is_near_goal(ray_cast3)
     70 		or is_near_goal(ray_cast4))
     71 
     72 	if near_goal and near_floor:
     73 		emit_signal("reached_goal")
     74 	if near_floor:
     75 		direction = 0
     76 		if (
     77 			(not Input.is_action_just_pressed("ui_right")) or
     78 			(not Input.is_action_just_pressed("ui_left"))
     79 			):
     80 			if Input.is_action_pressed("ui_right") and Input.is_action_pressed("ui_left"):
     81 				if last_pressed_direction == "right":
     82 					direction = 1
     83 				elif last_pressed_direction == "left":
     84 					direction = -1
     85 			else:
     86 				if Input.is_action_pressed("ui_right"):
     87 					direction = 1
     88 				if Input.is_action_pressed("ui_left"):
     89 					direction = -1
     90 		if Input.is_action_just_pressed("ui_right"):
     91 			last_pressed_direction = "right"
     92 			direction = 1
     93 		if Input.is_action_just_pressed("ui_left"):
     94 			last_pressed_direction = "left"
     95 			direction = -1
     96 		if Input.is_action_just_released("ui_right"):
     97 			if Input.is_action_pressed("ui_left"):
     98 				last_pressed_direction = "left"
     99 				direction = -1
    100 			else:
    101 				last_pressed_direction = ""
    102 				direction = 0
    103 		if Input.is_action_just_released("ui_left"):
    104 			if Input.is_action_pressed("ui_right"):
    105 				last_pressed_direction = "right"
    106 				direction = 1
    107 			else:
    108 				last_pressed_direction = ""
    109 				direction = 0
    110 	if Input.is_action_pressed("ui_up") and forward_speed_change_timer.time_left == 0.0:
    111 		print_debug(forward_speed_change_timer.time_left)
    112 		forward_speed_change_timer.start()
    113 		driving_speed += Constants.forward_speed_increment
    114 		driving_speed = clamp(driving_speed, 0, Constants.maximum_forward_speed)
    115 		emit_signal("speed_changed", driving_speed)
    116 	if Input.is_action_pressed("ui_down") and forward_speed_change_timer.time_left == 0.0:
    117 		print_debug(forward_speed_change_timer.time_left)
    118 		forward_speed_change_timer.start()
    119 		driving_speed -= Constants.forward_speed_increment
    120 		driving_speed = clamp(driving_speed, 0, Constants.maximum_forward_speed)
    121 		emit_signal("speed_changed", driving_speed)
    122 
    123 
    124 	velocity.x = direction * Constants.side_speed
    125 	velocity.z = -driving_speed
    126 
    127 	if near_floor and Input.is_action_just_pressed("jump"):
    128 		velocity.y += Constants.jump_impulse
    129 	if near_floor:
    130 		velocity.y -= Constants.on_the_floor_fall_acceleration * delta
    131 	else:
    132 		velocity.y -= Constants.fall_acceleration * delta
    133 
    134 	velocity = move_and_slide(velocity, Vector3.UP)
    135 
    136 	if translation.y < -5:
    137 		emit_signal("off_the_deep_end")