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

130 lines
4 KiB
C#
Raw Normal View History

2019-04-27 12:40:09 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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 19:01:35 +00:00
public Dictionary<InventoryItems, int> inventory;
public InventoryItems selectedItem;
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;
2019-04-28 19:01:35 +00:00
//Init inventory
inventory.Add(InventoryItems.HealthPotion, 0);
2019-04-28 14:28:31 +00:00
}
protected void Update()
2019-04-27 12:40:09 +00:00
{
2019-04-28 14:28:31 +00:00
CheckForControlModeChange();
2019-04-28 19:01:35 +00:00
CheckForInventoryInput();
2019-04-28 14:28:31 +00:00
if(controlMode == ControlMode.Move) CheckForMovementInput();
if(controlMode == ControlMode.Attack) CheckForAttackInput();
}
2019-04-28 19:01:35 +00:00
protected void CheckForInventoryInput() {
if(Input.GetKeyDown(KeyCode.X)) {
if(selectedItem == InventoryItems.Empty) {
selectedItem = 0;
}
else {
selectedItem += 1;
}
}
}
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-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-28 12:11:21 +00:00
item.OnPickup(this);
}
}
//Turn happens!
2019-04-27 20:19:35 +00:00
turnHandler.OnTurn();
2019-04-27 15:57:01 +00:00
}
2019-04-27 12:40:09 +00:00
}
}