몬스터 에셋 추가
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using Assets.PixelFantasy.Common.Scripts;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.PixelFantasy.PixelMonsters.Common.Scripts.ExampleScripts
|
||||
{
|
||||
[RequireComponent(typeof(Monster))]
|
||||
public class MonsterAnimation : MonoBehaviour
|
||||
{
|
||||
private Monster _monster;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_monster = GetComponent<Monster>();
|
||||
}
|
||||
|
||||
public void SetState(MonsterState state)
|
||||
{
|
||||
foreach (var variable in new[] { "Idle", "Ready", "Walk", "Run", "Jump", "Die" })
|
||||
{
|
||||
_monster.Animator.SetBool(variable, false);
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case MonsterState.Idle: _monster.Animator.SetBool("Idle", true); break;
|
||||
case MonsterState.Ready: _monster.Animator.SetBool("Ready", true); break;
|
||||
case MonsterState.Walk: _monster.Animator.SetBool("Walk", true); break;
|
||||
case MonsterState.Run: _monster.Animator.SetBool("Run", true); break;
|
||||
case MonsterState.Jump: _monster.Animator.SetBool("Jump", true); break;
|
||||
case MonsterState.Die: _monster.Animator.SetBool("Die", true); break;
|
||||
default: throw new NotSupportedException();
|
||||
}
|
||||
|
||||
//Debug.Log("SetState: " + state);
|
||||
}
|
||||
|
||||
public MonsterState GetState()
|
||||
{
|
||||
if (_monster.Animator.GetBool("Idle")) return MonsterState.Idle;
|
||||
if (_monster.Animator.GetBool("Ready")) return MonsterState.Ready;
|
||||
if (_monster.Animator.GetBool("Walk")) return MonsterState.Walk;
|
||||
if (_monster.Animator.GetBool("Run")) return MonsterState.Run;
|
||||
if (_monster.Animator.GetBool("Jump")) return MonsterState.Jump;
|
||||
if (_monster.Animator.GetBool("Die")) return MonsterState.Die;
|
||||
|
||||
return MonsterState.Ready;
|
||||
}
|
||||
|
||||
public void Idle()
|
||||
{
|
||||
SetState(MonsterState.Idle);
|
||||
}
|
||||
|
||||
public void Ready()
|
||||
{
|
||||
if (GetState() == MonsterState.Walk)
|
||||
{
|
||||
EffectManager.Instance.CreateSpriteEffect(_monster, "Brake");
|
||||
}
|
||||
else if (GetState() == MonsterState.Idle)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SetState(MonsterState.Ready);
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
if (GetState() != MonsterState.Walk)
|
||||
{
|
||||
EffectManager.Instance.CreateSpriteEffect(_monster, "Run");
|
||||
}
|
||||
|
||||
SetState(MonsterState.Walk);
|
||||
}
|
||||
|
||||
public void Jump()
|
||||
{
|
||||
EffectManager.Instance.CreateSpriteEffect(_monster, "Jump");
|
||||
SetState(MonsterState.Run);
|
||||
}
|
||||
|
||||
public void Fall()
|
||||
{
|
||||
SetState(MonsterState.Run);
|
||||
}
|
||||
|
||||
public void Land()
|
||||
{
|
||||
EffectManager.Instance.CreateSpriteEffect(_monster, "Fall");
|
||||
}
|
||||
|
||||
public void Die()
|
||||
{
|
||||
SetState(MonsterState.Die);
|
||||
}
|
||||
|
||||
public void Attack()
|
||||
{
|
||||
_monster.Animator.SetTrigger("Attack");
|
||||
}
|
||||
|
||||
public void Fire()
|
||||
{
|
||||
_monster.Animator.SetTrigger("Fire");
|
||||
}
|
||||
|
||||
public void Hit()
|
||||
{
|
||||
_monster.Animator.SetTrigger("Hit");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e5bf41b75130d841bf3666dc0e7fa00
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 269333
|
||||
packageName: Pixel Monsters Animated Boss Pack #1
|
||||
packageVersion: 2.7
|
||||
assetPath: Assets/PixelFantasy/PixelMonsters/Common/Scripts/ExampleScripts/MonsterAnimation.cs
|
||||
uploadId: 656019
|
||||
@@ -0,0 +1,130 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.PixelFantasy.PixelMonsters.Common.Scripts.ExampleScripts
|
||||
{
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(MonsterAnimation))]
|
||||
public class MonsterController2D : MonoBehaviour
|
||||
{
|
||||
public Vector2 Input;
|
||||
public bool IsGrounded;
|
||||
|
||||
public float Acceleration = 40;
|
||||
public float MaxSpeed = 8;
|
||||
public float JumpForce = 1000;
|
||||
public float Gravity = 70;
|
||||
|
||||
private Collider2D _collider;
|
||||
private Rigidbody2D _rigidbody;
|
||||
private MonsterAnimation _animation;
|
||||
|
||||
private bool _jump;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_collider = GetComponent<Collider2D>();
|
||||
_rigidbody = GetComponent<Rigidbody2D>();
|
||||
_animation = GetComponent<MonsterAnimation>();
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
var state = _animation.GetState();
|
||||
|
||||
if (state == MonsterState.Die) return;
|
||||
|
||||
var velocity = _rigidbody.velocity;
|
||||
|
||||
if (Input.x == 0)
|
||||
{
|
||||
if (IsGrounded)
|
||||
{
|
||||
velocity.x = Mathf.MoveTowards(velocity.x, 0, Acceleration * 3 * Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var maxSpeed = MaxSpeed;
|
||||
var acceleration = Acceleration;
|
||||
|
||||
if (_jump)
|
||||
{
|
||||
acceleration /= 2;
|
||||
}
|
||||
|
||||
velocity.x = Mathf.MoveTowards(velocity.x, Input.x * maxSpeed, acceleration * Time.fixedDeltaTime);
|
||||
Turn(velocity.x);
|
||||
}
|
||||
|
||||
if (IsGrounded)
|
||||
{
|
||||
if (!_jump)
|
||||
{
|
||||
if (Input.x == 0)
|
||||
{
|
||||
_animation.Ready();
|
||||
}
|
||||
else
|
||||
{
|
||||
_animation.Run();
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.y > 0 && !_jump)
|
||||
{
|
||||
_jump = true;
|
||||
_rigidbody.AddForce(Vector2.up * JumpForce);
|
||||
_animation.Jump();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity.y -= Gravity * Time.fixedDeltaTime;
|
||||
|
||||
if (velocity.y < 0)
|
||||
{
|
||||
_jump = true;
|
||||
_animation.Fall();
|
||||
}
|
||||
}
|
||||
|
||||
_rigidbody.velocity = velocity;
|
||||
}
|
||||
|
||||
private void Turn(float direction)
|
||||
{
|
||||
var scale = transform.localScale;
|
||||
|
||||
scale.x = Mathf.Sign(direction) * Mathf.Abs(scale.x);
|
||||
|
||||
transform.localScale = scale;
|
||||
}
|
||||
|
||||
private Collider2D _ground;
|
||||
|
||||
public void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (collision.contacts.All(i => i.point.y <= _collider.bounds.min.y + 0.1f))
|
||||
{
|
||||
IsGrounded = true;
|
||||
_ground = collision.collider;
|
||||
|
||||
if (_jump)
|
||||
{
|
||||
_jump = false;
|
||||
_animation.Land();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCollisionExit2D(Collision2D collision)
|
||||
{
|
||||
if (IsGrounded && collision.collider == _ground)
|
||||
{
|
||||
IsGrounded = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef32fbd63a65f054b90112e91c4a0f69
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 269333
|
||||
packageName: Pixel Monsters Animated Boss Pack #1
|
||||
packageVersion: 2.7
|
||||
assetPath: Assets/PixelFantasy/PixelMonsters/Common/Scripts/ExampleScripts/MonsterController2D.cs
|
||||
uploadId: 656019
|
||||
@@ -0,0 +1,64 @@
|
||||
using Assets.PixelFantasy.Common.Scripts;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.PixelFantasy.PixelMonsters.Common.Scripts.ExampleScripts
|
||||
{
|
||||
[RequireComponent(typeof(Monster))]
|
||||
[RequireComponent(typeof(MonsterController2D))]
|
||||
[RequireComponent(typeof(MonsterAnimation))]
|
||||
public class MonsterControls : MonoBehaviour
|
||||
{
|
||||
private Monster _monster;
|
||||
private MonsterController2D _controller;
|
||||
private MonsterAnimation _animation;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_monster = GetComponent<Monster>();
|
||||
_controller = GetComponent<MonsterController2D>();
|
||||
_animation = GetComponent<MonsterAnimation>();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Move();
|
||||
Attack();
|
||||
|
||||
// Play other animations, just for example.
|
||||
if (Input.GetKeyDown(KeyCode.I)) { _animation.SetState(MonsterState.Idle); }
|
||||
if (Input.GetKeyDown(KeyCode.R)) { _animation.SetState(MonsterState.Ready); }
|
||||
if (Input.GetKeyDown(KeyCode.D)) _animation.SetState(MonsterState.Die);
|
||||
if (Input.GetKeyUp(KeyCode.H)) _animation.Hit();
|
||||
if (Input.GetKeyUp(KeyCode.L)) EffectManager.Instance.Blink(_monster);
|
||||
}
|
||||
|
||||
private void Move()
|
||||
{
|
||||
_controller.Input = Vector2.zero;
|
||||
|
||||
if (Input.GetKey(KeyCode.LeftArrow))
|
||||
{
|
||||
_controller.Input.x = -1;
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.RightArrow))
|
||||
{
|
||||
_controller.Input.x = 1;
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.UpArrow))
|
||||
{
|
||||
_controller.Input.y = 1;
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.DownArrow))
|
||||
{
|
||||
_controller.Input.y = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void Attack()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.A)) _animation.Attack();
|
||||
if (Input.GetKeyDown(KeyCode.F)) _animation.Fire();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec7989c5e9d15f849b9baed57d5feb65
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 269333
|
||||
packageName: Pixel Monsters Animated Boss Pack #1
|
||||
packageVersion: 2.7
|
||||
assetPath: Assets/PixelFantasy/PixelMonsters/Common/Scripts/ExampleScripts/MonsterControls.cs
|
||||
uploadId: 656019
|
||||
Reference in New Issue
Block a user