2024-01-27 15:58:16 +01:00
|
|
|
class_name World extends Node3D
|
|
|
|
|
2024-01-28 12:22:58 +01:00
|
|
|
var players: Array
|
2024-01-27 15:58:16 +01:00
|
|
|
|
|
|
|
@onready var score_update_timer: Timer = $ScoreUpdateTimer
|
|
|
|
@onready var pixel_count_compute_shader: PixelCountComputeShader = $PixelCountComputeShader
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
@onready var camera: Camera3D = $Camera3D
|
2024-01-27 00:05:04 +01:00
|
|
|
@onready var level: Node3D = $Level
|
|
|
|
@onready var world_coloring_viewport: SubViewport = $WorldColoringViewport
|
2024-01-28 12:58:17 +01:00
|
|
|
@onready var music_stream_player: AudioStreamPlayer = $MusicAudioStreamPlayer
|
2024-01-27 01:41:29 +01:00
|
|
|
|
2024-01-28 12:22:58 +01:00
|
|
|
var player_scene = preload("res://entities/Player.tscn")
|
|
|
|
|
2024-01-28 00:17:29 +01:00
|
|
|
const GAME_DURATION_SECONDS = 20
|
2024-01-27 23:12:04 +01:00
|
|
|
|
2024-01-27 15:58:16 +01:00
|
|
|
var camera_position: Vector3;
|
2024-01-27 00:05:04 +01:00
|
|
|
var world_coloring_material: ShaderMaterial
|
2024-01-27 15:58:16 +01:00
|
|
|
var world_plane: Plane = Plane(Vector3.UP, Vector3.ZERO)
|
2024-01-27 18:03:18 +01:00
|
|
|
var spawn_points: Array = Array()
|
2024-01-26 21:07:19 +01:00
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
enum GameState {
|
|
|
|
Startup,
|
|
|
|
Running,
|
|
|
|
Finished
|
|
|
|
}
|
|
|
|
|
|
|
|
var time_left = GAME_DURATION_SECONDS
|
|
|
|
var game_state : GameState = GameState.Startup
|
|
|
|
var game_state_last : GameState = GameState.Finished
|
|
|
|
|
2024-01-26 21:07:19 +01:00
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2024-01-27 15:58:16 +01:00
|
|
|
camera_position = camera.global_position
|
|
|
|
|
|
|
|
score_update_timer.connect("timeout", update_score)
|
|
|
|
|
2024-01-27 18:03:18 +01:00
|
|
|
find_spawn_points(level)
|
2024-01-28 12:22:58 +01:00
|
|
|
|
|
|
|
spawn_players()
|
2024-01-27 00:05:04 +01:00
|
|
|
apply_world_coloring_recursive(level)
|
2024-01-26 21:07:19 +01:00
|
|
|
|
2024-01-28 12:22:58 +01:00
|
|
|
|
2024-01-26 21:07:19 +01:00
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
2024-01-27 00:05:04 +01:00
|
|
|
func _process(_delta):
|
2024-01-27 23:12:04 +01:00
|
|
|
if game_state_last != game_state:
|
|
|
|
if game_state == GameState.Startup:
|
|
|
|
on_game_startup()
|
|
|
|
elif game_state == GameState.Running:
|
|
|
|
on_game_running()
|
|
|
|
elif game_state == GameState.Finished:
|
|
|
|
on_game_finished()
|
|
|
|
game_state_last = game_state
|
|
|
|
|
|
|
|
if game_state == GameState.Running:
|
|
|
|
time_left -= _delta;
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if time_left <= 0:
|
|
|
|
game_state = GameState.Finished
|
|
|
|
|
2024-01-27 15:58:16 +01:00
|
|
|
var player1 : Player = players[0]
|
|
|
|
var player_center = Vector3.ZERO
|
|
|
|
var player_coords_rect : Rect2 = Rect2(
|
|
|
|
player1.global_position.x,
|
|
|
|
player1.global_position.z,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
for player: CharacterBody3D in players:
|
|
|
|
player_center += player.global_position
|
|
|
|
player_coords_rect = player_coords_rect.expand(Vector2(player.global_position.x, player.global_position.z))
|
|
|
|
|
|
|
|
player_center = player_center / players.size()
|
|
|
|
player_center.y = 0
|
|
|
|
|
|
|
|
var view_width = player_coords_rect.size.x + 10
|
|
|
|
var view_height = player_coords_rect.size.y + 15
|
|
|
|
var camera_dist = max (view_width, view_height) * 0.5 / sin(deg_to_rad(camera.fov) * 0.5)
|
|
|
|
|
|
|
|
var camera_direction : Vector3 = -camera.basis.z
|
|
|
|
camera_position = player_center - camera_direction * camera_dist
|
|
|
|
|
|
|
|
camera.global_position = camera_position
|
2024-01-27 00:05:04 +01:00
|
|
|
|
|
|
|
func _physics_process(_delta):
|
2024-01-27 18:03:18 +01:00
|
|
|
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
|
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
if game_state != GameState.Running:
|
|
|
|
return
|
|
|
|
|
2024-01-27 18:03:18 +01:00
|
|
|
for player: Player in players:
|
|
|
|
if player.state == Player.PlayerState.Dead:
|
|
|
|
var occupied = true
|
|
|
|
var spawn_point
|
|
|
|
|
|
|
|
while occupied:
|
|
|
|
spawn_point = spawn_points.pick_random() as SpawnPoint
|
|
|
|
if not spawn_point.is_occupied():
|
|
|
|
occupied = false
|
|
|
|
|
2024-01-28 12:22:58 +01:00
|
|
|
player.global_position = spawn_point.global_position + Vector3.UP * 2
|
2024-01-27 18:03:18 +01:00
|
|
|
player.state = Player.PlayerState.Alive
|
|
|
|
spawn_point.near_players.append(player)
|
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
func _unhandled_input(event):
|
|
|
|
if game_state == GameState.Startup:
|
|
|
|
if event.is_action_pressed("ui_accept"):
|
|
|
|
game_state = GameState.Running
|
|
|
|
|
2024-01-27 18:03:18 +01:00
|
|
|
func find_spawn_points (node):
|
|
|
|
for child in node.get_children():
|
|
|
|
var spawn_point = child as SpawnPoint
|
|
|
|
if spawn_point:
|
|
|
|
spawn_points.append(spawn_point)
|
2024-01-28 13:19:08 +01:00
|
|
|
|
|
|
|
find_spawn_points(child)
|
2024-01-27 00:05:04 +01:00
|
|
|
|
|
|
|
func apply_world_coloring_recursive (node):
|
|
|
|
for child in node.get_children():
|
|
|
|
var mesh_instance := child as MeshInstance3D
|
|
|
|
if mesh_instance:
|
|
|
|
assign_world_coloring_pass(mesh_instance)
|
|
|
|
|
|
|
|
apply_world_coloring_recursive(child)
|
|
|
|
|
|
|
|
func assign_world_coloring_pass(mesh_instance: MeshInstance3D) -> void:
|
|
|
|
var material = mesh_instance.get_active_material(0)
|
|
|
|
if not material:
|
|
|
|
return
|
2024-01-27 01:15:01 +01:00
|
|
|
|
|
|
|
material.next_pass = world_coloring_material
|
2024-01-27 15:58:16 +01:00
|
|
|
|
|
|
|
func update_score():
|
|
|
|
pixel_count_compute_shader.recalculate_score()
|
|
|
|
|
|
|
|
players[0].score = pixel_count_compute_shader.red_score
|
|
|
|
|
2024-01-27 17:06:32 +01:00
|
|
|
if players.size() > 1:
|
2024-01-27 15:58:16 +01:00
|
|
|
players[1].score = pixel_count_compute_shader.blue_score
|
2024-01-27 23:12:04 +01:00
|
|
|
|
2024-01-28 12:22:58 +01:00
|
|
|
func spawn_players():
|
|
|
|
var players_node = $Players
|
|
|
|
|
|
|
|
var player1: Player = player_scene.instantiate()
|
2024-01-28 12:58:17 +01:00
|
|
|
player1.name = "Red"
|
2024-01-28 12:22:58 +01:00
|
|
|
player1.color = Color(1, 0, 0, 1)
|
|
|
|
player1.coloring_sprite_path = world_coloring_viewport.get_node("Player1Sprite").get_path()
|
|
|
|
player1.coloring_bomb_sprite_path = world_coloring_viewport.get_node("Player1BombSprite").get_path()
|
2024-01-28 12:30:57 +01:00
|
|
|
player1.move_right_action += "_p1"
|
|
|
|
player1.move_left_action += "_p1"
|
|
|
|
player1.move_up_action += "_p1"
|
|
|
|
player1.move_down_action += "_p1"
|
|
|
|
player1.dash_action += "_p1"
|
|
|
|
player1.bomb_action += "_p1"
|
2024-01-28 12:22:58 +01:00
|
|
|
var player2: Player = player_scene.instantiate()
|
2024-01-28 13:19:08 +01:00
|
|
|
player2.name = "Blue"
|
2024-01-28 12:22:58 +01:00
|
|
|
player2.color = Color(0, 0, 1, 1)
|
|
|
|
player2.coloring_sprite_path = world_coloring_viewport.get_node("Player2Sprite").get_path()
|
|
|
|
player2.coloring_bomb_sprite_path = world_coloring_viewport.get_node("Player2BombSprite").get_path()
|
2024-01-28 12:30:57 +01:00
|
|
|
player2.move_right_action += "_p2"
|
|
|
|
player2.move_left_action += "_p2"
|
|
|
|
player2.move_up_action += "_p2"
|
|
|
|
player2.move_down_action += "_p2"
|
|
|
|
player2.dash_action += "_p2"
|
|
|
|
player2.bomb_action += "_p2"
|
2024-01-28 12:22:58 +01:00
|
|
|
|
|
|
|
players_node.add_child(player1)
|
|
|
|
players_node.add_child(player2)
|
|
|
|
|
|
|
|
players.clear()
|
|
|
|
|
|
|
|
players.append(player1)
|
|
|
|
players.append(player2)
|
|
|
|
|
|
|
|
if players.size() == 1:
|
|
|
|
world_coloring_material = load("res://materials/WorldColoringMaterialPass.tres")
|
|
|
|
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
|
|
|
|
else:
|
|
|
|
world_coloring_material = load("res://materials/WorldColoringMaterialPassVersus.tres")
|
|
|
|
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
|
|
|
|
|
|
|
|
for player: Player in players:
|
|
|
|
player.state = player.PlayerState.Dead
|
|
|
|
|
|
|
|
func remove_players():
|
|
|
|
var players_node = $Players
|
|
|
|
|
|
|
|
for player: Player in players_node.get_children():
|
|
|
|
player.queue_free()
|
2024-01-28 12:30:57 +01:00
|
|
|
|
|
|
|
players.clear()
|
2024-01-28 12:22:58 +01:00
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
func on_game_startup():
|
|
|
|
print("Switching to startup")
|
|
|
|
stop_gameplay()
|
|
|
|
|
2024-01-28 12:58:17 +01:00
|
|
|
music_stream_player.stop()
|
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
world_coloring_viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_ONCE
|
|
|
|
world_coloring_viewport.render_target_update_mode = SubViewport.UPDATE_ONCE
|
|
|
|
|
|
|
|
func on_game_running():
|
|
|
|
print("Switching to gameplay")
|
|
|
|
start_gameplay()
|
|
|
|
|
2024-01-28 12:58:17 +01:00
|
|
|
music_stream_player.play()
|
|
|
|
|
2024-01-27 23:12:04 +01:00
|
|
|
world_coloring_viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_NEVER
|
|
|
|
world_coloring_viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS
|
|
|
|
|
|
|
|
func on_game_finished():
|
|
|
|
print("Switching to finished")
|
|
|
|
stop_gameplay()
|
|
|
|
|
|
|
|
world_coloring_viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_NEVER
|
|
|
|
world_coloring_viewport.render_target_update_mode = SubViewport.UPDATE_ONCE
|
|
|
|
|
|
|
|
func stop_gameplay():
|
|
|
|
print("Stopping gameplay")
|
|
|
|
for player: Player in players:
|
|
|
|
player.coloring_sprite.visible = false
|
|
|
|
player.coloring_bomb_sprite.visible = false
|
|
|
|
player.set_process(false)
|
|
|
|
player.set_process_input(false)
|
|
|
|
player.set_physics_process(false)
|
|
|
|
|
|
|
|
func start_gameplay():
|
2024-01-27 23:30:23 +01:00
|
|
|
print("Starting ggameplay")
|
2024-01-27 23:12:04 +01:00
|
|
|
time_left = GAME_DURATION_SECONDS
|
|
|
|
|
|
|
|
for player: Player in players:
|
2024-01-27 23:30:23 +01:00
|
|
|
player.energy = 100;
|
2024-01-27 23:12:04 +01:00
|
|
|
player.coloring_sprite.visible = true
|
|
|
|
player.coloring_bomb_sprite.visible = false
|
|
|
|
player.set_process(true)
|
|
|
|
player.set_process_input(true)
|
|
|
|
player.set_physics_process(true)
|
|
|
|
player.state = Player.PlayerState.Dead
|