1
Fork 0

Commit a lot of stuff

This commit is contained in:
Steffo 2019-04-28 16:28:31 +02:00
parent fe80dc2a0f
commit 1ac4d2866b
13 changed files with 172 additions and 30 deletions

View file

@ -11,6 +11,7 @@ GameObject:
- component: {fileID: 2935319493830293414} - component: {fileID: 2935319493830293414}
- component: {fileID: 2935319493830293413} - component: {fileID: 2935319493830293413}
- component: {fileID: -5893075763862095451} - component: {fileID: -5893075763862095451}
- component: {fileID: 8856581523727923621}
m_Layer: 0 m_Layer: 0
m_Name: Player m_Name: Player
m_TagString: Player m_TagString: Player
@ -99,6 +100,19 @@ SpriteRenderer:
m_WasSpriteAssigned: 1 m_WasSpriteAssigned: 1
m_MaskInteraction: 0 m_MaskInteraction: 0
m_SpriteSortPoint: 0 m_SpriteSortPoint: 0
--- !u!114 &8856581523727923621
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2935319493830293412}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 05a8e698d12930547a9e83f369e506d2, type: 3}
m_Name:
m_EditorClassIdentifier:
damage: 1
--- !u!1001 &5734536248370648602 --- !u!1001 &5734536248370648602
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -44,9 +44,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2fcfe4b94620b234fb7a294bf4453a69, type: 3} m_Script: {fileID: 11500000, guid: 2fcfe4b94620b234fb7a294bf4453a69, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
hpMax: 0 hpMax: 2
overlappable: 0
hp: 0 hp: 0
sprite: {fileID: 21300000, guid: ee7ef72c830dc204c9df0595e161f049, type: 3} sprite: {fileID: 21300000, guid: ee7ef72c830dc204c9df0595e161f049, type: 3}
moveChance: 0.5
visionRange: 4 visionRange: 4
--- !u!212 &1351652798109185028 --- !u!212 &1351652798109185028
SpriteRenderer: SpriteRenderer:

View file

@ -12,7 +12,7 @@ GameObject:
- component: {fileID: 470211819356819162} - component: {fileID: 470211819356819162}
- component: {fileID: 7501912107144583665} - component: {fileID: 7501912107144583665}
m_Layer: 0 m_Layer: 0
m_Name: TEST_ Item m_Name: TEST Item
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -44,7 +44,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1f0883433553dbb4fa23a815949f596b, type: 3} m_Script: {fileID: 11500000, guid: 1f0883433553dbb4fa23a815949f596b, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
hpMax: 0 hpMax: 1
overlappable: 0 overlappable: 0
hp: 0 hp: 0
sprite: {fileID: 21300000, guid: 5e4d29e1adc2ea0429b199b82253416c, type: 3} sprite: {fileID: 21300000, guid: 5e4d29e1adc2ea0429b199b82253416c, type: 3}

View file

@ -23,6 +23,7 @@ public class Entity : MonoBehaviour
protected SpriteRenderer spriteRenderer; protected SpriteRenderer spriteRenderer;
protected TurnHandler turnHandler; protected TurnHandler turnHandler;
protected Map map; protected Map map;
protected MessageBar messageBar;
protected virtual void Start() protected virtual void Start()
{ {
@ -31,6 +32,13 @@ public class Entity : MonoBehaviour
gameController = GameObject.FindGameObjectWithTag("GameController"); gameController = GameObject.FindGameObjectWithTag("GameController");
turnHandler = gameController.GetComponentInChildren<TurnHandler>(); turnHandler = gameController.GetComponentInChildren<TurnHandler>();
map = gameController.GetComponentInChildren<Map>(); map = gameController.GetComponentInChildren<Map>();
GameObject canvas = GameObject.FindGameObjectWithTag("Canvas");
messageBar = canvas.GetComponentInChildren<MessageBar>();
hp = hpMax; hp = hpMax;
} }
public virtual void Die() {
Debug.LogWarning("Die not overridden");
Destroy(gameObject);
}
} }

