Skip to content

Commit

Permalink
candies steal
Browse files Browse the repository at this point in the history
  • Loading branch information
Cod3lta committed Nov 16, 2021
1 parent 341bb82 commit 47494d3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/Game.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://src/actors/candy/candy.tscn" type="PackedScene" id=1]
[ext_resource path="res://src/ui/game-hud.tscn" type="PackedScene" id=2]
[ext_resource path="res://src/levels/level-demo.tscn" type="PackedScene" id=3]
[ext_resource path="res://src/Game.gd" type="Script" id=4]
Expand All @@ -18,9 +17,6 @@ script = ExtResource( 4 )

[node name="Candies" type="YSort" parent="YSort"]

[node name="Candy" parent="YSort/Candies" instance=ExtResource( 1 )]
position = Vector2( 1056.73, -671.071 )

[node name="Map" parent="YSort" instance=ExtResource( 3 )]
position = Vector2( 6.10352e-05, 0.00012207 )

Expand Down
25 changes: 15 additions & 10 deletions src/actors/candy/candy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extends Node2D
########################################"""


var next_candy: Node2D = null
var taken_by: KinematicBody2D = null
var spawner: Node = null

Expand All @@ -22,30 +21,36 @@ puppet var puppet_targeted_pos = Vector2.ZERO
func _init():
pass


# Called when the node enters the scene tree for the first time.
func _ready():
targeted_position = position
self.puppet_targeted_pos = self.targeted_position


"""########################################
PROCESS
########################################"""


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if taken_by != null:
if is_network_master():
if is_network_master():
if taken_by != null:
assert(get_tree().is_network_server())
# self.position = taken_by.position
rset("puppet_targeted_pos", targeted_position)
else:
targeted_position = puppet_targeted_pos
else:
self.targeted_position = self.puppet_targeted_pos

self.position = lerp(self.position, self.targeted_position, 0.1)
self.position = lerp(self.position, self.targeted_position, 0.1)


# Picked up from the ground by a player
func _on_Area2D_body_entered(player):
if not player is KinematicBody2D:
return

print($Area2D.get_collision_mask())
self.taken_by = player
assert(get_tree().is_network_server())

self.set_collision_player(false) # Doesn't detect every player
player.take_candy(self)

Expand Down
3 changes: 2 additions & 1 deletion src/actors/candy/candy.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ script = ExtResource( 1 )
scale = Vector2( 10, 10 )
frames = ExtResource( 2 )
animation = "idle"
frame = 7
frame = 5
playing = true

[node name="Area2D" type="Area2D" parent="."]
collision_layer = 2
collision_mask = 0

[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource( 1 )
Expand Down
40 changes: 37 additions & 3 deletions src/actors/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func init(name: String, gender: bool, team: bool, position: Vector2):
self.gender = gender
self.team = team
self.position = position
if team:
self.set_collision_layer_bit(4, true)
else:
self.set_collision_layer_bit(3, true)


# Called when the node enters the scene tree for the first time
Expand Down Expand Up @@ -143,9 +147,39 @@ func multiplayer_movements():

func take_candy(candy: Node2D):
assert(candy is Node2D)
self.trail.insert(0, candy)
candy.set_collision_team(bool(self.team))
candy.set_color_team(bool(self.team))

var candies_to_append = Array()

if candy.taken_by != null:
# Picked up from the ground
candies_to_append = self.steal_candies(candy)
else:
# Steal the candies from another player
candies_to_append.append(candy)

candies_to_append.invert()

for c in candies_to_append:
self.trail.insert(0, c)
c.taken_by = self
c.set_collision_team(not bool(self.team))
c.set_color_team(bool(self.team))


func steal_candies(candy: Node2D) -> Array:
if not get_tree().is_network_server():
return Array()
return candy.taken_by.loose_candies(candy)


func loose_candies(from: Node2D) -> Array:
var i_start: int = trail.find(from)
if i_start == -1: return Array()

var stolen_candies = self.trail.slice(i_start, self.trail.size())
self.trail.resize(i_start)

return stolen_candies


"""########################################
Expand Down
2 changes: 1 addition & 1 deletion src/actors/player/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ shape = SubResource( 1 )
position = Vector2( 0, -3.00909 )
frames = ExtResource( 2 )
animation = "idle"
frame = 2
frame = 1
playing = true

[node name="Camera" type="Camera2D" parent="."]
Expand Down
7 changes: 6 additions & 1 deletion src/scripts/server/candySpawners/spawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ func server_spawn():
# Canned on every client
remote func spawn_candy():
var candy: Node2D = self.candy_scene.instance()
candy.position = self.position
candy.set_network_master(1)
#if get_tree().is_network_server():
# candy.position = self.position
#else:
candy.targeted_position = self.position
candy.position = self.position


if get_tree().is_network_server():
candy.spawner = self
Expand Down

0 comments on commit 47494d3

Please sign in to comment.