From 60e8d7051bcac601ce8fbd4b51f8386f80d25b7d Mon Sep 17 00:00:00 2001
From: STEINMETZ THOMAS <thomas.steinmetz2@etu.unistra.fr>
Date: Tue, 14 Apr 2020 14:54:58 +0200
Subject: [PATCH] #125 add creation with new

---
 client/levels/mainMenu/MainMenu.tscn | 38 ++++++++++++++--------------
 client/levels/test/game.gd           | 10 ++++++--
 client/project.godot                 |  5 ++++
 server/autoloads/gamestate.gd        | 21 ++++++++-------
 server/entities/characters/player.gd | 17 ++++++++++++-
 server/entities/items/beer.gd        | 16 ++++++++++++
 server/entities/items/trumpet.gd     | 15 +++++++++++
 server/levels/test/game.gd           |  4 +--
 8 files changed, 93 insertions(+), 33 deletions(-)

diff --git a/client/levels/mainMenu/MainMenu.tscn b/client/levels/mainMenu/MainMenu.tscn
index 7ac146a..ce81194 100644
--- a/client/levels/mainMenu/MainMenu.tscn
+++ b/client/levels/mainMenu/MainMenu.tscn
@@ -6,13 +6,13 @@
 [ext_resource path="res://levels/mainMenu/assets/mainMenuCharacters.png" type="Texture" id=4]
 [ext_resource path="res://levels/commons/assets/logotmp.png" type="Texture" id=5]
 [ext_resource path="res://levels/commons/fonts/montserrat/Montserrat-Bold.otf" type="DynamicFontData" id=6]
-[ext_resource path="res://levels/mainMenu/assets/letsplay.png" type="Texture" id=8]
-[ext_resource path="res://levels/commons/scripts/buttonHover.gd" type="Script" id=10]
-[ext_resource path="res://levels/mainMenu/assets/profile.png" type="Texture" id=11]
-[ext_resource path="res://levels/mainMenu/assets/settings.png" type="Texture" id=12]
-[ext_resource path="res://levels/mainMenu/assets/credits.png" type="Texture" id=13]
-[ext_resource path="res://levels/mainMenu/assets/exit.png" type="Texture" id=14]
-[ext_resource path="res://levels/commons/fonts/montserrat/Montserrat-Regular.otf" type="DynamicFontData" id=15]
+[ext_resource path="res://levels/mainMenu/assets/letsplay.png" type="Texture" id=7]
+[ext_resource path="res://levels/commons/scripts/buttonHover.gd" type="Script" id=8]
+[ext_resource path="res://levels/mainMenu/assets/profile.png" type="Texture" id=9]
+[ext_resource path="res://levels/mainMenu/assets/settings.png" type="Texture" id=10]
+[ext_resource path="res://levels/mainMenu/assets/credits.png" type="Texture" id=11]
+[ext_resource path="res://levels/mainMenu/assets/exit.png" type="Texture" id=12]
+[ext_resource path="res://levels/commons/fonts/montserrat/Montserrat-Regular.otf" type="DynamicFontData" id=13]
 
 [sub_resource type="Animation" id=1]
 resource_name = "anim"
@@ -11653,7 +11653,7 @@ size = 20
 extra_spacing_top = 5
 extra_spacing_bottom = 5
 extra_spacing_char = 1
-font_data = ExtResource( 15 )
+font_data = ExtResource( 13 )
 
 [sub_resource type="Animation" id=1394]
 tracks/0/type = "value"
@@ -11817,7 +11817,7 @@ margin_bottom = 100.0
 rect_min_size = Vector2( 0, 100 )
 size_flags_horizontal = 5
 theme = SubResource( 1390 )
-texture_normal = ExtResource( 8 )
+texture_normal = ExtResource( 7 )
 expand = true
 stretch_mode = 4
 
@@ -11829,10 +11829,10 @@ rect_pivot_offset = Vector2( 179.208, 47.8025 )
 mouse_default_cursor_shape = 2
 size_flags_horizontal = 5
 size_flags_vertical = 0
-texture_normal = ExtResource( 8 )
+texture_normal = ExtResource( 7 )
 expand = true
 stretch_mode = 5
-script = ExtResource( 10 )
+script = ExtResource( 8 )
 
 [node name="Tween" type="Tween" parent="homePage/sdfg/MarginContainer/menu/liensMenu/Play"]
 
@@ -11845,10 +11845,10 @@ rect_pivot_offset = Vector2( 171.841, 50.9325 )
 mouse_default_cursor_shape = 2
 size_flags_horizontal = 5
 size_flags_vertical = 0
-texture_normal = ExtResource( 11 )
+texture_normal = ExtResource( 9 )
 expand = true
 stretch_mode = 5
-script = ExtResource( 10 )
+script = ExtResource( 8 )
 
 [node name="Tween" type="Tween" parent="homePage/sdfg/MarginContainer/menu/liensMenu/Profile"]
 
@@ -11861,10 +11861,10 @@ rect_pivot_offset = Vector2( 181.663, 47.9238 )
 mouse_default_cursor_shape = 2
 size_flags_horizontal = 5
 size_flags_vertical = 0
-texture_normal = ExtResource( 12 )
+texture_normal = ExtResource( 10 )
 expand = true
 stretch_mode = 5
-script = ExtResource( 10 )
+script = ExtResource( 8 )
 
 [node name="Tween" type="Tween" parent="homePage/sdfg/MarginContainer/menu/liensMenu/Settings"]
 
@@ -11877,10 +11877,10 @@ rect_pivot_offset = Vector2( 177.98, 48.5983 )
 mouse_default_cursor_shape = 2
 size_flags_horizontal = 5
 size_flags_vertical = 0
-texture_normal = ExtResource( 13 )
+texture_normal = ExtResource( 11 )
 expand = true
 stretch_mode = 5
-script = ExtResource( 10 )
+script = ExtResource( 8 )
 
 [node name="Tween" type="Tween" parent="homePage/sdfg/MarginContainer/menu/liensMenu/Credits"]
 
@@ -11896,11 +11896,11 @@ rect_pivot_offset = Vector2( 180.436, 49.2728 )
 mouse_default_cursor_shape = 2
 size_flags_horizontal = 5
 size_flags_vertical = 0
-texture_normal = ExtResource( 14 )
+texture_normal = ExtResource( 12 )
 texture_pressed = SubResource( 1392 )
 expand = true
 stretch_mode = 5
-script = ExtResource( 10 )
+script = ExtResource( 8 )
 
 [node name="Tween" type="Tween" parent="homePage/sdfg/MarginContainer/menu/liensMenu/Exit"]
 
diff --git a/client/levels/test/game.gd b/client/levels/test/game.gd
index 68e7f56..917d3e4 100644
--- a/client/levels/test/game.gd
+++ b/client/levels/test/game.gd
@@ -5,6 +5,7 @@ var movementInput:int = 0
 var jumpId:int = 0              # Distinguish 2 different jumps
 var mouseX:int = 0							# Mouse X percentage
 var mouseY:int = 0							# Mouse Y percentage
+var itemInput:bool = false
 
 # Attacks
 enum {
@@ -19,6 +20,7 @@ func _physics_process(_delta):
 
 func getPlayerInput():
 	movementInput = 0
+	itemInput = false
 	var attackStateInput:int = NONE
 	var vpSize:Vector2 = get_viewport().size
 	var mousePosition:Vector2 = get_viewport().get_mouse_position()
@@ -30,7 +32,11 @@ func getPlayerInput():
 
 	if Input.is_action_just_pressed("movementJump"):
 		jumpId += 1
-
+	
+	if Input.is_action_just_pressed("itemInput"):
+		itemInput = true
+		
+	
 	if Input.is_action_pressed("primaryAttack"):
 		attackStateInput = PUNCH
 		mouseX = int(mousePosition.x/vpSize.x * 100) - 50
@@ -42,7 +48,7 @@ func getPlayerInput():
 
 	# Sent without safety resend
 	rpc_unreliable_id(1, "sendPlayerInputs", movementInput, jumpId,
-	attackStateInput, mouseX, mouseY)
+	attackStateInput, mouseX, mouseY, itemInput)
 
 
 puppet func backToLobby():
diff --git a/client/project.godot b/client/project.godot
index f0278d7..2b624cc 100644
--- a/client/project.godot
+++ b/client/project.godot
@@ -61,6 +61,11 @@ secondaryAttack={
 "events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":2,"pressed":false,"doubleclick":false,"script":null)
  ]
 }
+itemInput={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null)
+ ]
+}
 
 [rendering]
 
diff --git a/server/autoloads/gamestate.gd b/server/autoloads/gamestate.gd
index f4d18ae..2954893 100644
--- a/server/autoloads/gamestate.gd
+++ b/server/autoloads/gamestate.gd
@@ -11,11 +11,11 @@ const ENVIRONMENT_LAYER = 0
 const PLAYER_LAYER = 1
 
 const MAX_SPAWN_POINT = 70
-const MAX_ITEMS = 20
-var nbItemTotal = 0
+const MAX_ITEMS = 5
+var nbItemTotal:int = 0
 
 var timer = null
-var item_delay = 15
+var item_delay = 30
 signal deleteItem
 
 var rng = RandomNumberGenerator.new()
