Skip to content

Commit

Permalink
Merge pull request #121 from NancokPS2/fix-colls
Browse files Browse the repository at this point in the history
Changed how entities collide with each other (fixes players pushing each other)
  • Loading branch information
jonathaneeckhout authored Oct 26, 2023
2 parents 0b73bbc + e3b22ed commit 7e30d69
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends ConditionLeaf
@export var aggro_radius: Area2D = null
@export var should_leash := false
@export var leash_distance: float = 350.0
@export var targetable_entity_type: J.ENTITY_TYPE = J.ENTITY_TYPE.PLAYER
@onready var reset_timer: Timer = $ResetTimer
var start_combat := false
var in_combat := false
Expand Down Expand Up @@ -50,7 +51,7 @@ func tick(actor: Node, blackboard: Blackboard):


func _on_body_entered(body: Node2D):
if target == null:
if target == null and body.get("entity_type") == targetable_entity_type:
start_combat = true
target = body

Expand Down
43 changes: 35 additions & 8 deletions scripts/classes/JBody2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ var is_dead := false


func _init():
collision_layer = J.PHYSICS_LAYER_WORLD

if J.is_server():
collision_mask = J.PHYSICS_LAYER_WORLD
else:
# Don't handle collision on client side
collision_mask = 0

synchronizer = load("res://scripts/classes/JSynchronizer.gd").new()
synchronizer.name = "Synchronizer"
synchronizer.to_be_synced = self
Expand All @@ -33,6 +25,41 @@ func _init():
stats.parent = self
add_child(stats)

#Call this at the last moment, prevents possible errors from non-ready collisions and allows overriden _init methods to run first.
_init_collisions.call_deferred()


func _init_collisions():
#Each creature only exists on their own layer, they collide with the world thanks to their collision_mask
match entity_type:
J.ENTITY_TYPE.PLAYER:
collision_layer = J.PHYSICS_LAYER_PLAYERS
J.ENTITY_TYPE.ENEMY:
collision_layer = J.PHYSICS_LAYER_ENEMIES
J.ENTITY_TYPE.NPC:
collision_layer = J.PHYSICS_LAYER_NPCS

if J.is_server():
#By default, only collide with the world. No other entities.
collision_mask = J.PHYSICS_LAYER_WORLD

#Set what it will collide with
match entity_type:
#The player cannot walk past NPCs and enemies. But other players cannot block their path.
J.ENTITY_TYPE.PLAYER:
collision_mask += J.PHYSICS_LAYER_ENEMIES + J.PHYSICS_LAYER_NPCS

#Enemies can be blocked by NPCs and players.
J.ENTITY_TYPE.ENEMY:
collision_mask += J.PHYSICS_LAYER_PLAYERS + J.PHYSICS_LAYER_NPCS

#NPCs cannot be stopped by any entity.
J.ENTITY_TYPE.NPC:
collision_mask = 0
else:
# Don't handle collision on client side
collision_mask = 0


func _ready():
if not J.is_server() and J.client.player:
Expand Down
2 changes: 0 additions & 2 deletions scripts/classes/JEnemyBody2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func _init():

entity_type = J.ENTITY_TYPE.ENEMY

collision_layer += J.PHYSICS_LAYER_ENEMIES

if J.is_server():
despawn_timer = Timer.new()
despawn_timer.name = "DespawnTimer"
Expand Down
2 changes: 0 additions & 2 deletions scripts/classes/JNPCBody2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ func _init():

entity_type = J.ENTITY_TYPE.NPC

collision_layer += J.PHYSICS_LAYER_NPCS

if J.is_server():
shop = JShop.new()
shop.name = "Shop"
Expand Down
2 changes: 0 additions & 2 deletions scripts/classes/JPlayerBody2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ func _init():

entity_type = J.ENTITY_TYPE.PLAYER

collision_layer += J.PHYSICS_LAYER_PLAYERS

player_synchronizer = JPlayerSynchronizer.new()
player_synchronizer.name = "PlayerSynchronizer"
player_synchronizer.player = self
Expand Down

0 comments on commit 7e30d69

Please sign in to comment.