Skip to content

Instantly share code, notes, and snippets.

@jesterswilde
Created December 18, 2018 19:57
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 jesterswilde/9c173ac19a528b557591ba0215f3b1f8 to your computer and use it in GitHub Desktop.
Save jesterswilde/9c173ac19a528b557591ba0215f3b1f8 to your computer and use it in GitHub Desktop.
public class Player : MonoBehaviour, ITimed
{
[SerializeField]
float speed = 1;
[SerializeField]
bool isStartingPlayer = false;
public bool IsStartingPlayer {get{return isStartingPlayer;}}
[SerializeField]
CollDetector forwardDetector;
bool movingForward = false;
public bool MovingForward { get { return movingForward; } }
bool movingBackward = false;
public bool MovingBackward { get { return movingBackward; } }
bool movingLeft = false;
public bool MovingLeft { get { return movingLeft; } }
bool movingRight = false;
public bool MovingRight { get { return movingRight; } }
[SerializeField]
int futuristFrame = 0;
public int FuturistFrame { get { return futuristFrame; } }
ItemDetector itemDetector;
int forwardMove
{
get
{
int move = 0;
move += movingForward ? 1 : 0;
move -= movingBackward ? 1 : 0;
return move;
}
}
int sidewaysMove
{
get
{
int move = 0;
move += movingRight ? 1 : 0;
move -= movingLeft ? 1 : 0;
return move;
}
}
void Awake()
{
itemDetector = GetComponentInChildren<ItemDetector>();
}
void Start()
{
GameManager.RegisterTimedThing(this);
if (isStartingPlayer)
{
GameManager.SelectPlayer(this);
}
GameManager.AddSerielizedState(Serialize());
}
public void Play(float deltaTime)
{
if (sidewaysMove != 0 || forwardMove != 0)
{
transform.up = new Vector3(sidewaysMove, forwardMove, 0);
}
if (forwardDetector.IsFree)
{
transform.position += new Vector3(sidewaysMove, forwardMove, 0) * speed * deltaTime;
}
}
public void ProcessAction(Action action)
{
switch (action.Type)
{
case ActionEnum.PressForward:
movingForward = true;
break;
case ActionEnum.PressRight:
movingRight = true;
break;
case ActionEnum.PressBack:
movingBackward = true;
break;
case ActionEnum.PressLeft:
movingLeft = true;
break;
case ActionEnum.ReleaseForward:
movingForward = false;
break;
case ActionEnum.ReleaseRight:
movingRight = false;
break;
case ActionEnum.ReleaseBack:
movingBackward = false;
break;
case ActionEnum.ReleaseLeft:
movingLeft = false;
break;
case ActionEnum.Activate:
itemDetector.SelectedItem?.Activate();
break;
default:
break;
}
}
public void Hydrate(SeriealizedState state)
{
PlayerState pState = (PlayerState)state;
transform.position = pState.Position;
transform.rotation = pState.Rotation;
movingForward = pState.Forward;
movingRight = pState.Right;
movingBackward = pState.Back;
movingLeft = pState.Left;
}
public TimedState Serialize()
{
PlayerState state = new PlayerState(this);
return new TimedState(this, state);
}
public void Select()
{
}
public void Deselect()
{
futuristFrame = GameManager.CurrentFrame;
}
}
public class PlayerState : SeriealizedState
{
Vector3 position;
public Vector3 Position { get { return position; } }
Quaternion rotation;
public Quaternion Rotation { get { return rotation; } }
bool forward;
public bool Forward { get { return forward; } }
bool right;
public bool Right { get { return right; } }
bool back;
public bool Back { get { return back; } }
bool left;
public bool Left { get { return left; } }
public PlayerState(Player player)
{
position = player.transform.position;
rotation = player.transform.rotation;
forward = player.MovingForward;
right = player.MovingRight;
back = player.MovingBackward;
left = player.MovingLeft;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment