From ff98a2de92514cbfac5de5705e44cb7117e4f511 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C2=96=C2=96ZERBIB=20TIMOTHEE?=
 <timothee.zerbib@etu.unistra.fr>
Date: Tue, 25 Feb 2020 16:09:03 +0100
Subject: [PATCH] Improved attacks

---
 client/entities/characters/player.gd |  2 +-
 server/autoloads/gamestate.gd        |  2 +-
 server/entities/characters/player.gd | 78 ++++++++++++++++++----------
 3 files changed, 52 insertions(+), 30 deletions(-)

diff --git a/client/entities/characters/player.gd b/client/entities/characters/player.gd
index 71c73b5..1dfa8a3 100644
--- a/client/entities/characters/player.gd
+++ b/client/entities/characters/player.gd
@@ -5,4 +5,4 @@ puppet func getRemoteMovement(position:Vector3):
 	self.set_translation(position)
 	
 puppet func hurt(damages:int):
-	print("You suffered a hit and lost "+str(damages)+" HPs !")
\ No newline at end of file
+	print("You suffered a hit and lost " + str(damages) + " HPs !")
\ No newline at end of file
diff --git a/server/autoloads/gamestate.gd b/server/autoloads/gamestate.gd
index af0a0e0..d28868e 100644
--- a/server/autoloads/gamestate.gd
+++ b/server/autoloads/gamestate.gd
@@ -3,7 +3,7 @@ extends Node
 const GAMEPATH = "/root/game/"
 
 const PORT = 10001
-const MAX_CLIENTS = 2
+const MAX_CLIENTS = 1
 
 var players = {}
 
diff --git a/server/entities/characters/player.gd b/server/entities/characters/player.gd
index 88004aa..f46b03f 100644
--- a/server/entities/characters/player.gd
+++ b/server/entities/characters/player.gd
@@ -2,10 +2,8 @@ extends KinematicBody
 
 onready var ownId = self.name
 
-######## MOVEMENT AND POSITION VARS ########
 
-# The side faced by the player
-var orientation:int = 1
+######## MOVEMENT AND POSITION VARS ########
 
 # Speed values
 const MAX_SPEED:float = 8.0
@@ -28,18 +26,30 @@ const GRAVITY:float = -28.0
 var vel:Vector3 = Vector3()
 
 # move_and_slide need that
-const FLOOR_NORMAL:Vector3 = Vector3(0,1,0)
+const FLOOR_NORMAL:Vector3 = Vector3(0, 1, 0)
 
-######### ATTACKS VARS ########
 
-# l2r
+######### ATTACKS VARS ########
+ 
 var isAttacking:bool = false
+onready var attackTimer:Tween = $attackTween
+
 # attack type
 enum {NONE=0, PRIMARY=1, SECONDARY=2}
-# attacks related nodes
-onready var attackTimer:Tween = $attackTween
+
+# Primary attack parameters
 onready var primaryHitArea:Area = $primaryHitArea
+var primaryAttackDmg:float = 30.0
+var primaryAttackDist:float = 1.2
+var primaryAttackDuration:float = 0.6
+
+# Secondary attack parameters
 onready var secondaryHitArea:Area = $secondaryHitArea
+var secondaryAttackDmg:float = 15.0
+var secondaryAttackDist:float = 1.2
+var secondaryAttackDuration:float = 0.3
+
+
 
 ######## FUNCTIONS ########
 
@@ -50,18 +60,26 @@ func _physics_process(delta):
 
 # Called from the game script to update the vars
 func getPlayerInputs(movementInput, sprintInput, jumpInput, attackTypeInput):
+	motion = 0
+	
+	# Check if the character isn't already attacking
+	if isAttacking:
+		return
+		
 	motion = movementInput
-	orientation = movementInput if movementInput!=0 else orientation
 	isSprinting = sprintInput
 	isJumping = jumpInput
-	if !isAttacking && attackTypeInput:
-		processAttack(attackTypeInput)
 	
+	if attackTypeInput != NONE:
+		processAttack(attackTypeInput)
+
+
 func processMovement(delta):
-	if orientation == 1:
-		set_rotation_degrees(Vector3(0,0,0))
-	else: 
-		set_rotation_degrees(Vector3(0,180,0))
+	# Set the side faced by the character
+	if motion > 0:
+		set_rotation_degrees(Vector3(0, 0, 0))
+	elif motion < 0: 
+		set_rotation_degrees(Vector3(0, 180, 0))
 	
 	if self.is_on_floor():
 		if isJumping:
@@ -80,7 +98,7 @@ func processMovement(delta):
 	
 	# Acceleration of the player if he is moving horizontally
 	var accel:float
-	var hvel:Vector3 = Vector3(vel.x,0,0)
+	var hvel:Vector3 = Vector3(vel.x, 0, 0)
 	if target.dot(hvel)>0:
 		if isSprinting:
 			accel = SPRINT_ACCEL
@@ -98,47 +116,49 @@ func processMovement(delta):
 # Send the position of the player
 func broadcastMovement():
 	rpc("getRemoteMovement", self.get_translation())
-	
+
+
 # Check and trigger the type of attack requested
 func processAttack(attackType):
 	isAttacking = true
+	
 	match attackType:
 		PRIMARY:
 			primaryAttack()
 		SECONDARY:
 			secondaryAttack()
 
+
 func primaryAttack():
 	primaryHitArea.set_monitoring(true)
 	primaryHitArea.set_visible(true)
-	var duration:float = 0.6
-	var distance:float = 1.2
 	
-	attackTimer.interpolate_property(primaryHitArea, "translation:x", 0, distance, duration, Tween.TRANS_BACK, Tween.EASE_IN)
+	attackTimer.interpolate_property(primaryHitArea, "translation:x", 0, primaryAttackDist, primaryAttackDuration, Tween.TRANS_BACK, Tween.EASE_IN)
 	attackTimer.start()
 	yield(attackTimer, "tween_completed")
 	
 	primaryHitArea.set_monitoring(false)
 	primaryHitArea.set_visible(false)
 	isAttacking = false
-	
+
+
 func secondaryAttack():
 	secondaryHitArea.set_monitoring(true)
 	secondaryHitArea.set_visible(true)
-	var duration:float = 0.3
-	var distance:float = 1.2
 	
-	attackTimer.interpolate_property(secondaryHitArea, "translation:x", 0, distance, duration, Tween.TRANS_BACK, Tween.EASE_IN)
+	attackTimer.interpolate_property(secondaryHitArea, "translation:x", 0, secondaryAttackDist, secondaryAttackDuration, Tween.TRANS_BACK, Tween.EASE_IN)
 	attackTimer.start()
 	yield(attackTimer, "tween_completed")
 	
 	secondaryHitArea.set_monitoring(false)
 	secondaryHitArea.set_visible(false)
 	isAttacking = false
-	
+
+
 func hurt(damages):
 	rpc_id(int(ownId), "hurt", str(damages));
 
+
 ######## SIGNALS ########
 
 # Primary hit has landed on something
@@ -146,13 +166,15 @@ func _on_primaryHitArea_body_entered(body):
 	if body.name==ownId:
 		return
 	
-	print("body named "+body.name+" hit: primary")
-	body.hurt(30)
+	print("body named " + body.name + " hit: primary")
+	body.hurt(primaryAttackDmg)
+
 
 # Secondary hit has landed on something
 func _on_secondaryHitArea_body_entered(body):
 	if body.name==ownId:
 		return
 	
-	print("body named "+body.name+" hit: secondary")
+	print("body named " + body.name + " hit: secondary")
+	body.hurt(secondaryAttackDmg)
 
-- 
GitLab