Skip to content

Commit

Permalink
Added back the possibility of playing local games
Browse files Browse the repository at this point in the history
  • Loading branch information
RichoM committed Dec 14, 2021
1 parent b6476eb commit b9b1d82
Show file tree
Hide file tree
Showing 9 changed files with 664 additions and 125 deletions.
28 changes: 15 additions & 13 deletions end.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends Node2D

var client = Client
var winner = null
var return_scene = "res://menu.tscn"
onready var p0 = $players/p0
onready var p1 = $players/p1
onready var p0_title = $GUI/purple_wins
Expand All @@ -15,21 +16,22 @@ func _ready():
else:
p1.play("alive")
p0.play("dead")
p1_title.visible = true
p1_title.visible = true

func _process(delta):
# Discard old packets
client.rtc_mp.poll()
while client.rtc_mp.get_available_packet_count() > 0:
var packet = client.rtc_mp.get_packet()

# Send a keep alive packet
var data = {"t": Globals.get_timestamp()}
var msg = JSON.print(data)
if client.rtc_mp.put_packet(msg.to_utf8()) != 0:
# TODO(Richo): Handle errors
print("ERROR!")
if Globals.mode == Globals.ONLINE_MULTIPLAYER:
# Discard old packets
client.rtc_mp.poll()
while client.rtc_mp.get_available_packet_count() > 0:
var packet = client.rtc_mp.get_packet()

# Send a keep alive packet
var data = {"t": Globals.get_timestamp()}
var msg = JSON.print(data)
if client.rtc_mp.put_packet(msg.to_utf8()) != 0:
# TODO(Richo): Handle errors
print("ERROR!")

func _on_play_button_pressed():
get_tree().change_scene("res://game.tscn")
get_tree().change_scene(return_scene)
get_tree().get_root().remove_child(self)
81 changes: 81 additions & 0 deletions game_local.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
extends Node2D

onready var client = Client
onready var p0 = $player0
onready var p1 = $player1

var game_over = false
var player = null
var opponent = null

var projectile_counter = 0
var player_projectiles = {} # Projectile -> int (id)

func _ready():
Globals.mode = Globals.LOCAL_MULTIPLAYER

player = p0
opponent = p1

player.is_local = true
player.input_map = "p0"
player.visible = true
player.input_enabled = true
opponent.is_local = true
opponent.input_map = "p1"
opponent.visible = true
opponent.input_enabled = true

for touch_btn in $touch.get_children():
touch_btn.visible = OS.has_touchscreen_ui_hint()

for planet in $planets.get_children():
var sprite = planet.get_node("sprite") as AnimatedSprite
sprite.frame = rand_range(0, sprite.frames.get_frame_count(sprite.animation) - 1)
planet.scale = Vector2.ONE * rand_range(1, 4.5)

player.connect("projectile_shot", self, "_on_projectile_shot")
opponent.connect("projectile_shot", self, "_on_projectile_shot")

func _on_projectile_shot(p):
p.is_local = true
if not player_projectiles.has(p):
projectile_counter += 1
player_projectiles[p] = projectile_counter


func _process(delta):
for p in player_projectiles.keys():
if p.exploded:
player_projectiles.erase(p)
p.explode()

func _on_player0_tree_exited():
if game_over: return
game_over = true
call_deferred("winner", "p1")

func _on_player1_tree_exited():
if game_over: return
game_over = true
call_deferred("winner", "p0")

func winner(winner):
var root = get_tree().get_root()

# Remove the current level
var level = get_tree().get_current_scene()
root.remove_child(level)
level.call_deferred("free")

# Add end scene
var end_scene = preload("res://end.tscn").instance()
end_scene.winner = winner
end_scene.return_scene = "res://game_local.tscn"
root.add_child(end_scene)

func _on_back_button_pressed():
back()

func back():
get_tree().change_scene("res://menu.tscn")
4 changes: 2 additions & 2 deletions game.tscn → game_local.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[ext_resource path="res://space_background_pack/layers/parallax-space-backgound.png" type="Texture" id=2]
[ext_resource path="res://space_background_pack/layers/parallax-space-stars.png" type="Texture" id=3]
[ext_resource path="res://hue.gdshader" type="Shader" id=4]
[ext_resource path="res://game.gd" type="Script" id=5]
[ext_resource path="res://game_local.gd" type="Script" id=5]
[ext_resource path="res://music/DavidKBD - My Little Bots Pack - 01 - My little bots.ogg" type="AudioStream" id=6]
[ext_resource path="res://player.tscn" type="PackedScene" id=7]
[ext_resource path="res://fonts/Tomorrow-Regular.ttf" type="DynamicFontData" id=8]
Expand Down Expand Up @@ -49,7 +49,7 @@ size = 28
use_filter = true
font_data = ExtResource( 13 )

[node name="game" type="Node2D"]
[node name="game_local" type="Node2D"]
script = ExtResource( 5 )

[node name="GUI" type="CanvasLayer" parent="."]
Expand Down
4 changes: 4 additions & 0 deletions game.gd → game_online.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ onready var last_msg_time = OS.get_ticks_msec()
var network_unstable_counter = 0

func _ready():
OS.hide_virtual_keyboard()
Globals.mode = Globals.ONLINE_MULTIPLAYER

if Globals.player == 0:
player = p0
opponent = p1
Expand Down Expand Up @@ -198,6 +201,7 @@ func winner(winner):
# Add end scene
var end_scene = preload("res://end.tscn").instance()
end_scene.winner = winner
end_scene.return_scene = "res://game_online.tscn"
root.add_child(end_scene)

func _on_back_button_pressed():
Expand Down
Loading

0 comments on commit b9b1d82

Please sign in to comment.