Skip to content

Commit

Permalink
Merge pull request #114 from Jibbajabbafic/Jibbajabbafic/issue88
Browse files Browse the repository at this point in the history
AI bot players
  • Loading branch information
Jibbajabbafic authored May 21, 2023
2 parents 680a33c + 741c7e6 commit a471a02
Show file tree
Hide file tree
Showing 38 changed files with 1,090 additions and 590 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Types of changes:

## [Unreleased]

### Added
- Bots!
- New AI bot players to spice up singleplayer and multiplayer games

## [0.7.1] - 2023-04-25

### Security
Expand Down
54 changes: 35 additions & 19 deletions client/client_network.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const LATENCY_THRESHOLD := 20

var title_scene := "res://client/menu/title/title_screen.tscn"
var server_browser_scene := "res://client/menu/lobby/server_browser.tscn"
var singleplayer_setup_scene := "res://client/menu/setup/singleplayer/singleplayer_setup.tscn"
var multiplayer_setup_scene := "res://client/menu/setup/multiplayer/multiplayer_setup.tscn"
var setup_scene := "res://client/menu/setup/setup.tscn"
var world_scene := "res://client/world/world.tscn"

# Clock sync and latency vars
Expand Down Expand Up @@ -74,10 +73,7 @@ func change_scene_to_title_screen(fade: bool = true) -> void:

func change_scene_to_setup() -> void:
change_scene("res://client/menu/menu_handler.tscn")
if is_singleplayer:
$MenuHandler.change_menu_with_fade(singleplayer_setup_scene)
else:
$MenuHandler.change_menu_with_fade(multiplayer_setup_scene)
$MenuHandler.change_menu_with_fade(setup_scene)


func change_scene_to_lobby() -> void:
Expand Down Expand Up @@ -270,7 +266,7 @@ remote func receive_player_colour_update(player_id: int, colour_choice: int) ->
if is_rpc_from_server() == false:
return
player_list[player_id].colour = colour_choice
var setup = get_node_or_null("MenuHandler/MultiplayerSetup")
var setup = get_node_or_null("MenuHandler/Setup")
if setup:
setup.update_player_colour(player_id, colour_choice)

Expand All @@ -283,7 +279,7 @@ remote func receive_player_spectate_update(player_id: int, is_spectating: bool)
if is_rpc_from_server() == false:
return
player_list[player_id].spectate = is_spectating
var setup = get_node_or_null("MenuHandler/MultiplayerSetup")
var setup = get_node_or_null("MenuHandler/Setup")
if setup:
setup.update_player_spectating(player_id, is_spectating)

Expand All @@ -297,7 +293,7 @@ remote func receive_goal_change(goal: int) -> void:
if is_rpc_from_server() == false:
return
game_options.goal = goal
var options = get_node_or_null("MenuHandler/MultiplayerSetup/Setup/GameOptions")
var options = get_node_or_null("MenuHandler/Setup/GameOptions")
Logger.print(self, "Received new goal: %d" % [goal])
if options:
options.set_goal(goal)
Expand All @@ -312,12 +308,27 @@ remote func receive_lives_change(lives: int) -> void:
if is_rpc_from_server() == false:
return
game_options.lives = lives
var options = get_node_or_null("MenuHandler/MultiplayerSetup/Setup/GameOptions")
var options = get_node_or_null("MenuHandler/Setup/GameOptions")
Logger.print(self, "Received new lives: %d" % [lives])
if options:
options.set_lives(lives)


func send_bots_change(bots: int) -> void:
if is_host():
rpc_id(SERVER_ID, "receive_bots_change", bots)


remote func receive_bots_change(bots: int) -> void:
if is_rpc_from_server() == false:
return
game_options.bots = bots
var options = get_node_or_null("MenuHandler/Setup/GameOptions")
Logger.print(self, "Received new bots: %d" % [bots])
if options:
options.set_bots(bots)


func send_client_ready() -> void:
Logger.print(self, "Sending client ready")
rpc_id(SERVER_ID, "receive_client_ready")
Expand All @@ -336,6 +347,19 @@ remote func receive_player_flap(player_id: int, flap_time: int) -> void:
player.flap_queue.append(flap_time)


