Skip to content

Commit

Permalink
Implemented side stepping to make room for other entities on same tile
Browse files Browse the repository at this point in the history
This implementation isn't perfect yet (there are still glitches sometimes, especially when the player or multiple entities are involved) but it works for now.
  • Loading branch information
T0astBread committed Nov 4, 2018
1 parent 720b41c commit 5a52775
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 50 deletions.
9 changes: 7 additions & 2 deletions Assets/Prefabs/Enemy.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Transform:
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1608370292961522}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -5, y: -7.5, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down Expand Up @@ -90,7 +90,12 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
stepTime: 0.28
mapPosition: {x: 5, y: 10}
movementLocks: 0
mapPosition: {x: 0, y: 0}
positionTile: {fileID: 0}
isAtSideOfTile: 0
offsetInTileWhenMovingToSide: {x: 0, y: 0}
offsetDistanceWhenMovingToSide: 0.35
--- !u!114 &114104290551586470
MonoBehaviour:
m_ObjectHideFlags: 1
Expand Down
73 changes: 72 additions & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,62 @@ Transform:
m_Father: {fileID: 716157404}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &704590077
Prefab:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalPosition.x
value: -4
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalPosition.y
value: -7
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4459018764244518, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_RootOrder
value: 3
objectReference: {fileID: 0}
- target: {fileID: 1608370292961522, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
propertyPath: m_Name
value: Enemy (1)
objectReference: {fileID: 0}
- target: {fileID: 114062212567018792, guid: a09ca902c908a1e4db37214a78e81f3a,
type: 2}
propertyPath: mapPosition.x
value: 5
objectReference: {fileID: 0}
- target: {fileID: 114062212567018792, guid: a09ca902c908a1e4db37214a78e81f3a,
type: 2}
propertyPath: mapPosition.y
value: 9
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
m_IsPrefabAsset: 0
--- !u!1 &716157402
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -220,7 +276,12 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
stepTime: 0.5
movementLocks: 0
mapPosition: {x: 1, y: 16}
positionTile: {fileID: 0}
isAtSideOfTile: 0
offsetInTileWhenMovingToSide: {x: 0, y: 0}
offsetDistanceWhenMovingToSide: 0.35
blockedStepTime: 0.25
blockedStepXMovement: 0.1
--- !u!4 &716157404
Expand Down Expand Up @@ -372,7 +433,7 @@ SpriteRenderer:
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 189609561
m_SortingLayerID: 742366935
m_SortingLayer: 2
m_SortingOrder: 0
m_Sprite: {fileID: 21300002, guid: b98b90b3e25054d419f497080f54b806, type: 3}
Expand Down Expand Up @@ -455,6 +516,16 @@ Prefab:
propertyPath: m_RootOrder
value: 2
objectReference: {fileID: 0}
- target: {fileID: 114062212567018792, guid: a09ca902c908a1e4db37214a78e81f3a,
type: 2}
propertyPath: mapPosition.x
value: 5
objectReference: {fileID: 0}
- target: {fileID: 114062212567018792, guid: a09ca902c908a1e4db37214a78e81f3a,
type: 2}
propertyPath: mapPosition.y
value: 10
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: a09ca902c908a1e4db37214a78e81f3a, type: 2}
m_IsPrefabAsset: 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class DirectionalAnimationControllerBehaviour : MoveListenerBehaviour
public class DirectionalAnimationControllerBehaviour : MonoBehaviour, IMoveListener
{
public RuntimeAnimatorController northEastController, southEastController, southWestController, northWestController;

Expand All @@ -13,9 +13,10 @@ void Start()
this.animator = GetComponent<Animator>();
}

override public void BeforeMove(Vector2 movement)
public void BeforeMove(Vector2 movement)
{
SetControllerDirection(movement);
if (this.enabled)
SetControllerDirection(movement);
}

public void SetControllerDirection(Vector2 direction)
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Behaviours/DirectionalSpriteBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class DirectionalSpriteBehaviour : MoveListenerBehaviour
public class DirectionalSpriteBehaviour : MonoBehaviour, IMoveListener
{
public const int SOUTH_EAST = 0, SOUTH_WEST = 1, NORTH_EAST = 2, NORTH_WEST = 3;

Expand All @@ -15,7 +15,7 @@ void Start()
this.spriteRenderer = GetComponent<SpriteRenderer>();
}

override public void BeforeMove(Vector2 movement)
public void BeforeMove(Vector2 movement)
{
if (this.enabled)
SetSpriteDirection(movement);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Behaviours/JumpBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IEnumerator Jump(float height, float duration)

if (this.resetPositionAfterJump)
{
transform.position = Map.RoundToGridPosition(transform.position);
transform.localPosition = Vector3.zero;
}
}

Expand Down
9 changes: 2 additions & 7 deletions Assets/Scripts/Behaviours/LinearMovementBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

public class LinearMovementBehaviour : MonoBehaviour
{
private MoveListenerBehaviour[] listeners;
private IMoveListener[] listeners;

void Start()
{
this.listeners = GetComponentsInChildren<MoveListenerBehaviour>();
this.listeners = GetComponentsInChildren<IMoveListener>();
}

public IEnumerator Move(Vector2 newMapPosition, Vector2 offsetInTile, float duration)
Expand All @@ -33,10 +33,5 @@ public IEnumerator Move(Vector2 newMapPosition, Vector2 offsetInTile, float dura
transform.position += movementDelta;
yield return null;
}

foreach (var listener in this.listeners)
{
listener.AfterMove(movement);
}
}
}
14 changes: 0 additions & 14 deletions Assets/Scripts/Behaviours/MoveListenerBehaviour.cs

