From ae086e8ed1f0098de2fccd038064f9e9f8e8af05 Mon Sep 17 00:00:00 2001
From: STEINMETZ THOMAS <thomas.steinmetz2@etu.unistra.fr>
Date: Tue, 7 Apr 2020 14:08:40 +0200
Subject: [PATCH] #125 fix spawn items

---
 client/entities/items/beer.gd      |  4 +++
 client/entities/items/beer.tscn    |  5 +++-
 client/entities/items/trumpet.gd   |  4 +++
 client/entities/items/trumpet.tscn |  5 +++-
 server/autoloads/gamestate.gd      | 44 ++++++++++++++++++++++++++----
 server/entities/items/beer.gd      | 30 ++++++++++++++++++--
 server/entities/items/trumpet.gd   | 29 ++++++++++++++++++--
 7 files changed, 108 insertions(+), 13 deletions(-)
 create mode 100644 client/entities/items/beer.gd
 create mode 100644 client/entities/items/trumpet.gd

diff --git a/client/entities/items/beer.gd b/client/entities/items/beer.gd
new file mode 100644
index 0000000..519d544
--- /dev/null
+++ b/client/entities/items/beer.gd
@@ -0,0 +1,4 @@
+extends Spatial
+
+remotesync func deleteItemClient():
+	self.queue_free()
\ No newline at end of file
diff --git a/client/entities/items/beer.tscn b/client/entities/items/beer.tscn
index 2d9277a..99c95de 100644
--- a/client/entities/items/beer.tscn
+++ b/client/entities/items/beer.tscn
@@ -1,4 +1,6 @@
-[gd_scene load_steps=12 format=2]
+[gd_scene load_steps=13 format=2]
+
+[ext_resource path="res://entities/items/beer.gd" type="Script" id=1]
 
 [sub_resource type="Shader" id=1]
 code = "shader_type spatial;
@@ -285,6 +287,7 @@ surfaces/4 = {
 }
 
 [node name="beer" type="Spatial"]
+script = ExtResource( 1 )
 
 [node name="Cylinder" type="MeshInstance" parent="."]
 transform = Transform( 0.389609, 0, 0, 0, 0.389609, 0, 0, 0, 0.389609, 0, -0.308799, 0 )
diff --git a/client/entities/items/trumpet.gd b/client/entities/items/trumpet.gd
new file mode 100644
index 0000000..74572a7
--- /dev/null
+++ b/client/entities/items/trumpet.gd
@@ -0,0 +1,4 @@
+extends Spatial
+
+remotesync func deleteItemClient():
+	queue_free()
\ No newline at end of file
diff --git a/client/entities/items/trumpet.tscn b/client/entities/items/trumpet.tscn
index f5ac004..81f3962 100644
--- a/client/entities/items/trumpet.tscn
+++ b/client/entities/items/trumpet.tscn
@@ -1,4 +1,6 @@
-[gd_scene load_steps=6 format=2]
+[gd_scene load_steps=7 format=2]
+
+[ext_resource path="res://entities/items/trumpet.gd" type="Script" id=1]
 
 [sub_resource type="Shader" id=1]
 resource_name = "Shader Nodetree"
@@ -118,6 +120,7 @@ surfaces/1 = {
 }
 
 [node name="trumpet" type="Spatial"]
+script = ExtResource( 1 )
 
 [node name="Cylinder" type="MeshInstance" parent="."]
 transform = Transform( 0.172074, 0, 0, 0, 0.172073, 0, 0, 0, 0.172073, 0, 0, 0 )
diff --git a/server/autoloads/gamestate.gd b/server/autoloads/gamestate.gd
index 2b4c27e..f4d18ae 100644
--- a/server/autoloads/gamestate.gd
+++ b/server/autoloads/gamestate.gd
@@ -11,12 +11,18 @@ const ENVIRONMENT_LAYER = 0
 const PLAYER_LAYER = 1
 
 const MAX_SPAWN_POINT = 70
-const MAX_ITEMS = 30
+const MAX_ITEMS = 20
+var nbItemTotal = 0
+
+var timer = null
+var item_delay = 15
+signal deleteItem
 
 var rng = RandomNumberGenerator.new()
 
 var typeItems = ["beer","trumpet"]
 
+
 func _ready():
 	# When a client connects to the server
 	get_tree().connect("network_peer_connected", self, "_playerConnected")
@@ -91,8 +97,13 @@ func spawnPlayer():
 		listAvailableSpawn.remove(i)
 
 
+func _initNewItem():
+	print("Nouveau spawn")
+	emit_signal("deleteItem")
+	spawnInitItem()
+
+
 func spawnInitItem():
-	
 	var listAvailableItem= []
 	# Create a list of available spawn points
 	for i in range(MAX_SPAWN_POINT):