func send_player_death() -> void:
rpc_id(SERVER_ID, "receive_player_death", client_clock)


remote func receive_player_death(player_id: int, death_time: int) -> void:
if player_id == multiplayer.get_network_unique_id():
# This is the same player who sent it so don't call death
return
Logger.print(self, "Received death for player %d @ time = %d" % [player_id, death_time])
var player = $World.get_node(str(player_id))
player.death()


remote func receive_player_add_item(player_id: int, item_id: int) -> void:
var item: Item = Items.get_item(item_id)
Logger.print(self, "Received add item %d (%s) for player %d" % [item_id, item.name, player_id])
Expand All @@ -352,14 +376,6 @@ remote func receive_player_lost_life(lives_left: int) -> void:
ui.update_lives(lives_left)


remote func receive_player_knockback() -> void:
if is_rpc_from_server() == false:
return
var world = get_node_or_null("World")
if world:
world.knockback_player(multiplayer.get_network_unique_id())


remote func receive_despawn_player(player_id: int) -> void:
if is_rpc_from_server() == false:
return
Expand All @@ -376,7 +392,7 @@ func send_start_game_request() -> void:
remote func receive_setup_info_message(message: String) -> void:
if is_rpc_from_server() == false:
return
var setup = get_node_or_null("MenuHandler/MultiplayerSetup")
var setup = get_node_or_null("MenuHandler/Setup")
if setup:
setup.show_message(message)

Expand Down
40 changes: 40 additions & 0 deletions client/menu/setup/game_options_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ extends Control
onready var ScoreInput = $Panel/VBoxContainer/ScoreToWin/ScoreInput
onready var LivesToggle = $Panel/VBoxContainer/PlayerLives/LivesToggle
onready var LivesInput = $Panel/VBoxContainer/PlayerLives/LivesInput
onready var BotsToggle = $Panel/VBoxContainer/Bots/BotsToggle
onready var BotsInput = $Panel/VBoxContainer/Bots/BotsInput

var emit_changes := false


func _ready() -> void:
BotsInput.max_value = Network.MAX_PLAYERS


func set_enable_host_options(is_host: bool) -> void:
$Panel/DisableGameOptions.visible = not is_host
ScoreInput.editable = is_host
LivesToggle.disabled = not is_host
LivesInput.editable = is_host
BotsToggle.disabled = not is_host
BotsInput.editable = is_host


func set_game_options(game_options: Dictionary) -> void:
set_goal(game_options.goal)
set_lives(game_options.lives)
set_bots(game_options.bots)
# Don't emit signals the first time the values are set or it can infinitely loop
emit_changes = true

Expand All @@ -28,6 +44,16 @@ func set_lives(new_lives: int) -> void:
LivesInput.show()


func set_bots(new_bots: int) -> void:
if new_bots < 1:
BotsToggle.pressed = false
BotsInput.hide()
else:
BotsToggle.pressed = true
BotsInput.value = new_bots
BotsInput.show()


func _on_ScoreInput_value_changed(new_goal: float) -> void:
if emit_changes:
Network.Client.send_goal_change(int(new_goal))
Expand All @@ -45,3 +71,17 @@ func _on_LivesToggle_toggled(button_pressed: bool) -> void:
func _on_LivesInput_value_changed(value: float) -> void:
if emit_changes:
Network.Client.send_lives_change(int(value))


func _on_BotsToggle_toggled(button_pressed: bool) -> void:
BotsInput.visible = button_pressed
if emit_changes:
if button_pressed:
Network.Client.send_bots_change(int(BotsInput.value))
else:
Network.Client.send_bots_change(0)


func _on_BotsInput_value_changed(value: float) -> void:
if emit_changes:
Network.Client.send_bots_change(int(value))
72 changes: 0 additions & 72 deletions client/menu/setup/multiplayer/multiplayer_setup.gd

This file was deleted.

Loading

0 comments on commit a471a02

Please sign in to comment.