This file was deleted.

21 changes: 16 additions & 5 deletions Assets/Scripts/Entities/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@ public class Enemy : Entity

private JumpBehaviour jumpBehaviour;
private LinearMovementBehaviour moveBehaviour;
private RuntimeAnimatorController runtimeAnimator;
private DirectionalAnimationControllerBehaviour directionalAnimationBehaviour;

void Start()
override public void Start()
{
base.Start();
this.jumpBehaviour = GetComponentInChildren<JumpBehaviour>();
this.moveBehaviour = GetComponent<LinearMovementBehaviour>();
this.runtimeAnimator = GetComponent<Animator>().runtimeAnimatorController;
this.directionalAnimationBehaviour = GetComponent<DirectionalAnimationControllerBehaviour>();
}

public override IEnumerator StepTo(Vector2Int newMapPosition, Vector2 offsetInTile)
public override IEnumerator ExecuteStepTo(Vector2Int newMapPosition, Vector2 offsetInTile)
{
yield return new WaitForSeconds(.14f);
StartCoroutine(this.jumpBehaviour.Jump(this.jumpBehaviour.jumpHeight, this.stepTime));
yield return this.moveBehaviour.Move(newMapPosition, offsetInTile, this.stepTime);
this.mapPosition = newMapPosition;
}

protected override void BeforeSideStep()
{
this.directionalAnimationBehaviour.SetControllerDirection(-this.offsetInTileWhenMovingToSide);
this.directionalAnimationBehaviour.enabled = false;
}

protected override void AfterSideStep()
{
this.directionalAnimationBehaviour.enabled = true;
}
}
87 changes: 85 additions & 2 deletions Assets/Scripts/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,90 @@
public abstract class Entity : MonoBehaviour
{
public float stepTime = .5f;
public Vector2Int mapPosition = Vector2Int.zero;
public int movementLocks = 0;
public Vector2Int mapPosition;
public Tile positionTile;
public bool isAtSideOfTile = false;
public Vector2 offsetInTileWhenMovingToSide;
public float offsetDistanceWhenMovingToSide = .35f;

public abstract IEnumerator StepTo(Vector2Int newMapPosition, Vector2 offsetInTile);
public IEnumerator StepTo(Vector2Int newMapPosition, Vector2 offsetInTile)
{
if (this.movementLocks <= 0)
{
this.movementLocks++;
UnregisterMapPosition();
yield return ExecuteStepTo(newMapPosition, offsetInTile);
RegisterNewMapPosition(newMapPosition);
this.movementLocks--;
}
}

public abstract IEnumerator ExecuteStepTo(Vector2Int newMapPosition, Vector2 offsetInTile);

private void UnregisterMapPosition()
{
if (this.positionTile != null)
{
this.positionTile.EntitiesOnTile.Remove(this);
}
}

private void RegisterNewMapPosition(Vector2Int newMapPosition)
{
Tile newPositionTile;
if (Map.Tiles.TryGetValue(newMapPosition, out newPositionTile))
{
this.positionTile = newPositionTile;
this.positionTile.EntitiesOnTile.Add(this);
}
this.mapPosition = newMapPosition;
}

public virtual void Start()
{
RegisterNewMapPosition(this.mapPosition);
}

public virtual void Update()
{
if (this.isAtSideOfTile)
{
if (this.positionTile != null && this.positionTile.EntitiesOnTile.Count <= 1)
{
StartCoroutine(StepToCenterOfTile());
}
}
else if (this.positionTile != null && this.positionTile.EntitiesOnTile.Count > 1 && this.offsetInTileWhenMovingToSide != null)
{
StartCoroutine(StepToSideOfTile());
}
}

public IEnumerator StepToSideOfTile()
{
if (this.movementLocks <= 0)
{
this.movementLocks++;
this.isAtSideOfTile = true;
BeforeSideStep();
yield return ExecuteStepTo(this.mapPosition, this.offsetInTileWhenMovingToSide);
AfterSideStep();
this.movementLocks--;
}
}

protected virtual void BeforeSideStep() { }
protected virtual void AfterSideStep() { }

private IEnumerator StepToCenterOfTile()
{
if (this.movementLocks <= 0)
{
this.movementLocks++;
this.isAtSideOfTile = false;
yield return ExecuteStepTo(this.mapPosition, Vector2.zero);
this.movementLocks--;
}
}
}
Loading

1 comment on commit 5a52775

@T0astBread
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visualization of Changes

GIFs showing this commit's changes in action

Desired operation

5a527753d05d3d3a18d40f80ce1a8647c11bf322_1
The player stepping on a tile causes the enemy to side step

5a527753d05d3d3a18d40f80ce1a8647c11bf322
The player and an enemy jump through each other then the player steps on the enemy's tile causing it to side step

5a527753d05d3d3a18d40f80ce1a8647c11bf322_2
An enemy steps on another enemy's tile causing it to side step

Oddities/potentially undesired

5a527753d05d3d3a18d40f80ce1a8647c11bf322_oddities
The player pushes two enemies into each other by stepping on a tile already containing two enemies

Bugs

5a527753d05d3d3a18d40f80ce1a8647c11bf322_bug
See #5

Please sign in to comment.