Created
November 6, 2014 14:55
-
-
Save kevinruscoe/c7eb5915bda38bc2fb43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Mario : MonoBehaviour { | |
public MarioState _state; | |
public tk2dSpriteAnimator animator; | |
public tk2dSprite sprite; | |
void Start () { | |
animator = gameObject.GetComponent<tk2dSpriteAnimator>(); | |
sprite = gameObject.GetComponent<tk2dSprite>(); | |
_state = new IdleState(); | |
} | |
void Update () { | |
_state = _state.Update( this ); | |
Debug.Log(_state); | |
} | |
void OnCollisionEnter2D( Collision2D other ){ | |
if (other.gameObject.layer == LayerMask.NameToLayer("Ground")) { | |
_state.SendMessage( StateMessages.message.HIT_FLOOR ); | |
} | |
} | |
} | |
public interface MarioState { | |
MarioState Update( Mario mario ); | |
void SendMessage( StateMessages.message message ); | |
} | |
public class IdleState: MarioState { | |
public MarioState Update( Mario mario ){ | |
mario.animator.Play("SM_Idle"); | |
Vector2 _vel = mario.rigidbody2D.velocity; | |
_vel.x = 0; | |
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) ) | |
{ | |
return new WalkState(); | |
} | |
if( Input.GetKey(KeyCode.W) ){ | |
return new JumpState(); | |
} | |
if( mario.transform.position.y < 0 ){ | |
return new DieState(); | |
} | |
mario.rigidbody2D.velocity = _vel; | |
return this; | |
} | |
public void SendMessage( StateMessages.message message ){ | |
return; | |
} | |
} | |
public class WalkState: MarioState { | |
public MarioState Update( Mario mario ){ | |
Vector2 _vel = mario.rigidbody2D.velocity; | |
mario.animator.Play("SM_Walk"); | |
if( Input.GetKey(KeyCode.A) ){ | |
mario.sprite.FlipX = false; | |
_vel.x = -1f; | |
} | |
if( Input.GetKey(KeyCode.D) ){ | |
mario.sprite.FlipX = true; | |
_vel.x = 1f; | |
} | |
if( Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D) ){ | |
_vel.x = 0; | |
} | |
if( Input.GetKey(KeyCode.W) ){ | |
return new JumpState(); | |
} | |
if (!Input.GetKey (KeyCode.A) && !Input.GetKey(KeyCode.D) ) | |
{ | |
return new IdleState(); | |
} | |
if( mario.transform.position.y < 0 ){ | |
return new DieState(); | |
} | |
mario.rigidbody2D.velocity = _vel; | |
return this; | |
} | |
public void SendMessage( StateMessages.message message ){ | |
return; | |
} | |
} | |
public class JumpState: MarioState { | |
public MarioState Update( Mario mario ){ | |
Vector2 _vel = mario.rigidbody2D.velocity; | |
mario.animator.Play("SM_Jump"); | |
_vel.y = 3f; | |
mario.rigidbody2D.velocity = _vel; | |
return new FallingState(); | |
} | |
public void SendMessage( StateMessages.message message ){ | |
return; | |
} | |
} | |
public class FallingState: MarioState { | |
bool hasHitFloor = false; | |
public MarioState Update( Mario mario ){ | |
if( mario.transform.position.y < 0 ){ | |
return new DieState(); | |
} | |
if ( hasHitFloor ) { | |
return new IdleState(); | |
} | |
return this; | |
} | |
public void SendMessage( StateMessages.message message ){ | |
if( message == StateMessages.message.HIT_FLOOR ){ | |
hasHitFloor = true; | |
} | |
return; | |
} | |
} | |
public class DieState: MarioState { | |
private bool hasDied = false; | |
public MarioState Update( Mario mario ){ | |
if( !hasDied ){ | |
Vector2 _vel = mario.rigidbody2D.velocity; | |
mario.collider2D.isTrigger = true; | |
mario.animator.Play("SM_Die"); | |
_vel.y = 3f; | |
_vel.x = 1.5f; | |
mario.rigidbody2D.velocity = _vel; | |
hasDied = true; | |
} | |
return this; | |
} | |
public void SendMessage( StateMessages.message message ){} | |
} | |
public class StateMessages { | |
public enum message { | |
HIT_FLOOR | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment