Skip to content
Snippets Groups Projects
Commit bf2a91bc authored by Canopteks's avatar Canopteks
Browse files

pre-merge develop/attacks issue

parents 3630b3d1 0f22fd52
No related merge requests found
...@@ -11,11 +11,11 @@ signal server_disconnected ...@@ -11,11 +11,11 @@ signal server_disconnected
func joinServer(ip, port): func joinServer(ip, port):
# Create the network client # Create the network client
var peer_join = NetworkedMultiplayerENet.new() var peer_join:NetworkedMultiplayerENet = NetworkedMultiplayerENet.new()
peer_join.create_client(ip, int(port)) peer_join.create_client(ip, int(port))
get_tree().set_network_peer(peer_join) get_tree().set_network_peer(peer_join)
print("Trying to join the server ", ip, ":", port) print("Trying to join the server ", ip, ":", port, " with id: ", get_tree().get_network_unique_id())
puppet func registerPlayer(id): puppet func registerPlayer(id):
...@@ -37,8 +37,8 @@ puppet func startGame(): ...@@ -37,8 +37,8 @@ puppet func startGame():
get_node("/root/lobby").hide() get_node("/root/lobby").hide()
var game = preload("res://levels/test/game.tscn").instance() var game:Node = preload("res://levels/test/game.tscn").instance()
var player_scene = preload("res://entities/characters/player.tscn") var player_scene:PackedScene = preload("res://entities/characters/player.tscn")
get_tree().get_root().add_child(game) get_tree().get_root().add_child(game)
# TODO: check (and perhaps) the scene changing functionality # TODO: check (and perhaps) the scene changing functionality
...@@ -47,16 +47,16 @@ puppet func startGame(): ...@@ -47,16 +47,16 @@ puppet func startGame():
#Next evey player will spa every other player including the server's own client! Try to move this to server only #Next evey player will spa every other player including the server's own client! Try to move this to server only
var i = 1 var i = 1
for peer_id in players: for peer_id in players:
var player = player_scene.instance() var player:Node = player_scene.instance()
player.set_name(str(peer_id)) player.set_name(str(peer_id))
get_node(GAMEPATH).add_child(player) get_node(GAMEPATH).add_child(player)
var spawnPosition = get_node("/root/game/spawnCollection/spawn"+str(i)).get_translation() var spawnPosition:Vector3 = get_node("/root/game/spawnCollection/spawn"+str(i)).get_translation()
player.translate(spawnPosition) player.translate(spawnPosition)
i+=1 i+=1
var playerArrow = preload("res://entities/characters/playerArrow.tscn").instance() var playerArrow:Node = preload("res://entities/characters/playerArrow.tscn").instance()
get_node(GAMEPATH+str(get_tree().get_network_unique_id())).add_child(playerArrow) get_node(GAMEPATH+str(get_tree().get_network_unique_id())).add_child(playerArrow)
\ No newline at end of file
extends KinematicBody extends KinematicBody
# Update the player's position based on the position computed by the remote scene on the server # Update the player's position based on the position computed by the remote scene on the server
puppet func getRemoteMovement(position): puppet func getRemoteMovement(position:Vector3):
self.set_translation(position) self.set_translation(position)
\ No newline at end of file
# 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
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
script = ExtResource( 1 ) 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 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.513621, 0 )
......
...@@ -10,13 +10,13 @@ func _ready(): ...@@ -10,13 +10,13 @@ func _ready():
func _on_joinButton_pressed(): func _on_joinButton_pressed():
# Get and check the Ip # Get and check the Ip
var ip = get_node("menu/ipLineEdit").text var ip:String = get_node("menu/ipLineEdit").text
if not ip.is_valid_ip_address(): if not ip.is_valid_ip_address():
get_node("menu/errorLabel").text = "Invalid IPv4 address!" get_node("menu/errorLabel").text = "Invalid IPv4 address!"
return return
# Get and check the port # Get and check the port
var port = get_node("menu/portLineEdit").text var port:String = get_node("menu/portLineEdit").text
if port == "": if port == "":
get_node("menu/errorLabel").text = "Invalid port!" get_node("menu/errorLabel").text = "Invalid port!"
return return
......
extends Node extends Node
# Movements
var movementInput:int = 0 var movementInput:int = 0
var jumpId:int = 0 var jumpId:int = 0
var sprintInput:bool = false var sprintInput:bool = false
# Attacks
enum {NONE=0, PRIMARY=1, SECONDARY=2}
func _physics_process(delta): func _physics_process(delta):
getPlayerInput() getPlayerInput()
func getPlayerInput(): func getPlayerInput():
movementInput = 0 movementInput = 0
var attackStateInput:int = NONE
if Input.is_action_pressed("movementLeft"): if Input.is_action_pressed("movementLeft"):
movementInput -= 1 movementInput -= 1
if Input.is_action_pressed("movementRight"): if Input.is_action_pressed("movementRight"):
movementInput += 1 movementInput += 1
if Input.is_action_just_pressed("movementJump"): if Input.is_action_just_pressed("movementJump"):
jumpId += 1 jumpId += 1
if Input.is_action_pressed("movementSprint"): if Input.is_action_pressed("movementSprint"):
sprintInput = true sprintInput = true
else: else:
sprintInput = false sprintInput = false
if Input.is_action_pressed("primaryAttack"):
attackStateInput = PRIMARY
elif Input.is_action_pressed("secondaryAttack"):
attackStateInput = SECONDARY
# Sent without safety resend # Sent without safety resend
rpc_unreliable_id(1, "sendPlayerInputs", movementInput, sprintInput, jumpId) rpc_unreliable_id(1, "sendPlayerInputs", movementInput, sprintInput, jumpInput, attackStateInput)
\ No newline at end of file
...@@ -48,6 +48,16 @@ movementSprint={ ...@@ -48,6 +48,16 @@ movementSprint={
"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":16777237,"unicode":0,"echo":false,"script":null) "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":16777237,"unicode":0,"echo":false,"script":null)
] ]
} }
primaryAttack={
"deadzone": 0.5,
"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":1,"pressed":false,"doubleclick":false,"script":null)
]
}
secondaryAttack={
"deadzone": 0.5,
"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)
]
}
[rendering] [rendering]
......
extends KinematicBody extends KinematicBody
onready var ownId = self.name
######## MOVEMENT AND POSITION VARS ########
# Speed values # Speed values
const MAX_SPEED:float = 8.0 const MAX_SPEED:float = 8.0
const MAX_SPRINT_SPEED:float = 12.0 const MAX_SPRINT_SPEED:float = 12.0
...@@ -21,8 +26,28 @@ const GRAVITY:float = -28.0 ...@@ -21,8 +26,28 @@ const GRAVITY:float = -28.0
var vel:Vector3 = Vector3(0,0,0) var vel:Vector3 = Vector3(0,0,0)
# move_and_slide need that # move_and_slide need that
const FLOOR_NORMAL:Vector3 = Vector3(0,1,0) const FLOOR_NORMAL:Vector3 = Vector3(0, 1, 0)
######### ATTACKS VARS ########
var isAttacking:bool = false
onready var attackTimer:Tween = $attackTween
# attack type
enum {NONE=0, PRIMARY=1, SECONDARY=2}
# 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
# Character informations # Character informations
var damages:int = 0 var damages:int = 0
...@@ -34,49 +59,57 @@ var lastJumpId:int = 0 # Detect a new jump ...@@ -34,49 +59,57 @@ var lastJumpId:int = 0 # Detect a new jump
var lastPunchId:int = 0 # Detect a new punch (of any type) var lastPunchId:int = 0 # Detect a new punch (of any type)
var idleTime:float = 0 # Stun when hitted + prevent spam var idleTime:float = 0 # Stun when hitted + prevent spam
######## FUNCTIONS ########
# called by the engine # called by the engine
func _physics_process(delta): func _physics_process(delta):
processMovement(delta) processMovement(delta)
broadcastMovement(delta) broadcastMovement()
# Called from the game script to update the vars # Called from the game script to update the vars
func getPlayerInputs(movementInput, sprintInput, jumpId): func getPlayerInputs(movementInput, sprintInput, jumpInput, attackTypeInput):
if lastJumpId != jumpId: motion = 0
lastJumpId = jumpId
isJumping = true # Check if the character isn't already attacking
else: if isAttacking:
isJumping = false return
motion = movementInput motion = movementInput
isSprinting = sprintInput isSprinting = sprintInput
isJumping = jumpInput
if attackTypeInput != NONE:
processAttack(attackTypeInput)
func processMovement(delta): func processMovement(delta):
# reset max_jump # 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 self.is_on_floor():
currentJump = 0 currentJump = 0
if isJumping: if isJumping:
if currentJump < maxJump: if currentJump < maxJump:
currentJump += 1 currentJump += 1
vel.y = JUMP_SPEED - delta*GRAVITY vel.y = JUMP_SPEED - delta*GRAVITY
# Consider the gravity # Consider the gravity
vel.y += delta * GRAVITY vel.y += delta * GRAVITY
# Speed of the player # Speed of the player
var target = Vector3(motion, 0, 0) var target:Vector3 = Vector3(motion, 0, 0)
if isSprinting: if isSprinting:
target *= MAX_SPRINT_SPEED target *= MAX_SPRINT_SPEED
else: else:
target *= MAX_SPEED target *= MAX_SPEED
# Acceleration of the player if he is moving horizontally # Acceleration of the player if he is moving horizontally
var accel 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 target.dot(hvel)>0:
if isSprinting: if isSprinting:
accel = SPRINT_ACCEL accel = SPRINT_ACCEL
...@@ -84,13 +117,80 @@ func processMovement(delta): ...@@ -84,13 +117,80 @@ func processMovement(delta):
accel = ACCEL accel = ACCEL
else: else:
accel = DEACCEL accel = DEACCEL
# Interpolating speed and acceleration through time and getting it # Interpolating speed and acceleration through time and getting it
vel.x = hvel.linear_interpolate(target, accel * delta).x vel.x = hvel.linear_interpolate(target, accel * delta).x
# Update the velocity after the collisions (physical engine ftw) # Update the velocity after the collisions (physical engine ftw)
vel = self.move_and_slide(vel, FLOOR_NORMAL) vel = self.move_and_slide(vel, FLOOR_NORMAL)
func broadcastMovement(delta): # Send the position of the player
# Send the position of the player func broadcastMovement():
rpc("getRemoteMovement", self.get_translation()) rpc("getRemoteMovement", self.get_translation())
\ No newline at end of file
# Check and trigger the type of attack requested
func processAttack(attackType):
# Turn off the other attacks input for the time being
isAttacking = true
# Use the right function
match attackType:
PRIMARY:
primaryAttack()
SECONDARY:
secondaryAttack()
func primaryAttack():
# Set active the hitbox of the attack, its visual feedback on server
primaryHitArea.set_monitoring(true)
primaryHitArea.set_visible(true)
# Prepare the translation as an interpolation over time (end position, duration and function used)
attackTimer.interpolate_property(primaryHitArea, "translation:x", 0, primaryAttackDist, primaryAttackDuration, Tween.TRANS_BACK, Tween.EASE_IN)
attackTimer.start()
# Once the movement is finished, turn off the hitbox and visual feedback
yield(attackTimer, "tween_completed")
isAttacking = false
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)
secondaryHitArea.set_visible(true)
# Prepare the translation as an interpolation over time (end position, duration and function used)
attackTimer.interpolate_property(secondaryHitArea, "translation:x", 0, secondaryAttackDist, secondaryAttackDuration, Tween.TRANS_BACK, Tween.EASE_IN)
attackTimer.start()
# Once the movement is finished, turn off the hitbox and visual feedback
yield(attackTimer, "tween_completed")
secondaryHitArea.set_monitoring(false)
secondaryHitArea.set_visible(false)
isAttacking = false
# Called when detected by an attack's hitbox
func hurt(damages):
rpc_id(int(ownId), "hurt", damages);
######## SIGNALS ########
# Primary hit has landed on something
func _on_primaryHitArea_body_entered(body):
if body.name==ownId:
return
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")
body.hurt(secondaryAttackDmg)
[gd_scene load_steps=3 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://entities/characters/Class1/class1.gd" type="Script" id=1] [ext_resource path="res://entities/characters/Class1/class1.gd" type="Script" id=1]
[sub_resource type="BoxShape" id=1] [sub_resource type="BoxShape" id=1]
extents = Vector3( 0.175, 0.275, 0.7 ) 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 ) script = ExtResource( 1 )
[node name="Body_CollisionShape" type="CollisionShape" parent="."] [node name="Body_CollisionShape" type="CollisionShape" parent="."]
...@@ -24,3 +29,39 @@ depth = 0.684 ...@@ -24,3 +29,39 @@ depth = 0.684
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.17856, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.17856, 0 )
radius = 0.2 radius = 0.2
radial_segments = 24 radial_segments = 24
[node name="primaryHitArea" type="Area" parent="."]
visible = false
monitoring = false
collision_layer = 0
collision_mask = 2
[node name="primaryHitCol" type="CollisionShape" parent="primaryHitArea"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.420072, 0.815, 0 )
shape = SubResource( 2 )
[node name="primaryHitCSG" type="CSGBox" parent="primaryHitArea"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.419673, 0.815, 0 )
width = 1.09437
height = 0.400067
depth = 0.618424
[node name="secondaryHitArea" type="Area" parent="."]
visible = false
monitoring = false
collision_layer = 0
collision_mask = 2
[node name="secondaryHitCol" type="CollisionShape" parent="secondaryHitArea"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.42, 0.2, 0 )
shape = SubResource( 2 )
[node name="secondaryHitCSG" type="CSGBox" parent="secondaryHitArea"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.42, 0.2, 0 )
width = 1.09437
height = 0.400067
depth = 0.618424
[node name="attackTween" type="Tween" parent="."]
[connection signal="body_entered" from="primaryHitArea" to="." method="_on_primaryHitArea_body_entered"]
[connection signal="body_entered" from="secondaryHitArea" to="." method="_on_secondaryHitArea_body_entered"]
...@@ -12,6 +12,7 @@ mesh = SubResource( 1 ) ...@@ -12,6 +12,7 @@ mesh = SubResource( 1 )
material/0 = null material/0 = null
[node name="StaticBody" type="StaticBody" parent="MeshInstance"] [node name="StaticBody" type="StaticBody" parent="MeshInstance"]
collision_mask = 2
[node name="CollisionShape" type="CollisionShape" parent="MeshInstance/StaticBody"] [node name="CollisionShape" type="CollisionShape" parent="MeshInstance/StaticBody"]
shape = SubResource( 2 ) shape = SubResource( 2 )
extends Node extends Node
master func sendPlayerInputs(movementInput, sprintInput, jumpId): master func sendPlayerInputs(movementInput, jumpInput, sprintInput, attackStateInput):
# Get the input from a client and send it to the matchig remote player # Get the input from a client and send it to the matchig remote player
var senderId = get_tree().get_rpc_sender_id() var senderId = get_tree().get_rpc_sender_id()
gamestate.get_node("/root/game/"+str(senderId)).getPlayerInputs(movementInput, sprintInput, jumpId) gamestate.get_node("/root/game/"+str(senderId)).getPlayerInputs(movementInput, jumpInput, sprintInput, attackStateInput)
\ No newline at end of file
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
[ext_resource path="res://levels/test/game.gd" type="Script" id=1] [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] [ext_resource path="res://levels/test/bloc/bloc.tscn" type="PackedScene" id=2]
[node name="game" type="Spatial"] [node name="game" type="Spatial"]
script = ExtResource( 1 ) script = ExtResource( 1 )
......
...@@ -23,6 +23,11 @@ config/icon="res://icon.png" ...@@ -23,6 +23,11 @@ config/icon="res://icon.png"
gamestate="*res://autoloads/gamestate.gd" gamestate="*res://autoloads/gamestate.gd"
[layer_names]
3d_physics/layer_1="environment"
3d_physics/layer_2="players"
[rendering] [rendering]
environment/default_environment="res://default_env.tres" environment/default_environment="res://default_env.tres"
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment