From e31a64624bca0dbb88db0ce92184a8baac15a9ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C2=96=C2=96ZERBIB=20TIMOTHEE?=
 <timothee.zerbib@etu.unistra.fr>
Date: Fri, 28 Feb 2020 20:41:08 +0100
Subject: [PATCH] Ignore jump if they can't be directly performed (closes #84)

---
 client/levels/test/game.gd           | 11 +++++++++--
 server/entities/characters/player.gd | 29 +++++++++++++++++++---------
 server/levels/test/game.gd           |  4 ++--
 3 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/client/levels/test/game.gd b/client/levels/test/game.gd
index afb7929..ca18260 100644
--- a/client/levels/test/game.gd
+++ b/client/levels/test/game.gd
@@ -3,15 +3,18 @@ extends Node
 # Movements
 var movementInput:int = 0
 
-var jumpId:int = 0
+var jumpInput:bool = false      # Know if the player wants to jump
+var jumpId:int = 0              # Distinguish 2 different jumps
 var sprintInput:bool = false
 
 # Attacks
 enum {NONE=0, PRIMARY=1, SECONDARY=2}
 
+
 func _physics_process(delta):
 	getPlayerInput()
 
+
 func getPlayerInput():
 	movementInput = 0
 	var attackStateInput:int = NONE
@@ -23,6 +26,10 @@ func getPlayerInput():
 
 	if Input.is_action_just_pressed("movementJump"):
 		jumpId += 1
+	if Input.is_action_pressed("movementJump"):
+		jumpInput = true
+	else:
+		jumpInput = false
 
 	if Input.is_action_pressed("movementSprint"):
 		sprintInput = true
@@ -36,4 +43,4 @@ func getPlayerInput():
 		attackStateInput = SECONDARY
 
 	# Sent without safety resend
-	rpc_unreliable_id(1, "sendPlayerInputs", movementInput, jumpId, sprintInput, attackStateInput)
+	rpc_unreliable_id(1, "sendPlayerInputs", movementInput, jumpInput, jumpId, sprintInput, attackStateInput)
diff --git a/server/entities/characters/player.gd b/server/entities/characters/player.gd
index d03c6d3..ab17038 100644
--- a/server/entities/characters/player.gd
+++ b/server/entities/characters/player.gd
@@ -48,13 +48,14 @@ var secondaryAttackDist:float = 1.2
 var secondaryAttackDuration:float = 0.3
 
 # Character informations
-var maxJump:int = 2        	# Maximum number of jump that a character can do
+var maxJump:int = 2         # Maximum number of jump that a character can do
 var currentJump:int = 0     # Actual number of jump
-var lastJumpId:int = 0		# Detect a new jump
+var lastJumpId:int = 0      # Detect a new jump
 
 var lastPunchId:int = 0     # Detect a new punch (of any type)
 var idleTime:float = 0      # Stun when hitted + prevent spam
 
+
 ######## FUNCTIONS ########
 
 # called by the engine
@@ -62,26 +63,30 @@ func _physics_process(delta):
 	processMovement(delta)
 	broadcastMovement()
 
+
 # Called from the game script to update the vars
-func getPlayerInputs(movementInput, jumpId, sprintInput, attackTypeInput):
+func getPlayerInputs(movementInput, jumpInput, jumpId, sprintInput, attackTypeInput):
 	motion = 0
 
+	# Prevent the player from moving when attacking
 	if isAttacking:
 		return
-		
+	
 	motion = movementInput
 	isSprinting = sprintInput
+	isJumping = false
 	
+	# Differentiate a new jump from a reemitted one
 	if lastJumpId != jumpId:
 		lastJumpId = jumpId
-		isJumping = true
-	else:
-		isJumping = false
+		if jumpInput:
+			isJumping = true
 
 	# Check if the character isn't already attacking
-	if attackTypeInput!=NONE:
+	if attackTypeInput != NONE:
 		processAttack(attackTypeInput)
 
+
 func processMovement(delta):
 	# Set the side faced by the character
 	if motion > 0:
@@ -89,12 +94,14 @@ func processMovement(delta):
 	elif motion < 0:
 		set_rotation_degrees(Vector3(0, 180, 0))
 
+	# Reset permitted jump if on floor
 	if self.is_on_floor():
 		currentJump = 0
 
 	if isJumping:
 		if currentJump < maxJump:
 			currentJump += 1
+			# Consider the gravity when jumping
 			vel.y = JUMP_SPEED - delta*GRAVITY
 
 	# Consider the gravity
@@ -125,6 +132,7 @@ func processMovement(delta):
 	# Update the velocity after the collisions (physical engine ftw)
 	vel = self.move_and_slide(vel, FLOOR_NORMAL)
 
+
 # Send the position of the player
 func broadcastMovement():
 	rpc("getRemoteMovement", self.get_translation())
@@ -132,7 +140,6 @@ func broadcastMovement():
 
 # Check and trigger the type of attack requested
 func processAttack(attackType):
-
 	# Turn off the other attacks input for the time being
 	isAttacking = true
 
@@ -159,6 +166,7 @@ func primaryAttack():
 	primaryHitArea.set_monitoring(false)
 	primaryHitArea.set_visible(false)
 
+
 func secondaryAttack():
 	# Set active the hitbox of the attack, its visual feedback on server
 	secondaryHitArea.set_monitoring(true)
@@ -174,10 +182,12 @@ func secondaryAttack():
 	secondaryHitArea.set_visible(false)
 	isAttacking = false
 
+
 # Called when detected by an attack's hitbox
 func hurt(damages:int):
 	rpc_id(int(ownId), "hurt", damages)
 
+
 ######## SIGNALS ########
 
 # Primary hit has landed on something
@@ -188,6 +198,7 @@ func _on_primaryHitArea_body_entered(body):
 	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:
diff --git a/server/levels/test/game.gd b/server/levels/test/game.gd
index efd4c50..6774ce0 100644
--- a/server/levels/test/game.gd
+++ b/server/levels/test/game.gd
@@ -1,7 +1,7 @@
 extends Node
 
-master func sendPlayerInputs(movementInput, jumpId, sprintInput, attackStateInput):
+master func sendPlayerInputs(movementInput, jumpInput, jumpId, sprintInput, attackStateInput):
 	
 	# Get the input from a client and send it to the matchig remote player
 	var senderId = get_tree().get_rpc_sender_id()
-	gamestate.get_node("/root/game/"+str(senderId)).getPlayerInputs(movementInput, jumpId, sprintInput, attackStateInput)
+	gamestate.get_node("/root/game/"+str(senderId)).getPlayerInputs(movementInput, jumpInput, jumpId, sprintInput, attackStateInput)
-- 
GitLab