1
Fork 0
slime-blood-and-pain/Assets/Scripts/EntityPlayer.cs

135 lines
4.1 KiB
C#
Raw Permalink Normal View History

2019-04-27 12:40:09 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-04-29 17:53:45 +00:00
using UnityEngine.SceneManagement;
2019-04-27 12:40:09 +00:00
2019-04-28 14:28:31 +00:00
public enum ControlMode {
Move,
Attack
}
2019-04-28 14:34:50 +00:00
public class EntityPlayer : Entity
2019-04-27 12:40:09 +00:00
{
2019-04-28 14:28:31 +00:00
protected ControlMode controlMode;
2019-04-28 16:12:27 +00:00
protected Animator animator;
2019-04-28 14:28:31 +00:00
protected override void Start() {
base.Start();
2019-04-28 16:12:27 +00:00
animator = GetComponent<Animator>();
2019-04-28 14:28:31 +00:00
controlMode = ControlMode.Move;
}
protected void Update()
2019-04-27 12:40:09 +00:00
{
2019-04-28 14:28:31 +00:00
CheckForControlModeChange();
if(controlMode == ControlMode.Move) CheckForMovementInput();
2019-04-29 17:53:45 +00:00
if(controlMode == ControlMode.Attack) CheckForAttackInput();
CheckForTurnSkipInput();
2019-04-28 14:28:31 +00:00
}
protected void CheckForControlModeChange() {
if(Input.GetKeyDown(KeyCode.Escape)) {
controlMode = ControlMode.Move;
messageBar.Write("Control mode: Move", Color.cyan);
2019-04-28 16:45:59 +00:00
animator.SetBool("IsWalking", true);
2019-04-28 14:28:31 +00:00
}
if(Input.GetKeyDown(KeyCode.A)) {
controlMode = ControlMode.Attack;
messageBar.Write("Control mode: Attack", Color.cyan);
2019-04-28 16:45:59 +00:00
animator.SetBool("IsWalking", false);
2019-04-28 14:28:31 +00:00
}
}
protected void CheckForAttackInput() {
bool hasAttacked = false;
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
hasAttacked = GetComponent<PlayerAttack>().Attack(MapPosition + Vector2Int.left);
2019-04-28 16:12:27 +00:00
spriteRenderer.flipX = true;
2019-04-28 14:28:31 +00:00
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
hasAttacked = GetComponent<PlayerAttack>().Attack(MapPosition + Vector2Int.right);
2019-04-28 16:12:27 +00:00
spriteRenderer.flipX = false;
2019-04-28 14:28:31 +00:00
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
hasAttacked = GetComponent<PlayerAttack>().Attack(MapPosition + Vector2Int.up);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
hasAttacked = GetComponent<PlayerAttack>().Attack(MapPosition + Vector2Int.down);
}
if(hasAttacked) {
//Turn happens!
turnHandler.OnTurn();
}
2019-04-27 12:40:09 +00:00
}
2019-04-28 14:28:31 +00:00
protected void CheckForMovementInput()
2019-04-27 12:40:09 +00:00
{
2019-04-27 15:28:38 +00:00
bool hasMoved = false;
2019-04-28 14:28:31 +00:00
if (Input.GetKeyDown(KeyCode.LeftArrow))
2019-04-27 12:40:09 +00:00
{
2019-04-27 20:19:35 +00:00
if (map.CanMoveTo(MapPosition + Vector2Int.left)) {
2019-04-27 15:57:01 +00:00
transform.Translate(Vector3.left);
hasMoved = true;
2019-04-28 16:12:27 +00:00
spriteRenderer.flipX = true;
2019-04-27 15:57:01 +00:00
}
2019-04-27 12:40:09 +00:00
}
2019-04-28 14:28:31 +00:00
else if (Input.GetKeyDown(KeyCode.RightArrow))
2019-04-27 12:40:09 +00:00
{
2019-04-27 20:19:35 +00:00
if (map.CanMoveTo(MapPosition + Vector2Int.right)) {
2019-04-27 15:28:38 +00:00
transform.Translate(Vector3.right);
2019-04-27 15:57:01 +00:00
hasMoved = true;
2019-04-28 16:12:27 +00:00
spriteRenderer.flipX = false;
2019-04-27 15:28:38 +00:00
}
2019-04-27 12:40:09 +00:00
}
2019-04-28 14:28:31 +00:00
else if (Input.GetKeyDown(KeyCode.UpArrow))
2019-04-27 12:40:09 +00:00
{
2019-04-27 20:19:35 +00:00
if (map.CanMoveTo(MapPosition + Vector2Int.up)) {
2019-04-27 15:28:38 +00:00
transform.Translate(Vector3.up);
2019-04-27 15:57:01 +00:00
hasMoved = true;
2019-04-27 15:28:38 +00:00
}
2019-04-27 12:40:09 +00:00
}
2019-04-28 14:28:31 +00:00
else if (Input.GetKeyDown(KeyCode.DownArrow))
2019-04-27 12:40:09 +00:00
{
2019-04-27 20:19:35 +00:00
if (map.CanMoveTo(MapPosition + Vector2Int.down)) {
2019-04-27 15:28:38 +00:00
transform.Translate(Vector3.down);
2019-04-27 15:57:01 +00:00
hasMoved = true;
2019-04-27 15:28:38 +00:00
}
2019-04-27 12:40:09 +00:00
}
2019-04-27 15:57:01 +00:00
if(hasMoved) {
2019-04-29 19:45:54 +00:00
bool doTurn = true;
2019-04-28 12:11:21 +00:00
//Check for pickuppable items
2019-04-28 14:28:31 +00:00
List<Entity> entities = turnHandler.GetEntitiesAtPosition(MapPosition);
2019-04-28 12:11:21 +00:00
foreach(Entity entity in entities) {
2019-04-28 14:34:50 +00:00
if(entity is EntityItem) {
EntityItem item = entity as EntityItem;
2019-04-29 19:45:54 +00:00
if(item is EntityItemStairs) {
doTurn = false;
}
2019-04-28 12:11:21 +00:00
item.OnPickup(this);
}
}
//Turn happens!
2019-04-29 19:45:54 +00:00
if(doTurn) {
turnHandler.OnTurn();
}
2019-04-27 15:57:01 +00:00
}
2019-04-27 12:40:09 +00:00
}
2019-04-29 13:27:26 +00:00
2019-04-29 17:53:45 +00:00
protected void CheckForTurnSkipInput() {
if(Input.GetKeyDown(KeyCode.Space)) {
messageBar.Write("Skipped turn.", Color.cyan);
turnHandler.OnTurn();
}
}
public override void OnNewLevel() {}
2019-04-29 17:53:45 +00:00
public override void Die() {
SceneManager.LoadScene("Dead", LoadSceneMode.Single);
}
2019-04-27 12:40:09 +00:00
}