diff --git a/client/entities/characters/player.gd b/client/entities/characters/player.gd index ddd4dad04953d30674add880868bef123094f7e9..c19b556830ce7c87d301bad111cad9523bb74a55 100644 --- a/client/entities/characters/player.gd +++ b/client/entities/characters/player.gd @@ -4,6 +4,16 @@ extends KinematicBody puppet func getRemoteMovement(position:Vector3): self.set_translation(position) -# Called by the remote player -puppet func hurt(damages:int): - print("You suffered a hit and lost " + str(damages) + " HPs !") \ No newline at end of file +# Feedback when hurt +puppet func hurt(hpNow:int): + print("You suffered a hit and are now at " + String(hpNow) + " !") + +# Feedback when killed +puppet func die(killerId:int): + print(str(killerId) + " killed " + self.name) + + # Hide the killed player (NB: the client is only about inputs and display) + #self.set_visible(false) + + + \ No newline at end of file diff --git a/client/entities/characters/player.tscn b/client/entities/characters/player.tscn index 1d62a17316f3c1b7e404cdd206abeb868248e4bb..86fecbfd34110f5ad4b5b1d66c651d069242fa4b 100644 --- a/client/entities/characters/player.tscn +++ b/client/entities/characters/player.tscn @@ -2,19 +2,19 @@ [ext_resource path="res://entities/characters/player.gd" type="Script" id=1] -[node name="Player" type="KinematicBody"] +[node name="player" type="KinematicBody"] script = ExtResource( 1 ) -[node name="Model" type="Spatial" parent="."] +[node name="model" type="Spatial" parent="."] editor/display_folded = true -[node name="CSGBox" type="CSGBox" parent="Model"] +[node name="CSGBox" type="CSGBox" parent="model"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.513621, 0 ) width = 0.35 height = 1.05787 depth = 0.684 -[node name="CSGSphere" type="CSGSphere" parent="Model"] +[node name="CSGSphere" type="CSGSphere" parent="model"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.17856, 0 ) radius = 0.2 radial_segments = 24 diff --git a/client/levels/test/game.gd b/client/levels/test/game.gd index ca1826038946735208d02c41bff95cd1d85635ba..08680e37e70bd30e3b2c5bfc31a7243f63cb1e6a 100644 --- a/client/levels/test/game.gd +++ b/client/levels/test/game.gd @@ -44,3 +44,8 @@ func getPlayerInput(): # Sent without safety resend rpc_unreliable_id(1, "sendPlayerInputs", movementInput, jumpInput, jumpId, sprintInput, attackStateInput) + +puppet func backToLobby(): + #get_node("/root/lobby").show() + get_tree().quit() + queue_free() diff --git a/server/autoloads/gamestate.gd b/server/autoloads/gamestate.gd index d28868e90829ed83914f9a1086ab113f7242354d..6454431bec4a9eab7b62a83c02df72fe9b587490 100644 --- a/server/autoloads/gamestate.gd +++ b/server/autoloads/gamestate.gd @@ -3,10 +3,13 @@ extends Node const GAMEPATH = "/root/game/" const PORT = 10001 -const MAX_CLIENTS = 1 +const MAX_CLIENTS = 2 var players = {} +const ENVIRONMENT_LAYER = 0 +const PLAYER_LAYER = 1 + func _ready(): # When a client connects to the server get_tree().connect("network_peer_connected", self, "_playerConnected") diff --git a/server/entities/characters/player.gd b/server/entities/characters/player.gd index 2dc775fb0d96779b07d0e92e5e295326acfcc873..a87dfd2b9beb688870c25bad4bf85c99f70af168 100644 --- a/server/entities/characters/player.gd +++ b/server/entities/characters/player.gd @@ -1,9 +1,11 @@ extends KinematicBody -onready var ownId = self.name +onready var ownId:int = int(self.name) ######## MOVEMENT AND POSITION VARS ######## +onready var collisionShape:CollisionShape = $collisionShape + # Speed values const MAX_SPEED:float = 8.0 const MAX_SPRINT_SPEED:float = 12.0 @@ -18,6 +20,11 @@ const DEACCEL:float = 16.0 # Jump and fall values const JUMP_SPEED:float = 16.0 var isJumping:bool = false + +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 + # TODO: unreal, we may want to stick that to the physical value const GRAVITY:float = -28.0 @@ -29,32 +36,30 @@ const FLOOR_NORMAL:Vector3 = Vector3(0,1,0) ######### ATTACKS VARS ######## +onready var lastStrikerId:int = ownId; + var isAttacking:bool = false onready var attackTimer:Tween = $attackTween # attack type enum {NONE=0, PRIMARY=1, SECONDARY=2} +var lastPunchId:int = 0 # Detect a new punch (of any type) +var idleTime:float = 0 # Stun when hitted + prevent spam # Primary attack parameters onready var primaryHitArea:Area = $primaryHitArea -var primaryAttackDmg:float = 30.0 +var primaryAttackDmg:int = 30 var primaryAttackDist:float = 1.2 -var primaryAttackDuration:float = 0.6 +var primaryAttackDuration:float = 0.4 # Secondary attack parameters onready var secondaryHitArea:Area = $secondaryHitArea -var secondaryAttackDmg:float = 15.0 +var secondaryAttackDmg:int = 15 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 currentJump:int = 0 # Actual number of 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 +var secondaryAttackDuration:float = 0.2 +# Health points related values +var hp:int = 0 ######## FUNCTIONS ######## @@ -94,10 +99,13 @@ func processMovement(delta): elif motion < 0: set_rotation_degrees(Vector3(0, 180, 0)) - # Reset permitted jump if on floor and decrease max number of jumps if falling if self.is_on_floor(): + # Reset the number of jumps left currentJump = 0 + # Reset the last striker + lastStrikerId = ownId elif currentJump == 0 && !isJumping: + # Initial jump prevented currentJump = 1 # Jump attempts computation @@ -187,25 +195,31 @@ func secondaryAttack(): # Called when detected by an attack's hitbox -func hurt(damages:int): - rpc_id(int(ownId), "hurt", damages) +func hurt(damages:int, sourceId:int): + hp += damages + lastStrikerId = sourceId + rpc_unreliable_id(ownId, "hurt", hp) + +func die(): + rpc("die", lastStrikerId) + self.queue_free() ######## SIGNALS ######## # Primary hit has landed on something func _on_primaryHitArea_body_entered(body): - if body.name==ownId: + if int(body.name)==ownId: return print("body named " + body.name + " hit: primary") - body.hurt(primaryAttackDmg) + body.hurt(primaryAttackDmg, ownId) # Secondary hit has landed on something func _on_secondaryHitArea_body_entered(body): - if body.name==ownId: + if int(body.name)==ownId: return print("body named " + body.name + " hit: secondary") - body.hurt(secondaryAttackDmg) + body.hurt(secondaryAttackDmg, ownId) diff --git a/server/entities/characters/player.tscn b/server/entities/characters/player.tscn index 2c3ed4a4b6efdc7d8a1dfeb5182bfc99d34f19ac..eb94c6d53948e8b87d9cb78049ef7cc354d60632 100644 --- a/server/entities/characters/player.tscn +++ b/server/entities/characters/player.tscn @@ -8,24 +8,24 @@ extents = Vector3( 0.175, 0.275, 0.7 ) [sub_resource type="BoxShape" id=2] extents = Vector3( 0.544245, 0.192381, 0.312851 ) -[node name="Player" type="KinematicBody"] +[node name="player" type="KinematicBody"] collision_layer = 2 collision_mask = 3 script = ExtResource( 1 ) -[node name="Body_CollisionShape" type="CollisionShape" parent="."] +[node name="collisionShape" type="CollisionShape" parent="."] transform = Transform( 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0.65, 0 ) shape = SubResource( 1 ) -[node name="Model" type="Spatial" parent="."] +[node name="model" type="Spatial" parent="."] -[node name="CSGBox" type="CSGBox" parent="Model"] +[node name="CSGBox" type="CSGBox" parent="model"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.513621, 0 ) width = 0.35 height = 1.05787 depth = 0.684 -[node name="CSGSphere" type="CSGSphere" parent="Model"] +[node name="CSGSphere" type="CSGSphere" parent="model"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.17856, 0 ) radius = 0.2 radial_segments = 24 diff --git a/server/levels/test/game.gd b/server/levels/test/game.gd index 6774ce082ba9961cce19907ea66c78767cfb5480..86489f1ce45bb299a628551eb2bd362675d6e1f0 100644 --- a/server/levels/test/game.gd +++ b/server/levels/test/game.gd @@ -1,7 +1,18 @@ extends Node -master func sendPlayerInputs(movementInput, jumpInput, jumpId, sprintInput, attackStateInput): +master func sendPlayerInputs(movementInput:int, jumpInput:bool, jumpId:int, sprintInput:bool, attackStateInput:int): - # 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, jumpInput, jumpId, sprintInput, attackStateInput) + # 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, jumpInput, jumpId, sprintInput, attackStateInput) + + +# When the player exits the game area +func _on_killingFallArea_body_exited(body:PhysicsBody): + # Instant-kill if the body is a living player + if body.get_collision_layer_bit(gamestate.PLAYER_LAYER): + gamestate.players.erase(body.ownId) + rpc_id(body.ownId, "backToLobby") + #gamestate.disconnect_network_peer(body.ownId) + body.die() diff --git a/server/levels/test/game.tscn b/server/levels/test/game.tscn index a21c3a3fb343d5e8115c0e8038b795a59cd4d8c2..0e4f9308d78685900291e615b8732176abe9f014 100644 --- a/server/levels/test/game.tscn +++ b/server/levels/test/game.tscn @@ -1,8 +1,11 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://levels/test/game.gd" type="Script" id=1] [ext_resource path="res://levels/test/bloc/bloc.tscn" type="PackedScene" id=2] +[sub_resource type="BoxShape" id=1] +extents = Vector3( 175.136, 87.8901, 1 ) + [node name="game" type="Spatial"] script = ExtResource( 1 ) @@ -19,6 +22,7 @@ transform = Transform( 10, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ) transform = Transform( 5, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4.15191, 0 ) [node name="spawnCollection" type="Spatial" parent="."] +transform = Transform( 0.996196, -0.0845788, 0.0209877, 0.084609, 0.996414, -0.000553806, -0.0208656, 0.00232744, 0.99978, 0, 0, 0 ) [node name="spawn1" type="Position3D" parent="spawnCollection"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 6, 0 ) @@ -28,3 +32,10 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 6, 0 ) [node name="spawn3" type="Position3D" parent="spawnCollection"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 6, 0 ) + +[node name="killingFallArea" type="Area" parent="."] + +[node name="CollisionShape" type="CollisionShape" parent="killingFallArea"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.208731, 0 ) +shape = SubResource( 1 ) +[connection signal="body_exited" from="killingFallArea" to="." method="_on_killingFallArea_body_exited"]