@@ -102,16 +113,34 @@ func spawnInitItem():
 	
 	
 	for item_counter in range(MAX_ITEMS):
+		nbItemTotal+=1
 		var typeItem = rng.randi_range(0,len(typeItems)-1) 
 		var itemScene = load("res://entities/items/"+typeItems[typeItem]+".tscn")
 		var item = itemScene.instance()
-		item.set_name("item"+str(item_counter))
-		get_node(GAMEPATH+"itemCollection").add_child(item)
 		var i=rng.randi_range(0,len(listAvailableItem)-1)
+		item.set_name("item"+str(nbItemTotal))
+		get_node(GAMEPATH+"itemCollection").add_child(item)
+		self.connect("deleteItem",item,"on_timeout_complete")
 		var position = get_node(GAMEPATH+"spawnCollection/spawn"+str(listAvailableItem[i]+1)).get_translation()
 		item.translate(position)
 		listAvailableItem.remove(i)
-		rpc("registerItem",item_counter,typeItem,position)
+		rpc("registerItem",nbItemTotal,typeItem,position)
+		timer.start()
+
+#func on_createNewItem(id):
+#	listAvailableItem.push_back(id.to_int()-1)
+#	var typeItem = rng.randi_range(0,len(typeItems)-1) 
+#	var itemScene = load("res://entities/items/"+typeItems[typeItem]+".tscn")
+#	var item = itemScene.instance()
+#	var i=rng.randi_range(0,len(listAvailableItem)-1)
+#	item.set_name("item"+str(i+1))
+#	get_node(GAMEPATH+"itemCollection").add_child(item)
+#	var position = get_node(GAMEPATH+"spawnCollection/spawn"+str(listAvailableItem[i]+1)).get_translation()
+#	item.translate(position)
+#	listAvailableItem.remove(i)
+#	rpc("registerItem",i+1,typeItem,position)
+#	print("new item add")
+	
 
 func startGame():
 	# Instancing the map and adding it to the scene tree
@@ -119,6 +148,11 @@ func startGame():
 	get_tree().get_root().add_child(game)
 	spawnPlayer()
 	
+	timer = Timer.new()
+	timer.set_one_shot(false)
+	timer.set_wait_time(item_delay)
+	timer.connect("timeout",self,"_initNewItem")
+	get_node(GAMEPATH).add_child(timer)
 	# Broadcast the beginning of the game
 	rpc("startGame")
 	spawnInitItem()
diff --git a/server/entities/items/beer.gd b/server/entities/items/beer.gd
index 34d1327..8bbdc3b 100644
--- a/server/entities/items/beer.gd
+++ b/server/entities/items/beer.gd
@@ -1,8 +1,32 @@
 extends Area
 
+const GAMEPATH = "/root/game/"
+
 const player: = preload("res://entities/characters/player.gd")
 
+#var timer = null
+#var beer_delay = 15
+#
+#signal createNewItem
+
+#func _ready():
+#	timer = Timer.new()
+#	timer.set_one_shot(true)
+#	timer.autostart = true
+#	timer.set_wait_time(beer_delay)
+#	timer.connect("timeout",self,"on_timeout_complete")
+#	get_node(GAMEPATH).add_child(timer)
+
+	
 
-func _on_beer_body_entered(body):
-	if body is player:
-		queue_free()
+func on_timeout_complete():
+#	timer.free()
+	rpc("deleteItemClient")
+#	var x = self.name
+#	x.erase(0,4)
+#	x.to_int()
+#	print("x :",x)
+#	emit_signal("createNewItem",x)
+#	timer.stop()
+#	timer.queue_free()
+	queue_free()
\ No newline at end of file
diff --git a/server/entities/items/trumpet.gd b/server/entities/items/trumpet.gd
index 34d1327..2006681 100644
--- a/server/entities/items/trumpet.gd
+++ b/server/entities/items/trumpet.gd
@@ -2,7 +2,30 @@ extends Area
 
 const player: = preload("res://entities/characters/player.gd")
 
+const GAMEPATH = "/root/game/"
 
-func _on_beer_body_entered(body):
-	if body is player:
-		queue_free()
+#var timer = null
+#var beer_delay = 15
+#
+#
+#func _ready():
+#	timer = Timer.new()
+#	timer.set_one_shot(true)
+#	timer.autostart = true
+#	timer.set_wait_time(beer_delay)
+#	timer.connect("timeout",self,"on_timeout_complete")
+#	get_node(GAMEPATH).add_child(timer)
+	
+
+func on_timeout_complete():
+	rpc("deleteItemClient")
+	queue_free()
+
+
+
+
+
+
+#func _on_beer_body_entered(body):
+#	if body is player:
+#		queue_free()
-- 
GitLab