Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShirakawaYoshimaru/9567c38b85033ab42b0d1f7246968457 to your computer and use it in GitHub Desktop.
Save ShirakawaYoshimaru/9567c38b85033ab42b0d1f7246968457 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using System;
public abstract class PlayerBase : MonoBehaviour
{
protected StateBase nowState;
public abstract void ModifyState (StateBase state);
public abstract void Move (Vector3 nextPosition);
public abstract Vector3 GetPosition ();
}
using UnityEngine;
using UnityEngine.UI;
using System;
public class Player : PlayerBase
{
public Player ()
{
ModifyState (new WanderState (this));
}
void Update ()
{
nowState.Update ();
}
public override void ModifyState (StateBase state)
{
if (nowState != null) {
nowState.Delete ();
}
this.nowState = state;
}
void OnTriggerEnter (Collider other)
{
nowState.FindOther (other);
}
void OnTriggerExit (Collider other)
{
nowState.LostOther (other);
}
public override void Move (Vector3 nextPosition)
{
transform.position = nextPosition;
}
public override Vector3 GetPosition ()
{
return transform.position;
}
}
using UnityEngine;
using UnityEngine.UI;
using System;
//Stateのintereface
public abstract class StateBase
{
protected PlayerBase player;
public StateBase (PlayerBase player)
{
this.player = player;
}
public abstract void Update ();
public abstract void FindOther (Collider other);
public abstract void LostOther (Collider other);
public abstract void Delete ();
}
public class FollowState : StateBase
{
Transform target;
public FollowState (PlayerBase player, Transform target) : base (player)
{
this.target = target;
}
public override void Update ()
{
Debug.Log ("追いかけてるよ!");
var position = this.player.GetPosition ();
position = Vector3.Lerp (position, target.position, 0.01f);
this.player.Move (position);
}
public override void FindOther (Collider other)
{
target = other.gameObject.transform;
}
public override void LostOther (Collider other)
{
//見失ったらさまよう
this.player.ModifyState (new WanderState (this.player));
}
public override void Delete ()
{
Debug.Log ("追跡を終了する");
}
}
public class WanderState : StateBase
{
public WanderState (PlayerBase player) : base (player)
{
}
public override void Update ()
{
Debug.Log ("さまよっているよ!");
var position = this.player.GetPosition ();
position.x += 0.1f;
this.player.Move (position);
}
public override void FindOther (Collider other)
{
//誰かを発見したら追いかける
this.player.ModifyState (new FollowState (this.player, other.gameObject.transform));
}
public override void LostOther (Collider other)
{
}
public override void Delete ()
{
Debug.Log ("さまようのを終了する");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment