2024-01-27 15:58:16 +01:00
|
|
|
class_name Player extends CharacterBody3D
|
2024-01-27 00:05:04 +01:00
|
|
|
|
|
|
|
@export var color : Color = Color.DODGER_BLUE
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
@export var move_right_action := "move_right"
|
|
|
|
@export var move_left_action := "move_left"
|
|
|
|
@export var move_down_action := "move_down"
|
|
|
|
@export var move_up_action := "move_up"
|
|
|
|
@export var dash_action := "dash"
|
2024-01-27 01:15:01 +01:00
|
|
|
@export var bomb_action := "bomp"
|
2024-01-26 21:07:19 +01:00
|
|
|
|
2024-01-27 01:41:29 +01:00
|
|
|
@export var coloring_sprite_path : NodePath
|
|
|
|
@export var coloring_bomb_sprite_path : NodePath
|
|
|
|
|
2024-01-27 15:58:16 +01:00
|
|
|
@onready var geometry: MeshInstance3D = $Geometry
|
|
|
|
@onready var collision: CollisionShape3D = $CollisionShape3D
|
2024-01-26 21:07:19 +01:00
|
|
|
|
2024-01-28 01:14:56 +01:00
|
|
|
var bomb_emitter_scene = preload("res://entities/BombEmitter.tscn")
|
2024-01-27 01:15:01 +01:00
|
|
|
|
2024-01-27 17:06:32 +01:00
|
|
|
enum PlayerState {
|
|
|
|
Alive = 0,
|
|
|
|
Falling,
|
|
|
|
Dead
|
|
|
|
}
|
|
|
|
|
2024-01-27 15:58:16 +01:00
|
|
|
var score : int = 0
|
2024-01-27 23:30:23 +01:00
|
|
|
var energy : float = 100.0
|
2024-01-27 17:06:32 +01:00
|
|
|
var state : PlayerState = PlayerState.Alive
|
2024-01-27 23:12:04 +01:00
|
|
|
var state_last : PlayerState = PlayerState.Dead
|
2024-01-27 15:58:16 +01:00
|
|
|
|
|
|
|
var radius = 0.15
|
2024-01-26 21:07:19 +01:00
|
|
|
var angle = 0
|
|
|
|
var is_dashing = false
|
2024-01-28 01:14:56 +01:00
|
|
|
var explosion_velocity: Vector3 = Vector3.ZERO
|
|
|
|
var explosion_influence: float = 0
|
2024-01-26 21:07:19 +01:00
|
|
|
var dash_time = 0
|
2024-01-27 01:41:29 +01:00
|
|
|
var bomb_time = 0
|
|
|
|
var coloring_sprite : Sprite2D
|
|
|
|
var coloring_bomb_sprite : Sprite2D
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
const SPEED = 5.0
|
|
|
|
const DASH_SPEED: float = 20
|
|
|
|
const DASH_DURATION: float = 0.2
|
2024-01-27 23:30:23 +01:00
|
|
|
const DASH_ENERGY: float = 40
|
2024-01-27 01:41:29 +01:00
|
|
|
const BOMB_DURATION: float = 0.1
|
2024-01-27 23:30:23 +01:00
|
|
|
const BOMB_ENERGY: float = 60
|
|
|
|
const ENERGY_REPLENISH_RATE: float = 0.5;
|
2024-01-28 01:14:56 +01:00
|
|
|
const EXPLOSION_INFLUENCE_REDUCTION_RATE: float = 10.0;
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
|
2024-01-27 01:41:29 +01:00
|
|
|
func _ready():
|
|
|
|
coloring_sprite = get_node(coloring_sprite_path)
|
|
|
|
coloring_bomb_sprite = get_node(coloring_bomb_sprite_path)
|
|
|
|
coloring_sprite.modulate = color
|
|
|
|
coloring_sprite.visible = true
|
|
|
|
coloring_bomb_sprite.modulate = color
|
|
|
|
coloring_bomb_sprite.visible = false
|
|
|
|
|
2024-01-26 21:07:19 +01:00
|
|
|
func _physics_process(delta):
|
2024-01-27 23:30:23 +01:00
|
|
|
if energy < 100:
|
|
|
|
energy = min (100, energy + delta + ENERGY_REPLENISH_RATE)
|
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
if state_last != state:
|
|
|
|
if state == PlayerState.Alive:
|
|
|
|
on_player_spawn()
|
|
|
|
elif state == PlayerState.Falling:
|
|
|
|
on_player_falling()
|
|
|
|
elif state == PlayerState.Dead:
|
|
|
|
on_player_dead()
|
|
|
|
|
|
|
|
state_last = state
|
|
|
|
|
|
|
|
if global_position.y < -5:
|
|
|
|
state = PlayerState.Dead
|
|
|
|
elif not is_on_floor():
|
2024-01-27 17:06:32 +01:00
|
|
|
if not is_dashing:
|
|
|
|
velocity.y -= gravity * delta
|
|
|
|
state = PlayerState.Falling
|
|
|
|
elif state != PlayerState.Dead:
|
|
|
|
state = PlayerState.Alive
|
2024-01-27 18:03:18 +01:00
|
|
|
elif state == PlayerState.Dead:
|
|
|
|
visible = false
|
2024-01-27 23:12:04 +01:00
|
|
|
return
|
2024-01-27 18:03:18 +01:00
|
|
|
elif state == PlayerState.Alive:
|
|
|
|
visible = true
|
2024-01-27 17:06:32 +01:00
|
|
|
|
|
|
|
coloring_sprite.visible = state == PlayerState.Alive
|
2024-01-26 21:07:19 +01:00
|
|
|
|
2024-01-27 01:41:29 +01:00
|
|
|
if bomb_time > 0:
|
|
|
|
bomb_time -= delta
|
|
|
|
if bomb_time <= 0:
|
|
|
|
coloring_bomb_sprite.visible = false
|
|
|
|
bomb_time = 0
|
|
|
|
else:
|
2024-01-27 23:30:23 +01:00
|
|
|
if Input.is_action_just_pressed(bomb_action) \
|
|
|
|
and state == PlayerState.Alive \
|
|
|
|
and energy > BOMB_ENERGY:
|
|
|
|
on_drop_bomb()
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
|
|
var input_dir = Input.get_vector(move_left_action, move_right_action, move_up_action, move_down_action)
|
|
|
|
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
|
|
if direction:
|
|
|
|
velocity.x = direction.x * SPEED
|
|
|
|
velocity.z = direction.z * SPEED
|
|
|
|
angle = direction.signed_angle_to(Vector3.FORWARD, Vector3.UP)
|
|
|
|
geometry.global_basis = Basis.from_euler(Vector3(0, -angle, 0))
|
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
velocity.z = move_toward(velocity.z, 0, SPEED)
|
2024-01-27 00:05:04 +01:00
|
|
|
geometry.global_basis = Basis.from_euler(Vector3(0, -angle, 0))
|
2024-01-26 21:07:19 +01:00
|
|
|
|
2024-01-27 23:30:23 +01:00
|
|
|
if Input.is_action_just_pressed(dash_action) \
|
|
|
|
and state == PlayerState.Alive \
|
|
|
|
and energy > DASH_ENERGY:
|
|
|
|
on_dash()
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
if is_dashing:
|
|
|
|
velocity.x = sin(angle) * DASH_SPEED
|
|
|
|
velocity.z = -cos(angle) * DASH_SPEED
|
|
|
|
|
|
|
|
geometry.global_basis = Basis.from_euler(Vector3(-0.3, -angle, 0))
|
|
|
|
|
|
|
|
dash_time -= delta
|
|
|
|
if dash_time <= 0:
|
|
|
|
is_dashing = false
|
|
|
|
|
2024-01-28 01:14:56 +01:00
|
|
|
if explosion_influence > 0:
|
|
|
|
velocity += explosion_velocity
|
|
|
|
explosion_influence = max (0, explosion_influence - EXPLOSION_INFLUENCE_REDUCTION_RATE * delta)
|
|
|
|
|
2024-01-26 21:07:19 +01:00
|
|
|
move_and_slide()
|
2024-01-27 01:41:29 +01:00
|
|
|
|
|
|
|
coloring_sprite.global_position = calc_coloring_position()
|
|
|
|
coloring_bomb_sprite.global_position = coloring_sprite.global_position
|
|
|
|
|
|
|
|
func calc_coloring_position():
|
2024-01-27 15:58:16 +01:00
|
|
|
var result = Vector2 (global_position.x * 10 + 512, global_position.z * 10 + 512)
|
2024-01-27 01:41:29 +01:00
|
|
|
return result;
|
2024-01-27 23:12:04 +01:00
|
|
|
|
|
|
|
func on_player_spawn():
|
|
|
|
print ("Player " + str(self) + ": spawning")
|
|
|
|
|
|
|
|
func on_player_falling():
|
|
|
|
print ("Player " + str(self) + ": falling")
|
|
|
|
|
|
|
|
func on_player_dead():
|
|
|
|
print ("Player " + str(self) + ": death")
|
2024-01-27 23:30:23 +01:00
|
|
|
|
|
|
|
func on_dash():
|
|
|
|
is_dashing = true;
|
|
|
|
dash_time = DASH_DURATION
|
|
|
|
energy = max (0, energy - DASH_ENERGY)
|
|
|
|
|
|
|
|
func on_drop_bomb():
|
|
|
|
coloring_bomb_sprite.visible = true;
|
|
|
|
bomb_time = BOMB_DURATION
|
|
|
|
energy = max (0, energy - BOMB_ENERGY)
|
2024-01-28 01:14:56 +01:00
|
|
|
|
|
|
|
var bomb_emitter : BombEmitter = bomb_emitter_scene.instantiate()
|
|
|
|
bomb_emitter.global_position = global_position
|
|
|
|
get_parent_node_3d().add_child(bomb_emitter)
|