Skip to content

Commit

Permalink
Enable rigid body for all actors, bump Unity version
Browse files Browse the repository at this point in the history
Enable rigid body for all actors, bump Unity version
  • Loading branch information
0x7c13 committed Apr 2, 2022
1 parent 5ca49c2 commit 2b0568f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
63 changes: 30 additions & 33 deletions Assets/Scripts/Pal3/Actor/ActorActionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class ActorActionController : MonoBehaviour,
private Rigidbody _rigidbody;
private CapsuleCollider _collider;

// By default, all none-player actors are kinematic.
// Also, the player actor is only non-kinematic during gameplay state.
private bool _isKinematic = true;

public void Init(GameResourceProvider resourceProvider, Actor actor, Color tintColor)
{
_resourceProvider = resourceProvider;
Expand Down Expand Up @@ -149,6 +153,7 @@ public void PerformAction(string actionName,

SetupShadow(action);
SetupCollider();
SetupRigidBody();
}

private void SetupShadow(ActorActionType actorAction)
Expand All @@ -174,26 +179,29 @@ private void SetupShadow(ActorActionType actorAction)

private void SetupCollider()
{
if (_collider == null)
{
_collider = gameObject.AddComponent<CapsuleCollider>();
}

var bounds = GetLocalBounds();
_collider = gameObject.GetOrAddComponent<CapsuleCollider>();
_collider.center = bounds.center;
_collider.height = bounds.size.y;
_collider.radius = bounds.size.x * 0.5f;

var currentGameState = ServiceLocator.Instance.Get<GameStateManager>().GetCurrentState();
ToggleCollisionDetectionBasedOnGameState(currentGameState);
}

private void SetupRigidBody()
{
_rigidbody = gameObject.GetOrAddComponent<Rigidbody>();
_rigidbody.useGravity = false;
_rigidbody.isKinematic = false;
_rigidbody.constraints = RigidbodyConstraints.FreezePositionY |
RigidbodyConstraints.FreezeRotation;
if (_rigidbody == null)
{
_rigidbody = gameObject.AddComponent<Rigidbody>();
_rigidbody.useGravity = false;
_rigidbody.constraints = RigidbodyConstraints.FreezePositionY |
RigidbodyConstraints.FreezeRotation;
}

var currentGameState = ServiceLocator.Instance.Get<GameStateManager>().GetCurrentState();
ToggleCollisionDetectionBasedOnGameState(currentGameState);
_rigidbody.isKinematic = currentGameState != GameState.Gameplay || _isKinematic;
}

private void RenderShadow()
Expand Down Expand Up @@ -354,36 +362,25 @@ public void Execute(ActorChangeTextureCommand command)
public void Execute(ActorEnablePlayerControlCommand command)
{
if (command.ActorId == ActorConstants.PlayerActorVirtualID) return;
var enableRigidBody = _actor.Info.Id == command.ActorId;
if (enableRigidBody && _rigidbody == null) SetupRigidBody();
if (!enableRigidBody && _rigidbody != null) Destroy(_rigidbody);
}

public void Execute(GameStateChangedNotification command)
{
ToggleCollisionDetectionBasedOnGameState(command.NewState);
}
_isKinematic = _actor.Info.Id != command.ActorId;

// TODO: Temporarily disable collision detection during cutscene since
// the current path finding solution is not ideal and might cause issues
public void ToggleCollisionDetectionBasedOnGameState(GameState state)
{
if (_collider != null)
if (_rigidbody != null)
{
_collider.enabled = state switch
{
GameState.Gameplay => true,
_ => false
};
_rigidbody.isKinematic = _isKinematic;
}
}

if (_rigidbody != null)
public void Execute(GameStateChangedNotification command)
{
if (command.NewState != GameState.Gameplay && _isKinematic == false)
{
_rigidbody.detectCollisions = state switch
_isKinematic = true;

if (_rigidbody != null)
{
GameState.Gameplay => true,
_ => false
};
_rigidbody.isKinematic = _isKinematic;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Pal3/Actor/ActorMovementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void FixedUpdate()
// Also we need to adjust Y position based on tile information
// during the collision since we are locking Y movement for the
// player actor's rigidbody.
if (_isDuringCollision && _actionController.GetRigidBody() is {} _)
if (_isDuringCollision && _actionController.GetRigidBody() is {isKinematic: false})
{
var currentPosition = transform.position;
var tilePosition = _tilemap.GetTilePosition(currentPosition, _currentLayerIndex);
Expand Down
4 changes: 3 additions & 1 deletion Assets/Scripts/Pal3/Player/PlayerGamePlayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,13 @@ private IEnumerator PlayerActorMoveToClimbableObjectAndClimb(

public void Execute(ActorEnablePlayerControlCommand command)
{
if (command.ActorId == ActorConstants.PlayerActorVirtualID) return;

_lastKnownPosition = Vector3.zero;
_lastKnownTilePosition = Vector2Int.zero;

_playerActor = _sceneManager.GetCurrentScene()
.GetActorGameObject((byte) _playerManager.GetPlayerActor());
.GetActorGameObject((byte) command.ActorId);
_playerActorActionController = _playerActor.GetComponent<ActorActionController>();
_playerActorMovementController = _playerActor.GetComponent<ActorMovementController>();
}
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2021.2.16f1
m_EditorVersionWithRevision: 2021.2.16f1 (559fc0ec6670)
m_EditorVersion: 2021.2.18f1
m_EditorVersionWithRevision: 2021.2.18f1 (0c6e675195cf)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</p>
<p align="center">
<a style="text-decoration:none">
<img src="https://img.shields.io/badge/unity-2021.2.16f1-blue?style=flat-square" alt="Unity Version" />
<img src="https://img.shields.io/badge/unity-2021.2.18f1-blue?style=flat-square" alt="Unity Version" />
</a>
<a style="text-decoration:none">
<img src="https://img.shields.io/badge/platform-Linux%20%7C%20Win%20%7C%20Mac%20%7C%20iOS%20%7C%20Android-orange?style=flat-square" alt="Platform" />
Expand Down Expand Up @@ -66,4 +66,4 @@ https://www.bilibili.com/video/BV1Fu411R7jM

## 技术交流以及测试
仙剑三复刻版讨论群:252315306
仙剑三复刻版开发群:330680869

0 comments on commit 2b0568f

Please sign in to comment.