-
5
- 实现地图的生成、游戏关卡的管理、以及一些简单的GUI的显示与关闭
- using UnityEngine;
- using System;
- using System.Collections.Generic;
- using Random = UnityEngine.Random;
-
- public class BoardMannager : 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 InitialiseList()
- {
- 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 = -1; 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;
- instance.transform.SetParent(boardHolder);
- }
- }
- }
-
- Vector3 RandomPosition()
- {
- int randomIndex = Random.Range(0, gridPositions.Count);
- Vector3 randomPosition = gridPositions[randomIndex];
- gridPositions.RemoveAt(randomIndex);
- return randomPosition;
- }
-
-
- void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maxmum)
- {
- int objectCount = Random.Range(minimum,maxmum+1);
-
- for (int i = 0; i < objectCount; i++)
- {
- Vector3 randomPosition = RandomPosition();
- GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
- Instantiate(tileChoice,randomPosition,Quaternion.identity);
- }
- }
-
- public void SetupScene(int level)
- {
- BoardSetup();
- InitialiseList();
- LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
- LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum) ;
- int enemyCount = (int)Mathf.Log(level,2f);
- LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
- Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);
-
- }
- }
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
-
-
- public class GameManager : MonoBehaviour {
- public float levelStartDelay = 2f;
- public float turnDelay = .1f;
- public static GameManager instance = null;
- public BoardMannager boardScript;
- public int platerFoodPoints = 100;
- [HideInInspector]public bool playersTurn = true;
-
- private Text levelText;
- private GameObject levelImage;
- private int level = 1;
- private List<Enemy> enemies;
- private bool enemiesMoving;
- private bool doingSetup;
-
-
- void Awake() {
- if (instance==null)
- {
- instance = this;
- }
- else if (instance!=null)
- Destroy(gameObject);
- DontDestroyOnLoad(gameObject);
- enemies=new List<Enemy>();
- boardScript=GetComponent<BoardMannager>();
- InitGame();
- }
-
- private 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;
- }
- IEnumerator MoveEnemies( )
- {
- enemiesMoving = true;
- yield return new WaitForSeconds(turnDelay);
- if (enemies.Count==0)
- {
- yield return new WaitForSeconds(turnDelay);
- }
- for (int i = 0; i < enemies.Count; i++)
- {
- enemies[i].MoveEnemy();
- yield return new WaitForSeconds(enemies[i].moveTime);
- }
- playersTurn = true;
- enemiesMoving = false;
- }
-
- public void AddEnemyToList(Enemy script)
- {
- enemies.Add(script);
- }
-
-
- public void GameOver()
- {
- levelText.text = "After " + level + " days,you starved.";
- levelImage.SetActive(true);
- enabled = false;
- }
-
- void Update () {
- if (playersTurn || enemiesMoving||doingSetup)
- return;
- StartCoroutine(MoveEnemies());
-
- }
- }
-
相关阅读:
scws安装
Redis使用
安装redis服务
mkdir(): Permission denied
Zabbix告警脚本-邮件
Zabbix告警脚本-微信
Zabbix实现微信告警
Docker学习之安装mysql
docker安装使用
centos6与centos7区别
-
原文地址:https://www.cnblogs.com/wshyj/p/6096205.html
Copyright © 2020-2023
润新知