This commit is contained in:
Tommy Parnell
2015-04-12 22:33:50 -04:00
commit ff3692e87a
155 changed files with 7148 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
using UnityEngine;
using System.Collections.Generic;
using System;
using System.Collections.Specialized;
using Random = UnityEngine.Random;
public class Boardmanager : MonoBehaviour {
[Serializable]
public class Count
{
public int Minimum;
public int Maximum;
public Count(int min, int max)
{
Minimum = min;
Maximum = max;
}
}
public int Columns = 8;
public int Rows = 8;
public Count WallCount = new Count(5,9);
public Count FoodCount = new Count(1,5);
public GameObject Exit;
public GameObject[] FloorTiles;
public GameObject[] WallTiles;
public GameObject[] FoodTiles;
public GameObject[] EnemyTiles;
public GameObject[] OuterWallTiles;
private Transform _boardHolder;
private List<Vector3> _gridPositions = new List<Vector3>();
void InitializeList()
{
_gridPositions.Clear();
for (int x = 1; x < Columns -1 ; x++)
{
for (int y = 1; y < Rows - 1; y++)
{
_gridPositions.Add(new Vector3(x,y,0f));
}
}
}
void BoardSetup()
{
_boardHolder = new GameObject("Board").transform;
for (int x = -1; x < Columns + 1; x++)
{
for (int y = 0; y < Rows + 1; y++)
{
GameObject toInstantiate = FloorTiles[Random.Range(0, FloorTiles.Length)];
if (x == -1 || x == Columns || y == -1 || y == Rows)
{
toInstantiate = OuterWallTiles[Random.Range(0, OuterWallTiles.Length)];
}
GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
if (instance == null)
{
throw new Exception("Unable to produce instance");
}
instance.transform.SetParent(_boardHolder);
}
}
}
Vector3 RandomPosition()
{
var randomIndex = Random.Range(0, _gridPositions.Count);
var randomPosition = _gridPositions[randomIndex];
_gridPositions.RemoveAt(randomIndex);
return randomPosition;
}
void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)
{
var objectCount = Random.Range(minimum, maximum + 1);
for (int i = 0; i < objectCount; i++)
{
var tileChoice = tileArray[Random.Range(0, tileArray.Length)];
Instantiate(tileChoice, RandomPosition(), Quaternion.identity);
}
}
public void SetupScene(int level)
{
BoardSetup();
InitializeList();
LayoutObjectAtRandom(WallTiles, WallCount.Minimum, WallCount.Maximum );
LayoutObjectAtRandom(FoodTiles, FoodCount.Minimum, FoodCount.Maximum);
var enemyCount = (int) Mathf.Log(level, 2f);
LayoutObjectAtRandom(EnemyTiles, enemyCount, enemyCount);
Instantiate(Exit, new Vector3(Columns - 1, Rows - 1, 0f), Quaternion.identity);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: dee9632f692b03747990adc641ff7e7c
timeCreated: 1428868932
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

62
Assets/Scripts/Enemy.cs Normal file
View File

@@ -0,0 +1,62 @@
using UnityEngine;
using System.Collections;
public class Enemy : MovingObject {
public int playerDamage;
private Animator animator;
private Transform target;
private bool skipMove;
public AudioClip enemyAttack1;
public AudioClip enemyAttack2;
// Use this for initialization
protected override void Start()
{
base.Start();
GameManager.Instance.AddEnemyToList(this);
animator = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").transform;
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
if (skipMove)
{
skipMove = false;
return;
}
base.AttemptMove<T>(xDir, yDir);
skipMove = true;
}
public void MoveEnemy()
{
var xDir = 0;
var yDir = 0;
//in the same column?
if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon)
{
yDir = target.position.y > transform.position.y ? 1 : -1;
}
else
{
xDir = target.position.x > transform.position.x ? 1 : -1;
}
AttemptMove<Player>(xDir, yDir);
}
protected override void OnCantMove<T>(T component)
{
var hitPlayer = component as Player;
animator.SetTrigger("EnemyAttack");
SoundManager.instance.RandomizeSfx(enemyAttack1, enemyAttack2);
hitPlayer.LoseFood(playerDamage);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e9ea06b07394031488c8a06a5cd90ea6
timeCreated: 1428875994
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,98 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
public class GameManager : MonoBehaviour
{
public float TurnDelay = .1f;
public float LevelStartDelay = 2f;
public static GameManager Instance = null;
public Boardmanager BoardScript;
public int PlayerFoodPoints = 100;
[HideInInspector] public bool PlayersTurn = true;
private Text levelText;
private GameObject levelImage;
private int Level = 1;
private IList<Enemy> enemies;
private bool enemiesMoving;
private bool doingSetup;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this);
}
DontDestroyOnLoad(gameObject);
enemies = new List<Enemy>();
BoardScript = GetComponent<Boardmanager>();
InitGame();
}
void OnLevelWasLoaded(int index)
{
Level++;
InitGame();
}
void InitGame()
{
doingSetup = true;
levelImage = GameObject.Find("LevelImage");
levelText = GameObject.Find("LevelText").GetComponent<Text>();
levelText.text = "Day " + Level;
levelImage.SetActive(true);
Invoke("HideLevelImage", LevelStartDelay);
enemies.Clear();
BoardScript.SetupScene(Level);
}
private void HideLevelImage()
{
levelImage.SetActive(false);
doingSetup = false;
}
public void GameOver()
{
levelText.text = "After " + Level + " days, you starved.";
levelImage.SetActive(true);
enabled = false;
}
void Update()
{
if (PlayersTurn || enemiesMoving || doingSetup) return;
StartCoroutine(MoveEnemies());
}
public void AddEnemyToList(Enemy script)
{
enemies.Add(script);
}
IEnumerator MoveEnemies()
{
enemiesMoving = true;
yield return new WaitForSeconds(TurnDelay);
if (enemies.Count == 0)
{
yield return new WaitForSeconds(TurnDelay);
}
foreach (var enemy in enemies)
{
enemy.MoveEnemy();
yield return new WaitForSeconds(enemy.MoveTime);
}
PlayersTurn = true;
enemiesMoving = false;
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 31afdce4e16516f4b990535247f1d050
timeCreated: 1428868940
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

17
Assets/Scripts/Loader.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
using UnityEngine;
using System.Collections;
public class Loader : MonoBehaviour
{
public GameObject gameManager;
void Awake()
{
if (GameManager.Instance == null)
{
Instantiate(gameManager);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: efe7c5ed74994a6488d7da4bf78d322d
timeCreated: 1428872480
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,59 @@
using UnityEngine;
using System.Collections;
public abstract class MovingObject : MonoBehaviour
{
public float MoveTime = 0.1f;
public LayerMask BlockingLayer;
private BoxCollider2D BoxCollider;
private Rigidbody2D Rb2d;
private float InverseMoveTime;
protected virtual void Start()
{
BoxCollider = GetComponent<BoxCollider2D>();
Rb2d = GetComponent<Rigidbody2D>();
InverseMoveTime = 1f/MoveTime;
}
protected IEnumerator SmoothMovement(Vector3 end)
{
var sqrRemainingDistance = (transform.position - end).sqrMagnitude;
while (sqrRemainingDistance > float.Epsilon)
{
Vector3 newPosition = Vector3.MoveTowards(Rb2d.position, end, InverseMoveTime*Time.deltaTime);
Rb2d.MovePosition(newPosition);
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
yield return null;
}
}
protected bool Move(int xDir, int yDir, out RaycastHit2D hit)
{
Vector2 start = transform.position;
Vector2 end = start + new Vector2(xDir, yDir);
BoxCollider.enabled = false;
hit = Physics2D.Linecast(start, end, BlockingLayer);
BoxCollider.enabled = true;
if (hit.transform != null) return false;
StartCoroutine(SmoothMovement(end));
return true;
}
protected virtual void AttemptMove<T>(int xDir, int yDir)
where T : Component
{
RaycastHit2D hit;
bool canMove = Move(xDir, yDir, out hit);
if (hit.transform == null)
return;
var hitComponent = hit.transform.GetComponent<T>();
if (!canMove && hitComponent != null) { OnCantMove(hitComponent);}
}
protected abstract void OnCantMove<T>(T component)
where T : Component;
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: fd42a6692e72c7b47ba9b0f7c82c4d56
timeCreated: 1428872744
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

130
Assets/Scripts/Player.cs Normal file
View File

@@ -0,0 +1,130 @@
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Player : MovingObject
{
public int WallDamage = 1;
public int PointsPerFood = 10;
public int PointsPerSoda = 20;
public float RestartLevelDelay = 1f;
public Text foodText;
private Animator _animator;
private int _food;
public AudioClip moveSound1;
public AudioClip moveSound2;
public AudioClip eatSound1;
public AudioClip eatSound2;
public AudioClip drinkSound1;
public AudioClip drinkSound2;
public AudioClip gameOverSound;
// Use this for initialization
protected override void Start ()
{
foodText.text = "Food: " + _food;
_animator = GetComponent<Animator>();
_food = GameManager.Instance.PlayerFoodPoints;
base.Start();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (string.Equals(other.tag, "Exit", StringComparison.CurrentCultureIgnoreCase))
{
Invoke("Restart", RestartLevelDelay);
enabled = false;
}
else if (string.Equals(other.tag, "Food", StringComparison.CurrentCultureIgnoreCase))
{
_food += PointsPerFood;
foodText.text = "+" + PointsPerFood + " Food: " + _food;
SoundManager.instance.RandomizeSfx(eatSound1, eatSound2);
other.gameObject.SetActive(false);
}
else if (string.Equals(other.tag, "Soda", StringComparison.CurrentCultureIgnoreCase))
{
foodText.text = "+" + PointsPerFood + " Food: " + _food;
SoundManager.instance.RandomizeSfx(drinkSound1, drinkSound2);
_food += PointsPerSoda;
other.gameObject.SetActive(false);
}
}
void Update()
{
//If it's not the player's turn, exit the function.
if (!GameManager.Instance.PlayersTurn) return;
int horizontal = 0; //Used to store the horizontal move direction.
int vertical = 0; //Used to store the vertical move direction.
//Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
horizontal = (int)(Input.GetAxisRaw("Horizontal"));
//Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
vertical = (int)(Input.GetAxisRaw("Vertical"));
//Check if moving horizontally, if so set vertical to zero.
if (horizontal != 0)
{
vertical = 0;
}
if (horizontal != 0 || vertical != 0)
{
//Call AttemptMove passing in the generic parameter Wall, since that is what Player may interact with if they encounter one (by attacking it)
//Pass in horizontal and vertical as parameters to specify the direction to move Player in.
AttemptMove<Wall>(horizontal, vertical);
}
}
private void OnDisable()
{
GameManager.Instance.PlayerFoodPoints = _food;
}
private void CheckIfGameOver()
{
if (_food <= 0)
{
SoundManager.instance.PlaySingle(gameOverSound);
SoundManager.instance.musicSource.Stop();
GameManager.Instance.GameOver();
}
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
_food--;
foodText.text = "Food: " + _food;
base.AttemptMove<T>(xDir, yDir);
RaycastHit2D hit;
if (Move(xDir, yDir, out hit))
{
SoundManager.instance.RandomizeSfx(moveSound1, moveSound2);
}
CheckIfGameOver();
GameManager.Instance.PlayersTurn = false;
}
protected override void OnCantMove<T>(T component)
{
var hitWall = component as Wall;
hitWall.DamageWall(WallDamage);
_animator.SetTrigger("PlayerChop");
}
private void Restart()
{
Application.LoadLevel(Application.loadedLevel);
}
public void LoseFood(int loss)
{
_animator.SetTrigger("playerhit");
_food -= loss;
foodText.text = "-" + loss + " Food: " + _food;
CheckIfGameOver();
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3cb8cdc68ed227943b367958e3cbfe4e
timeCreated: 1428874985
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using UnityEngine;
using System.Collections;
public class SoundManager : MonoBehaviour
{
public AudioSource efxSource;
public AudioSource musicSource;
public static SoundManager instance = null;
public float lowPitchRange = .95f;
public float highPitchRange = 1.05f;
// Use this for initialization
void Start ()
{
if (instance == null)
{
instance = this;
}
else if(instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
public void PlaySingle(AudioClip clip)
{
efxSource.clip = clip;
efxSource.Play();
}
public void RandomizeSfx(params AudioClip[] clips)
{
var randomIndex = Random.Range(0, clips.Length);
float randomPitch = Random.Range(lowPitchRange, highPitchRange);
efxSource.pitch = randomPitch;
efxSource.clip = clips[randomIndex];
efxSource.Play();
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 327ebb5b95b7be64d99c702e2b2c6e8d
timeCreated: 1428884038
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

28
Assets/Scripts/Wall.cs Normal file
View File

@@ -0,0 +1,28 @@
using UnityEngine;
using System.Collections;
public class Wall : MonoBehaviour
{
public Sprite dmgSprite;
public int Hp = 4;
private SpriteRenderer spriteRenderer;
public AudioClip chopSound1;
public AudioClip chopSound2;
void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
public void DamageWall(int loss)
{
SoundManager.instance.RandomizeSfx(chopSound1, chopSound2);
spriteRenderer.sprite = dmgSprite;
Hp -= loss;
if (Hp <= 0)
{
gameObject.SetActive(false);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e2f3b2c94e0070946847bbc99e928a41
timeCreated: 1428873617
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: