Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/Composer/Composer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extends Node
## the option to have a loading screen and the transfer of data between scenes.

## Emitted when Composer has been fully initialised, alongside with its timer.
signal finished_initialising()
signal finished_initialising

## Emitted when provided scene has been invalid (may not exist or path is invalid).
signal invalid_scene(path: String)
Expand All @@ -22,7 +22,7 @@ signal finished_loading(scene: Node)

## Use with loading screens, for scene activation (i.e making scene visible or activating certain game logic).
@warning_ignore("unused_signal")
signal loading_activated()
signal loading_activated

## Tracks if composer finished initializing; i.e when its ready to use.
var has_initialized: bool = false:
Expand All @@ -42,7 +42,8 @@ var cache_mode: ResourceLoader.CacheMode = ResourceLoader.CACHE_MODE_REUSE
## This should not be changed unless absolutely necessary.
var loading_timer_delay: float = 0.1:
set(val):
if !has_initialized: await finished_initialising
if !has_initialized:
await finished_initialising

loading_timer_delay = val
_loading_timer.wait_time = loading_timer_delay
Expand All @@ -59,6 +60,7 @@ var _current_load_screen: Node = null

var _current_data: Dictionary = {}


func _enter_tree() -> void:
invalid_scene.connect(_on_invalid_scene)
failed_loading.connect(_on_failed_loading)
Expand All @@ -67,51 +69,63 @@ func _enter_tree() -> void:
root = get_tree().root
_setup_timer()


## Replaces the current scene with a new scene using a path,
## can also be used for transferring data between scenes with the optional data_to_transfer
## parameter. Data will be stored as new scene metadata, named "transferred_data".
func load_scene(path_to_scene: String, data_to_transfer: Dictionary = {}) -> void:
if _is_loading: return
if _is_loading:
return

if !has_initialized: await finished_initialising
if !has_initialized:
await finished_initialising

var loader: Error = ResourceLoader.load_threaded_request(path_to_scene, "", is_using_subthreads, cache_mode)
var loader: Error = ResourceLoader.load_threaded_request(
path_to_scene, "", is_using_subthreads, cache_mode
)
if not ResourceLoader.exists(path_to_scene) or loader == null:
invalid_scene.emit(path_to_scene)
return

_is_loading = true

if _loading_timer == null: _setup_timer()
if _loading_timer == null:
_setup_timer()

get_tree().current_scene.queue_free()

_current_loading_path = path_to_scene
_current_data = data_to_transfer
_loading_timer.start()


## Creates a loading screen using a path and adds it to the SceneTree.
## Returns an instance of it for usage with signals.
func setup_load_screen(path_to_load_screen: String) -> Node:
if _has_loading_screen: return null
if _has_loading_screen:
return null

_has_loading_screen = true
_current_load_screen = load(path_to_load_screen).instantiate()

root.call_deferred("add_child",_current_load_screen)
root.call_deferred("move_child",_current_load_screen, get_child_count()-1)
root.call_deferred("add_child", _current_load_screen)
root.call_deferred("move_child", _current_load_screen, get_child_count() - 1)

return _current_load_screen


## Gets rid of the loading screen.
func clear_load_screen() -> void:
_current_load_screen.queue_free()
_current_load_screen = null
_has_loading_screen = false


func _check_loading_status() -> void:
var load_progress: Array = []
var load_status: ResourceLoader.ThreadLoadStatus = ResourceLoader.load_threaded_get_status(_current_loading_path, load_progress)
var load_status: ResourceLoader.ThreadLoadStatus = ResourceLoader.load_threaded_get_status(
_current_loading_path, load_progress
)

match load_status:
ResourceLoader.THREAD_LOAD_INVALID_RESOURCE:
Expand All @@ -126,19 +140,23 @@ func _check_loading_status() -> void:
updated_loading.emit(_current_loading_path, int(load_progress[0] * 100))
ResourceLoader.THREAD_LOAD_LOADED:
_loading_timer.stop()
finished_loading.emit(ResourceLoader.load_threaded_get(_current_loading_path).instantiate())
finished_loading.emit(
ResourceLoader.load_threaded_get(_current_loading_path).instantiate()
)


func _setup_timer() -> void:
_loading_timer = Timer.new()
_loading_timer.name = "LoadingTimer"
_loading_timer.wait_time = 0.1
_loading_timer.timeout.connect(_check_loading_status)
root.call_deferred("add_child",_loading_timer)
root.call_deferred("add_child", _loading_timer)

await _loading_timer.ready

has_initialized = true


func _on_finished_loading(scene: Node) -> void:
scene.set_meta("transferred_data", _current_data)

Expand All @@ -149,8 +167,10 @@ func _on_finished_loading(scene: Node) -> void:
_is_loading = false
_current_data = {}


func _on_invalid_scene(path: String) -> void:
printerr("Error: Invalid resource: " + path)


func _on_failed_loading(path: String) -> void:
printerr("Error: Failed to load resource: " + path)
3 changes: 3 additions & 0 deletions src/Game/Bullet/bullet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ const SPEED: float = 1000.0

var direction: Vector2 = Vector2.RIGHT


func _physics_process(delta: float) -> void:
global_position += direction.rotated(rotation) * SPEED * delta


func _on_kill_timer_timeout() -> void:
queue_free()


func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("Players"):
return
Expand Down
42 changes: 30 additions & 12 deletions src/Game/Game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ var player_sprites: Array = [
preload("res://assets/images/player4.png")
]

var player_scores: Dictionary = {
var player_scores: Dictionary = {}

}

var is_player_dead: Dictionary = {

}
var is_player_dead: Dictionary = {}

var is_dead: bool = false


func _ready() -> void:
multiplayer.peer_disconnected.connect(_on_player_disconnected)
multiplayer.server_disconnected.connect(_on_server_disconnected)
Expand All @@ -60,10 +57,12 @@ func _ready() -> void:

start_game()


func _process(delta: float) -> void:
if Input.is_action_just_pressed("exit"):
pause_panel.visible = not pause_panel.visible


func spawn_players() -> void:
# Spawn players for each client and assign authorities
for idx in range(0, Lobby.connected_peers.keys().size()):
Expand All @@ -87,11 +86,12 @@ func spawn_players() -> void:
player_scores[p_id] = {"score": 0, "kills": 0, "wave": 0}

if multiplayer.get_unique_id() != 1:
game_manager.rpc_id(1,"add_players_spawned",multiplayer.get_unique_id())
game_manager.rpc_id(1, "add_players_spawned", multiplayer.get_unique_id())
else:
game_manager.add_players_spawned(1)

@rpc("call_remote","authority","reliable",1)

@rpc("call_remote", "authority", "reliable", 1)
func spawn_zombie(z_pos: Vector2, health: float, speed: float) -> void:
# Spawn zombie for each client
var zombie: Zombie = ZOMBIE.instantiate()
Expand All @@ -116,6 +116,7 @@ func spawn_zombie(z_pos: Vector2, health: float, speed: float) -> void:
zombie.score_updated.connect(player._on_zombie_score_updated)
zombie.zombie_killed.connect(player._on_zombie_killed)


func start_game() -> void:
# When the game starts, synchronize all players.
for plr: Player in players.get_children():
Expand All @@ -128,17 +129,23 @@ func start_game() -> void:

wave_system.start()

@rpc("authority","call_local","reliable",1)

@rpc("authority", "call_local", "reliable", 1)
func end_game(scores: Dictionary) -> void:
# Show the game over panel and the scores sent by the host for each client
$UI/GameHUD/GameEndPanel/VBoxContainer/Title.text = "Game Over!\nYou survived for " + str(scores[multiplayer.get_unique_id()]["wave"]) + " waves.\nPlayers' stats:"
$UI/GameHUD/GameEndPanel/VBoxContainer/Title.text = (
"Game Over!\nYou survived for "
+ str(scores[multiplayer.get_unique_id()]["wave"])
+ " waves.\nPlayers' stats:"
)

wave_system.stop()
$GameOver.play()

for idx in range(0, player_score_container.get_child_count()):
var child: HBoxContainer = player_score_container.get_child(idx)
if child.name == "HBoxContainer": continue
if child.name == "HBoxContainer":
continue

var actual_index: int = idx - 1
if Lobby.connected_peers.keys().size() <= actual_index:
Expand All @@ -161,7 +168,8 @@ func end_game(scores: Dictionary) -> void:

set_process(false)

@rpc("any_peer","call_local","reliable",1)

@rpc("any_peer", "call_local", "reliable", 1)
func kill_player(id: int) -> void:
# Kill the player for every client
for player in players.get_children():
Expand All @@ -181,25 +189,30 @@ func kill_player(id: int) -> void:
if not is_player_dead.values().has(false):
rpc("end_game", player_scores)


func _on_zombie_spawned(health: float, speed: float) -> void:
# Randomize the zombie spawn location
zombie_spawn_point.progress_ratio = randf()

if multiplayer.get_unique_id() == 1:
spawn_zombie(zombie_spawn_point.global_position, health, speed)


func _on_zombie_score_updated(id: int, value: int) -> void:
if multiplayer.get_unique_id() == 1:
player_scores[id]["score"] += value


func _on_zombie_killed(id: int) -> void:
if multiplayer.get_unique_id() == 1:
player_scores[id]["kills"] += 1


func _on_player_disconnected(id: int) -> void:
kill_player(id)
game_manager.clear_peer(id)


func _on_server_disconnected() -> void:
# Kick everyone once the host has left or disconnected
process_mode = PROCESS_MODE_DISABLED
Expand All @@ -212,23 +225,28 @@ func _on_server_disconnected() -> void:
)
ui.add_child(popup)


func _on_menu_button_pressed() -> void:
$ButtonClick.play()
process_mode = PROCESS_MODE_DISABLED
Composer.load_scene("res://src/MainMenu/MainMenu.tscn")
Lobby.clear_peer()


func _on_resume_button_pressed() -> void:
$ButtonClick.play()
pause_panel.hide()


func _on_wave_system_update_info_text(text: String) -> void:
if not is_dead:
info_text.text = text


func _on_wave_system_wave_started() -> void:
%InfoAnimation.play("WaveStart")


func _on_wave_system_wave_ended(wave: int) -> void:
%InfoAnimation.play("RESET")

Expand Down
Loading