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

65 lines
1.5 KiB
C#
Raw Normal View History

2019-04-27 12:40:09 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
2019-04-27 15:57:01 +00:00
public int exp;
public int level;
public int hpMax;
[AfterStartAttribute]
public int hp;
private Map map;
private GameObject gameController;
2019-04-27 12:40:09 +00:00
void Start()
{
2019-04-27 15:57:01 +00:00
map = GameObject.FindGameObjectWithTag("Map").GetComponent<Map>();
gameController = GameObject.FindGameObjectWithTag("GameController");
hp = hpMax;
2019-04-27 12:40:09 +00:00
}
void Update()
{
2019-04-27 15:28:38 +00:00
CheckForMovementInput();
2019-04-27 12:40:09 +00:00
}
2019-04-27 15:28:38 +00:00
void CheckForMovementInput()
2019-04-27 12:40:09 +00:00
{
2019-04-27 15:28:38 +00:00
bool hasMoved = false;
2019-04-27 15:24:26 +00:00
if (Input.GetKeyDown(KeyCode.A))
2019-04-27 12:40:09 +00:00
{
2019-04-27 15:57:01 +00:00
if (map.CanMoveTo(Vector2Int.left)) {
transform.Translate(Vector3.left);
hasMoved = true;
}
2019-04-27 12:40:09 +00:00
}
2019-04-27 15:28:38 +00:00
else if (Input.GetKeyDown(KeyCode.D))
2019-04-27 12:40:09 +00:00
{
2019-04-27 15:57:01 +00:00
if (map.CanMoveTo(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-27 15:28:38 +00:00
}
2019-04-27 12:40:09 +00:00
}
2019-04-27 15:28:38 +00:00
else if (Input.GetKeyDown(KeyCode.W))
2019-04-27 12:40:09 +00:00
{
2019-04-27 15:57:01 +00:00
if (map.CanMoveTo(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-27 15:28:38 +00:00
else if (Input.GetKeyDown(KeyCode.S))
2019-04-27 12:40:09 +00:00
{
2019-04-27 15:57:01 +00:00
if (map.CanMoveTo(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) {
gameController.BroadcastMessage("Turn");
}
2019-04-27 12:40:09 +00:00
}
}