Add enemy animation

This commit is contained in:
Jens Pitkänen 2019-08-04 21:09:00 +03:00
parent 6cf9a21b2a
commit 27937e0a24
5 changed files with 61 additions and 22 deletions

View File

@ -9,11 +9,11 @@ GameObject:
serializedVersion: 6 serializedVersion: 6
m_Component: m_Component:
- component: {fileID: 592487110401094196} - component: {fileID: 592487110401094196}
- component: {fileID: 592487110401094200}
- component: {fileID: 592487110401094195} - component: {fileID: 592487110401094195}
- component: {fileID: 592487110401094194} - component: {fileID: 592487110401094194}
- component: {fileID: 592487110401094193} - component: {fileID: 592487110401094193}
- component: {fileID: 3362493323285318061} - component: {fileID: 3362493323285318061}
- component: {fileID: 592487110401094200}
- component: {fileID: 592487110401094199} - component: {fileID: 592487110401094199}
- component: {fileID: 592487110401094198} - component: {fileID: 592487110401094198}
- component: {fileID: 592487110401094204} - component: {fileID: 592487110401094204}
@ -44,6 +44,31 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &592487110401094200
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 592487110401094192}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 978bec54408eb8f42a36fd68d7447f5a, type: 3}
m_Name:
m_EditorClassIdentifier:
CurrentBehavior: Nothing
Renderer: {fileID: 592487111066334295}
IdleSprite: {fileID: 6754164815184778395, guid: 8d816068e3260f54f9cb041dcd165103,
type: 3}
AttackingSprite: {fileID: 3837973818445772063, guid: 8d816068e3260f54f9cb041dcd165103,
type: 3}
MovementAnimationFramerate: 8
MovementSprites:
- {fileID: 6754164815184778395, guid: 8d816068e3260f54f9cb041dcd165103, type: 3}
- {fileID: -8236523633242920872, guid: 8d816068e3260f54f9cb041dcd165103, type: 3}
MoveSpeed: 1
BehaviourTree: {fileID: 592487110401094199}
Attacking: 0
--- !u!50 &592487110401094195 --- !u!50 &592487110401094195
Rigidbody2D: Rigidbody2D:
serializedVersion: 4 serializedVersion: 4
@ -122,21 +147,6 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
MaxHealth: 30 MaxHealth: 30
CurrentHealth: 30 CurrentHealth: 30
--- !u!114 &592487110401094200
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 592487110401094192}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 978bec54408eb8f42a36fd68d7447f5a, type: 3}
m_Name:
m_EditorClassIdentifier:
MoveSpeed: 1
BehaviourTree: {fileID: 592487110401094199}
CurrentBehavior: Nothing
--- !u!114 &592487110401094199 --- !u!114 &592487110401094199
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -40,12 +40,19 @@ namespace Saltosion.OneWeapon.AI.Behaviours {
} }
public override bool CanBehave(Enemy subject) { public override bool CanBehave(Enemy subject) {
return Follow.Target != null && (Follow.Target.position - subject.transform.position).magnitude <= MeleeRange; bool CanBehave = Follow.Target != null && (Follow.Target.position - subject.transform.position).magnitude <= MeleeRange;
if (!CanBehave) {
subject.Attacking = false;
}
return CanBehave;
} }
public override bool Execute(Enemy subject) { public override bool Execute(Enemy subject) {
if (CooldownProgress > 0) { if (CooldownProgress > 0) {
subject.Attacking = false;
return false; return false;
} else {
subject.Attacking = true;
} }
Vector2 Root = AnimatableTransform.parent.position + LocalOrigin; Vector2 Root = AnimatableTransform.parent.position + LocalOrigin;

View File

@ -4,10 +4,21 @@ using Saltosion.OneWeapon.AI;
namespace Saltosion.OneWeapon { namespace Saltosion.OneWeapon {
[RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Rigidbody2D))]
public class Enemy : MonoBehaviour { public class Enemy : MonoBehaviour {
public float MoveSpeed; [Header("Debug Info")]
public BehaviourNode BehaviourTree;
[Space(20)]
public string CurrentBehavior = "Nothing"; public string CurrentBehavior = "Nothing";
[Header("Graphics")]
public SpriteRenderer Renderer;
public Sprite IdleSprite;
public Sprite AttackingSprite;
public int MovementAnimationFramerate;
public Sprite[] MovementSprites;
[Header("Stats")]
public float MoveSpeed;
[Header("Behaviour")]
public BehaviourNode BehaviourTree;
[HideInInspector]
public bool Attacking;
private Rigidbody2D Body; private Rigidbody2D Body;
private bool MovingToTarget = false; private bool MovingToTarget = false;
@ -20,6 +31,14 @@ namespace Saltosion.OneWeapon {
private void Update() { private void Update() {
BehaviourTree.Execute(this); BehaviourTree.Execute(this);
CurrentBehavior = BehaviourTree.GetExecutedName(); CurrentBehavior = BehaviourTree.GetExecutedName();
if (Attacking) {
Renderer.sprite = AttackingSprite;
} else if (Body.velocity.magnitude > 0.1) {
Renderer.sprite = MovementSprites[(int)(Time.time * MovementAnimationFramerate) % MovementSprites.Length];
} else {
Renderer.sprite = IdleSprite;
}
} }
private void FixedUpdate() { private void FixedUpdate() {

View File

@ -32,7 +32,10 @@ namespace Saltosion.OneWeapon {
} }
public void Explode(bool destroyGameObject) { public void Explode(bool destroyGameObject) {
foreach (GameObject Obj in BodypartPrefabs) { int Count = Mathf.Clamp((int)(BodypartPrefabs.Length * Random.value + 1), 1, BodypartPrefabs.Length);
for (int i = 0; i < Count; i++) {
GameObject Obj = BodypartPrefabs[i];
// No body parts flying in censored mode // No body parts flying in censored mode
float DirectionRadians = Random.value * Mathf.PI * 2.0f; float DirectionRadians = Random.value * Mathf.PI * 2.0f;
Vector2 Direction = new Vector2(Mathf.Cos(DirectionRadians), Mathf.Sin(DirectionRadians)); Vector2 Direction = new Vector2(Mathf.Cos(DirectionRadians), Mathf.Sin(DirectionRadians));

View File

@ -18,7 +18,7 @@ namespace Saltosion.OneWeapon {
} }
public void Damage(float amount, Vector2 fromDirection) { public void Damage(float amount, Vector2 fromDirection) {
BloodLauncher.Splatter(transform.position, -fromDirection, (int)(10 * amount), 100f, 100); BloodLauncher.Splatter(transform.position, -fromDirection, (int)(amount), 100f, 100);
MaxHealth -= amount * (1 + PlayerFun.CurrentDamageBoost); MaxHealth -= amount * (1 + PlayerFun.CurrentDamageBoost);
if (MaxHealth <= 0) { if (MaxHealth <= 0) {
MaxHealth = 0; MaxHealth = 0;