TowerDefense/Scripts/PlayerManager.gd
2025-08-27 13:27:32 +02:00

188 lines
4.6 KiB
GDScript

extends Node3D
class_name PlayerManager
enum STATE {IDLE,PLACING}
var _state = STATE.IDLE
#TODO Get rid of tower_blueprint
@onready var tower_blueprint : PackedScene
@onready var selected_tower : Tower = null
@onready var cam : Camera3D = $"../Camera3D"
@onready var the_cube : TheCube = %TheCube
@onready var towers : Array[Tower]
var tower_count : int = 0:
set(value):
tower_count = value
EventBus.tower_count_changed.emit(tower_count)
var team_in_action : Array[Tower]:
set(value):
team_in_action = value
EventBus.team_in_action_changed.emit(team_in_action)
var team_in_rest : Array[Tower]:
set(value):
team_in_rest = value
EventBus.team_in_rest_changed.emit(team_in_rest)
var target_tower : Tower
var selected_collider : CollisionObject3D
func _ready() -> void:
EventBus.tower_selected.connect(_on_EventBus_tower_selected)
EventBus.tower_to_build.connect(_on_EventBus_tower_to_build)
func _process(delta: float) -> void:
handle_player_controls()
if Input.is_action_just_pressed("build"):
if not selected_collider:
return
if selected_collider.collision_layer == 1:
build_tower()
if selected_collider is TheCube:
EventBus.open_shop.emit()
else:
EventBus.close_shop.emit()
if Input.is_action_just_pressed("rest"):
if _state == STATE.PLACING:
quit_placing()
EventBus.close_shop.emit()
if target_tower:
send_to_rest()
func handle_player_controls() -> void:
var space_state : PhysicsDirectSpaceState3D = get_world_3d().direct_space_state
var mouse_pos : Vector2 = get_viewport().get_mouse_position()
var origin : Vector3 = cam.project_ray_origin(mouse_pos)
var end : Vector3 = origin + cam.project_ray_normal(mouse_pos) * 100
var ray : PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.create(origin, end)
ray.collide_with_bodies = true
var ray_result : Dictionary = space_state.intersect_ray(ray)
if ray_result.is_empty():
visible = false
selected_collider = null
return
var collider : CollisionObject3D = ray_result.get("collider")
selected_collider = collider
visible = true
$"selection-a".visible = true
global_position = collider.global_position
global_position += Vector3(0.0,0.2,0.0)
if selected_collider is Tower:
target_tower = collider
else:
target_tower = null
if _state == STATE.PLACING:
tower_to_place.sprite.modulate = "ff4545c8" #If the tower can't be placed he is red
$"selection-a".visible = false
if selected_collider is BuildTile: #if we are placing a tower, hide the selector model
tower_to_place.sprite.modulate = "61ff45c8" #If the tower can be placed he is green
var tower_to_place : Tower #It is freed in the build_tower()
func place_tower() -> void:
_state = STATE.PLACING
tower_to_place = tower_blueprint.instantiate()
add_child(tower_to_place)
tower_to_place.can_shoot = false
tower_to_place.collision_layer = 0
tower_to_place.sprite.modulate = "ffffffc8"
tower_to_place.show_price_tag()
func quit_placing() -> void:
_state = STATE.IDLE
tower_to_place.queue_free()
# Refactoriser ce code
func build_tower() -> void:
if tower_blueprint == null:
return
if !visible:
return
var temp_tower : Tower = tower_blueprint.instantiate()
#Move existing tower
for tower : Tower in team_in_action:
if tower.tower_name == temp_tower.tower_name:
move_tower()
return
#Send to action a resting tower
for tower : Tower in team_in_rest:
if tower.tower_name == temp_tower.tower_name:
move_tower()
send_to_action()
return
#Build Tower
if temp_tower.price < the_cube.money:
#Can't build
print("Too costly")
return
else :
#Can build
quit_placing() #Destroy the tower placeholder
EventBus.money_spent.emit(temp_tower.price)
tower_count += 1
$"../Towers".add_child(temp_tower)
EventBus.tower_builded.emit(temp_tower)
towers.append(temp_tower)
tower_blueprint == null
selected_tower = temp_tower
send_to_action()
move_tower()
func move_tower():
selected_tower.global_position = global_position
func send_to_rest():
team_in_action.erase(selected_tower)
team_in_rest.append(selected_tower)
selected_tower.is_rest = true
EventBus.team_in_action_changed.emit(team_in_action)
func send_to_action():
team_in_rest.erase(selected_tower)
team_in_action.append(selected_tower)
selected_tower.is_rest = false
EventBus.team_in_action_changed.emit(team_in_action)
func _on_EventBus_tower_selected(tower : PackedScene):
# TODO: Remove tower_blueprint
print("pouette")
# tower_blueprint = tower
# TODO: Utile ?
func _on_cube_icon_pressed() -> void:
pass
#open_shop()
func _on_EventBus_tower_to_build(packed_tower : PackedScene) -> void:
tower_blueprint = packed_tower
place_tower()