Skip to content

Commit

Permalink
fix: enemy will not teleport to new enemy on possess
Browse files Browse the repository at this point in the history
  • Loading branch information
LTYGUY authored and winstxnhdw committed Feb 13, 2024
1 parent 8163e12 commit 97b314d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
6 changes: 6 additions & 0 deletions lc-hax/Scripts/Components/CharacterMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ internal class CharacterMovement : MonoBehaviour {
bool IsSprintHeld { get; set; } = false;
float SprintTimer { get; set; } = 0.0f;
Keyboard Keyboard { get; set; } = Keyboard.current;
KeyboardMovement? NoClipKeyboard { get; set; } = null;

internal void SetNoClipMode(bool enabled) => this.NoClipKeyboard!.enabled = enabled;

// Initialize method
internal void Init() {
Expand All @@ -31,6 +34,7 @@ internal void Init() {

void Awake() {
this.Keyboard = Keyboard.current;
this.NoClipKeyboard = this.gameObject.AddComponent<KeyboardMovement>();
this.CharacterController = this.gameObject.AddComponent<CharacterController>();
this.CharacterController.height = 0.0f; // Adjust as needed
this.CharacterController.center = new Vector3(0.0f, 0.3f, 0.5f); // Adjust as needed
Expand All @@ -39,6 +43,8 @@ void Awake() {

// Update is called once per frame
void Update() {
if (this.NoClipKeyboard!.enabled) return;

// Read movement input from keyboard
Vector2 moveInput = new Vector2(
this.Keyboard.dKey.ReadValue() - this.Keyboard.aKey.ReadValue(),
Expand Down
49 changes: 33 additions & 16 deletions lc-hax/Scripts/Modules/Possession/PossessionMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal sealed class PossessionMod : MonoBehaviour {

Possession Possession { get; } = new();
Coroutine? UpdateCoroutine { get; set; } = null;
GameObject? CharacterMovementInstance { get; set; } = null;
CharacterMovement? CharacterMovement { get; set; } = null;
KeyboardMovement? Keyboard { get; set; } = null;
MousePan? MousePan { get; set; } = null;

bool FirstUpdate { get; set; } = true;
Expand All @@ -34,14 +34,27 @@ internal sealed class PossessionMod : MonoBehaviour {
};

void Awake() {
this.CharacterMovement = this.gameObject.AddComponent<CharacterMovement>();
this.Keyboard = this.gameObject.AddComponent<KeyboardMovement>();
this.InitCharacterMovement();
this.MousePan = this.gameObject.AddComponent<MousePan>();
this.enabled = false;

PossessionMod.Instance = this;
}

void InitCharacterMovement(Vector3 initPos = default) {
this.CharacterMovementInstance = new GameObject("Hax_CharacterMovement");
this.CharacterMovementInstance.transform.position = initPos;
this.CharacterMovement = this.CharacterMovementInstance.AddComponent<CharacterMovement>();
this.CharacterMovement.Init();
DontDestroyOnLoad(this.CharacterMovementInstance);
}

void UnInitCharacterMovement() {
if (this.CharacterMovementInstance is not GameObject characterMovementInstance) return;

Destroy(characterMovementInstance);
}

void OnEnable() {
InputListener.OnNPress += this.ToggleNoClip;
InputListener.OnZPress += this.Unpossess;
Expand Down Expand Up @@ -81,12 +94,11 @@ void ToggleNoClip() {

void UpdateComponentsOnCurrentState(bool thisGameObjectIsEnabled) {
if (this.MousePan is not MousePan mousePan) return;
if (this.CharacterMovement is not CharacterMovement rigidbodyKeyboard) return;
if (this.Keyboard is not KeyboardMovement keyboard) return;
if (this.CharacterMovement is not CharacterMovement characterMovement) return;

mousePan.enabled = thisGameObjectIsEnabled;
rigidbodyKeyboard.enabled = !this.NoClipEnabled;
keyboard.enabled = this.NoClipEnabled;
characterMovement.gameObject.SetActive(thisGameObjectIsEnabled);
characterMovement.SetNoClipMode(this.NoClipEnabled);
}

IEnumerator EndOfFrameCoroutine() {
Expand Down Expand Up @@ -120,19 +132,18 @@ void EndOfFrameUpdate() {
agent.updateRotation = false;
}

characterMovement.Init();
this.transform.position = enemy.transform.position;
this.InitCharacterMovement(enemy.transform.position);
this.UpdateComponentsOnCurrentState(true);
}

if (!this.EnemyControllers.TryGetValue(enemy.GetType(), out IController controller)) {
this.UpdateEnemyPositionToHere(enemy);
this.UpdateEnemyPosition(enemy);
this.UpdateCameraPosition(camera, enemy);
return;
}

else if (controller.IsAbleToMove(enemy)) {
this.UpdateEnemyPositionToHere(enemy);
this.UpdateEnemyPosition(enemy);
this.HandleEnemyMovements(controller, enemy, characterMovement.IsMoving, characterMovement.IsSprinting);
this.EnemyUpdate(controller, enemy);
}
Expand All @@ -142,17 +153,22 @@ void EndOfFrameUpdate() {
}

void UpdateCameraPosition(Camera camera, EnemyAI enemy) {
camera.transform.position = this.transform.position + (3.0f * (Vector3.up - enemy.transform.forward));
if (this.CharacterMovement is not CharacterMovement characterMovement) return;

camera.transform.position = characterMovement.transform.position + (3.0f * (Vector3.up - enemy.transform.forward));
camera.transform.rotation = this.transform.rotation;
characterMovement.transform.rotation = this.transform.rotation;
}

// Updates enemy's position to match the possessed object's position
void UpdateEnemyPositionToHere(EnemyAI enemy) {
void UpdateEnemyPosition(EnemyAI enemy) {
if (this.CharacterMovement is not CharacterMovement characterMovement) return;

enemy.updatePositionThreshold = 0;
Vector3 enemyEuler = enemy.transform.eulerAngles;
enemyEuler.y = this.transform.eulerAngles.y;
enemy.transform.eulerAngles = enemyEuler;
enemy.transform.position = this.transform.position;
enemy.transform.position = characterMovement.transform.position;
}

// Disables/enables colliders of the possessed enemy
Expand All @@ -163,18 +179,19 @@ void SetEnemyColliders(EnemyAI enemy, bool enabled) =>
internal void Possess(EnemyAI enemy) {
this.Unpossess();

this.Possession.SetEnemy(enemy);
this.FirstUpdate = true;
this.Possession.SetEnemy(enemy);
}

// Releases possession of the current enemy
internal void Unpossess() {
this.UnInitCharacterMovement();
if (this.Possession.Enemy is not EnemyAI enemy) return;
if (enemy.agent.Unfake() is NavMeshAgent navMeshAgent) {
navMeshAgent.updatePosition = true;
navMeshAgent.updateRotation = true;

this.UpdateEnemyPositionToHere(enemy);
this.UpdateEnemyPosition(enemy);
_ = enemy.agent.Warp(enemy.transform.position);
}

Expand Down

0 comments on commit 97b314d

Please sign in to comment.