View file

@ -4,6 +4,8 @@ using UnityEngine;
public class Item : Entity public class Item : Entity
{ {
public static string itemName = "White Triangle";
protected override void Start() { protected override void Start() {
base.Start(); base.Start();
overlappable = true; overlappable = true;
@ -11,7 +13,12 @@ public class Item : Entity
public virtual void OnPickup(Player player) { public virtual void OnPickup(Player player) {
Debug.LogWarning("OnPickup not overridden"); Debug.LogWarning("OnPickup not overridden");
turnHandler.WriteToMessageBar("Picked up [NULL]."); messageBar.Write("Picked up: " + itemName, Color.yellow);
Destroy(gameObject);
}
public override void Die() {
messageBar.Write("Destroyed: " + itemName, Color.red);
Destroy(gameObject); Destroy(gameObject);
} }
} }

View file

@ -138,7 +138,7 @@ public class Map : MonoBehaviour
{ {
try { try {
bool walkable = GetTile(position).walkable; bool walkable = GetTile(position).walkable;
List<Entity> entities = turnHandler.GetEntityAtPosition(position); List<Entity> entities = turnHandler.GetEntitiesAtPosition(position);
bool free = true; bool free = true;
foreach(Entity entity in entities) { foreach(Entity entity in entities) {
free &= entity.overlappable; free &= entity.overlappable;

View file

@ -17,7 +17,8 @@ public class MessageBar : MonoBehaviour
opacity = 0f; opacity = 0f;
} }
public void Write(string message) { public void Write(string message, Color color) {
text.color = color;
text.text = message; text.text = message;
opacity = 1f; opacity = 1f;
} }

View file

@ -2,20 +2,69 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public enum ControlMode {
Move,
Attack
}
public class Player : Entity public class Player : Entity
{ {
public int exp; public int exp;
public int level; public int level;
void Update() protected ControlMode controlMode;
{
CheckForMovementInput(); protected override void Start() {
base.Start();
controlMode = ControlMode.Move;
} }
void CheckForMovementInput() protected void Update()
{
CheckForControlModeChange();
if(controlMode == ControlMode.Move) CheckForMovementInput();
if(controlMode == ControlMode.Attack) CheckForAttackInput();
}
protected void CheckForControlModeChange() {
if(Input.GetKeyDown(KeyCode.Escape)) {
controlMode = ControlMode.Move;
messageBar.Write("Control mode: Move", Color.cyan);
}
if(Input.GetKeyDown(KeyCode.A)) {
controlMode = ControlMode.Attack;
messageBar.Write("Control mode: Attack", Color.cyan);
}
}
protected void CheckForAttackInput() {
bool hasAttacked = false;
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
hasAttacked = GetComponent<PlayerAttack>().Attack(MapPosition + Vector2Int.left);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
hasAttacked = GetComponent<PlayerAttack>().Attack(MapPosition + Vector2Int.right);
}
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();
}
}
protected void CheckForMovementInput()
{ {
bool hasMoved = false; bool hasMoved = false;
if (Input.GetKeyDown(KeyCode.A)) if (Input.GetKeyDown(KeyCode.LeftArrow))
{ {
if (map.CanMoveTo(MapPosition + Vector2Int.left)) { if (map.CanMoveTo(MapPosition + Vector2Int.left)) {
transform.Translate(Vector3.left); transform.Translate(Vector3.left);
@ -23,7 +72,7 @@ public class Player : Entity
spriteRenderer.flipX = false; spriteRenderer.flipX = false;
} }
} }
else if (Input.GetKeyDown(KeyCode.D)) else if (Input.GetKeyDown(KeyCode.RightArrow))
{ {
if (map.CanMoveTo(MapPosition + Vector2Int.right)) { if (map.CanMoveTo(MapPosition + Vector2Int.right)) {
transform.Translate(Vector3.right); transform.Translate(Vector3.right);
@ -31,14 +80,14 @@ public class Player : Entity
spriteRenderer.flipX = true; spriteRenderer.flipX = true;
} }
} }
else if (Input.GetKeyDown(KeyCode.W)) else if (Input.GetKeyDown(KeyCode.UpArrow))
{ {
if (map.CanMoveTo(MapPosition + Vector2Int.up)) { if (map.CanMoveTo(MapPosition + Vector2Int.up)) {
transform.Translate(Vector3.up); transform.Translate(Vector3.up);
hasMoved = true; hasMoved = true;
} }
} }
else if (Input.GetKeyDown(KeyCode.S)) else if (Input.GetKeyDown(KeyCode.DownArrow))
{ {
if (map.CanMoveTo(MapPosition + Vector2Int.down)) { if (map.CanMoveTo(MapPosition + Vector2Int.down)) {
transform.Translate(Vector3.down); transform.Translate(Vector3.down);
@ -47,7 +96,7 @@ public class Player : Entity
} }
if(hasMoved) { if(hasMoved) {
//Check for pickuppable items //Check for pickuppable items
List<Entity> entities = turnHandler.GetEntityAtPosition(MapPosition); List<Entity> entities = turnHandler.GetEntitiesAtPosition(MapPosition);
foreach(Entity entity in entities) { foreach(Entity entity in entities) {
if(entity is Item) { if(entity is Item) {
Item item = entity as Item; Item item = entity as Item;

View file

@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
protected Player player;
protected GameObject gameController;
protected TurnHandler turnHandler;
protected Map map;
protected void Start() {
player = GetComponent<Player>();
gameController = GameObject.FindGameObjectWithTag("GameController");
turnHandler = gameController.GetComponentInChildren<TurnHandler>();
map = gameController.GetComponentInChildren<Map>();
}
public virtual bool Attack(Vector2Int target) {
//Returns if the attack was successful.
Debug.LogWarning("Attack not overridden");
return false;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1bb56d02d177e9e4e9146940057ba1ad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack_Melee : PlayerAttack
{
public float damage = 1f;
public override bool Attack(Vector2Int target) {
List<Entity> targetEntities = turnHandler.GetEntitiesAtPosition(target);
if(targetEntities.Count == 0) {
return false;
}
Entity targetEntity = targetEntities[0];
targetEntity.hp -= damage;
return true;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 05a8e698d12930547a9e83f369e506d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -4,25 +4,22 @@ using UnityEngine;
public class TurnHandler : MonoBehaviour public class TurnHandler : MonoBehaviour
{ {
private MessageBar messageBar;
private void Start() {
GameObject canvas = GameObject.FindGameObjectWithTag("Canvas");
messageBar = canvas.GetComponentInChildren<MessageBar>();
}
public void WriteToMessageBar(string message) {
messageBar.Write(message);
}
public void OnTurn() { public void OnTurn() {
AI[] ais = gameObject.GetComponentsInChildren<AI>(); Entity[] entities = gameObject.GetComponentsInChildren<Entity>();
foreach(AI ai in ais) { foreach(Entity entity in entities) {
//Check for deaths
if(entity.hp <= 0) {
entity.Die();
}
//Move AIs
if(entity is AI) {
AI ai = entity as AI;
ai.OnTurn(); ai.OnTurn();
} }
} }
}
public List<Entity> GetEntityAtPosition(Vector2Int position) { public List<Entity> GetEntitiesAtPosition(Vector2Int position) {
Entity[] entities = GetComponentsInChildren<Entity>(); Entity[] entities = GetComponentsInChildren<Entity>();
List<Entity> found = new List<Entity>(); List<Entity> found = new List<Entity>();
foreach(Entity entity in entities) { foreach(Entity entity in entities) {