@@ -23,6 +23,9 @@ 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")
@@ -115,14 +118,14 @@ 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()
+		var itemScene = load("res://entities/items/"+typeItems[typeItem]+".gd")
 		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)
+		var newitemScene:Object = itemScene.new(nbItemTotal,typeItem,position)
+		newitemScene.set_name("item"+str(newitemScene.get_instance_id()))
+		get_node(GAMEPATH+"itemCollection").add_child(newitemScene)
+		self.connect("deleteItem",newitemScene,"on_timeout_complete")
+		newitemScene.translate(position)
 		listAvailableItem.remove(i)
 		rpc("registerItem",nbItemTotal,typeItem,position)
 		timer.start()
diff --git a/server/entities/characters/player.gd b/server/entities/characters/player.gd
index ea5cbcd..786c22d 100644
--- a/server/entities/characters/player.gd
+++ b/server/entities/characters/player.gd
@@ -1,5 +1,7 @@
 extends KinematicBody
 
+const GAMEPATH = "/root/game/"
+
 onready var ownId:int = int(name)
 
 ######## MOVEMENT AND POSITION VARS ########
@@ -136,6 +138,11 @@ enum {
 	ANIM_BIG_IMPACT = 15
 }
 
+######## ITEMS ########
+
+var typeItem = null
+var nbShot = null
+
 ######## FUNCTIONS ########
 
 # called by the engine
@@ -146,7 +153,7 @@ func _physics_process(delta:float):
 
 # Called from the game script to update the vars
 func getPlayerInputs(movementInput:int, jumpId:int, attackTypeInput:int,
-mouseX:int, mouseY:int):
+mouseX:int, mouseY:int, itemInput:bool):
 	# Prevent the player from moving when attacking
 	if (isAttacking):
 		if (is_on_floor()):
@@ -181,6 +188,14 @@ mouseX:int, mouseY:int):
 			&& 1.5*mouseX > -mouseY):
 			attackDirection = MOUSE_DOWN
 		processAttack(attackTypeInput)
+	
+	# Pick and Drop Item
+	if itemInput :
+		print("input")
+		for node in get_node(GAMEPATH+"spawnCollection/"):
+			print("node :"+node.name())
+#			node.nearItem()
+		pass
 
 
 func processMovement(delta:float):
diff --git a/server/entities/items/beer.gd b/server/entities/items/beer.gd
index 8bbdc3b..735be7e 100644
--- a/server/entities/items/beer.gd
+++ b/server/entities/items/beer.gd
@@ -4,6 +4,22 @@ const GAMEPATH = "/root/game/"
 
 const player: = preload("res://entities/characters/player.gd")
 
+var id:int
+var typeItem: int
+var position: Vector3
+
+
+func _init(id: int, typeItem: int,  position:Vector3):
+	self.id = id
+	self.typeItem = typeItem
+	self.position = position
+
+
+
+func nearItem():
+	var listBody = self.get_overlapping_bodies()
+	print(listBody)
+
 #var timer = null
 #var beer_delay = 15
 #
diff --git a/server/entities/items/trumpet.gd b/server/entities/items/trumpet.gd
index 2006681..22becae 100644
--- a/server/entities/items/trumpet.gd
+++ b/server/entities/items/trumpet.gd
@@ -4,6 +4,21 @@ const player: = preload("res://entities/characters/player.gd")
 
 const GAMEPATH = "/root/game/"
 
+
+var id:int
+var typeItem:int
+var position:Vector3
+
+
+func _init(id: int, typeItem: int,  position:Vector3):
+	self.id = id
+	self.typeItem = typeItem
+	self.position = position
+
+	
+func nearItem():
+	var listBody = self.get_overlapping_bodies()
+	print(listBody)
 #var timer = null
 #var beer_delay = 15
 #
diff --git a/server/levels/test/game.gd b/server/levels/test/game.gd
index 17b8eab..b9db2b6 100644
--- a/server/levels/test/game.gd
+++ b/server/levels/test/game.gd
@@ -1,11 +1,11 @@
 extends Node
 
-master func sendPlayerInputs(movementInput:int, jumpId:int, attackStateInput:int, mouseX:int, mouseY:int):
+master func sendPlayerInputs(movementInput:int, jumpId:int, attackStateInput:int, mouseX:int, mouseY:int, itemInput:bool):
 	# Get the input from a client and send it to the matching remote player
 	var senderId:int = get_tree().get_rpc_sender_id()
 	if gamestate.players.has(senderId):
 		gamestate.get_node("/root/game/"+str(senderId)).getPlayerInputs(movementInput,
-		jumpId, attackStateInput, mouseX, mouseY)
+		jumpId, attackStateInput, mouseX, mouseY, itemInput)
 
 
 # When the player exits the game area 
-- 
GitLab