Added AI code for enemies utilizing State Management Created simple enemy prefab with the AI
187 lines
4.9 KiB
C#
187 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.VisualScripting;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
[SelectionBase]
|
|
public class Player : MonoBehaviour {
|
|
[Header("Asset/Prefab")]
|
|
[SerializeField] public BuilderManager Builder;
|
|
|
|
[Header("Movement")]
|
|
[SerializeField] private Rigidbody2D RigidBody;
|
|
[SerializeField] private Animator Animator;
|
|
[SerializeField] private SpriteRenderer Renderer;
|
|
|
|
[Header("Character Class")]
|
|
[SerializeField] private ParticleSystem Aura;
|
|
[SerializeField] private GameObject[] ClassIndicators;
|
|
|
|
[Header("Movement Attributes")]
|
|
[SerializeField] private float MoveSpeed = 8;
|
|
[SerializeField] private float DashMultiplier = 60;
|
|
[SerializeField] private float DriftSpeed = 60;
|
|
[SerializeField] private float DriftFactorial = 0.85f;
|
|
|
|
[Header("VFX")]
|
|
[SerializeField] private GameObject VfxDash;
|
|
private VfxHandlerBase VfxDashHandler;
|
|
|
|
private Vector2 MoveDirection = Vector2.zero;
|
|
private Directions FaceDir = Directions.Down;
|
|
|
|
private bool isDashing;
|
|
private Vector2 DriftDirection = Vector2.zero;
|
|
private float Drift = 0;
|
|
|
|
private readonly int AnimMoveRight = Animator.StringToHash("Anim_Player_MoveRight");
|
|
private readonly int AnimIdleRight = Animator.StringToHash("Anim_Player_IdleRight");
|
|
|
|
|
|
private enum Directions { Left, Right, Up, Down }
|
|
|
|
void Awake() {
|
|
Builder = GetComponent<BuilderManager>();
|
|
VfxDashHandler = new VfxHandlerBase(VfxDash, 5, 5);
|
|
SetClass(1);
|
|
}
|
|
|
|
private void Update() {
|
|
GatherInput();
|
|
KeyPressActions();
|
|
}
|
|
private void GatherInput() {
|
|
MoveDirection.x = Input.GetAxisRaw("Horizontal");
|
|
MoveDirection.y = Input.GetAxisRaw("Vertical");
|
|
}
|
|
|
|
private void KeyPressActions() {
|
|
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)) {
|
|
Dash(MoveDirection);
|
|
} else if (Input.GetKeyDown(KeyCode.F1)) {
|
|
SetClass(0);
|
|
} else if (Input.GetKeyDown(KeyCode.F2)) {
|
|
SetClass(1);
|
|
} else if (Input.GetKeyDown(KeyCode.F3)) {
|
|
SetClass(2);
|
|
} else if (Input.GetKeyDown(KeyCode.F4)) {
|
|
SetClass(3);
|
|
}
|
|
}
|
|
|
|
private void Dash(Vector2 direction) {
|
|
if (Drift > 0.5f) return;
|
|
isDashing = true;
|
|
|
|
RigidBody.linearVelocity = direction.normalized * MoveSpeed * DashMultiplier;
|
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 180;
|
|
if (direction.normalized != Vector2.zero)
|
|
VfxDashHandler.PlayAll(this.transform.position, Quaternion.Euler(0f, 0f, angle));
|
|
DriftDirection = direction.normalized;
|
|
Drift = DriftSpeed;
|
|
isDashing = false;
|
|
}
|
|
|
|
private void SetClass(int classIdx) {
|
|
Builder.SetBuildMode(classIdx == 3);
|
|
|
|
foreach (var (indicator, i) in ClassIndicators.Select((obj, i) => (obj, i)))
|
|
if (i != classIdx) {
|
|
indicator.transform.localScale = Vector3.one;
|
|
indicator.transform.rotation = Quaternion.identity;
|
|
} else {
|
|
indicator.transform.localScale = Vector3.one * 2;
|
|
indicator.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
|
|
}
|
|
|
|
RawImage image = ClassIndicators[classIdx].GetComponent<RawImage>();
|
|
var main = Aura.main;
|
|
main.startColor = image.color;
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate() {
|
|
MovementUpdate();
|
|
CalculateFacingDirection();
|
|
UpdateAnimation();
|
|
}
|
|
|
|
private void MovementUpdate() {
|
|
if (isDashing) return;
|
|
|
|
RigidBody.linearVelocity = MoveDirection.normalized * MoveSpeed;
|
|
if (Drift > 0.2f) {
|
|
RigidBody.linearVelocity += DriftDirection * Drift;
|
|
Drift *= DriftFactorial;
|
|
} else {
|
|
Drift = 0f;
|
|
}
|
|
}
|
|
|
|
|
|
private void CalculateFacingDirection() {
|
|
if (MoveDirection.x != 0)
|
|
if (MoveDirection.x < 0)
|
|
FaceDir = Directions.Left;
|
|
else
|
|
FaceDir = Directions.Right;
|
|
}
|
|
|
|
private void UpdateAnimation() {
|
|
Renderer.flipX = (FaceDir == Directions.Left);
|
|
Animator.CrossFade((MoveDirection.sqrMagnitude > 0) ? AnimMoveRight : AnimIdleRight, 0);
|
|
}
|
|
|
|
|
|
|
|
void OnDrawGizmos() {
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<CharacterClass> classes; // assign in Inspector
|
|
|
|
[HideInInspector] public int maxHealth;
|
|
[HideInInspector] public int maxMana;
|
|
[HideInInspector] public float attackPower;
|
|
[HideInInspector] public List<Ability> abilities;
|
|
|
|
//void Awake() {
|
|
// //InitializeCharacter();
|
|
//}
|
|
|
|
void InitializeCharacter() {
|
|
maxHealth = 0;
|
|
maxMana = 0;
|
|
attackPower = 1f;
|
|
abilities = new List<Ability>();
|
|
|
|
foreach (var cc in classes) {
|
|
maxHealth += cc.baseHealth;
|
|
maxMana += cc.baseMana;
|
|
attackPower *= cc.attackMultiplier;
|
|
abilities.AddRange(cc.abilities);
|
|
}
|
|
|
|
Debug.Log($"{name} initialized as {string.Join(", ", classes.ConvertAll(c => c.className))} with {abilities.Count} abilities.");
|
|
}
|
|
|
|
public void UseAbility(int index) {
|
|
if (index < 0 || index >= abilities.Count) return;
|
|
var ability = abilities[index];
|
|
ability.Use(this);
|
|
}
|
|
}
|