Merge pull request #9 from dnutiu/game-manager

Game manager
This commit is contained in:
Denis-Cosmin Nutiu 2019-06-08 14:50:42 +03:00 committed by GitHub
commit afa6c0ab6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 137 additions and 37 deletions

3
.gitignore vendored
View file

@ -140,6 +140,9 @@ fabric.properties
# Uncomment this line if you wish to ignore the asset store tools plugin # Uncomment this line if you wish to ignore the asset store tools plugin
# [Aa]ssets/AssetStoreTools* # [Aa]ssets/AssetStoreTools*
# Ignore Rider Plugin
[Aa]ssets/Plugins/Editor/JetBrains/*
# TextMesh Pro files # TextMesh Pro files
[Aa]ssets/TextMesh*Pro/ [Aa]ssets/TextMesh*Pro/

View file

@ -8215,9 +8215,10 @@ GameObject:
serializedVersion: 6 serializedVersion: 6
m_Component: m_Component:
- component: {fileID: 840487112} - component: {fileID: 840487112}
- component: {fileID: 840487113}
- component: {fileID: 840487111} - component: {fileID: 840487111}
m_Layer: 0 m_Layer: 0
m_Name: LevelManager m_Name: GameManager
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -8235,14 +8236,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 308dfc6b26c3b422faf1717231fc888b, type: 3} m_Script: {fileID: 11500000, guid: 308dfc6b26c3b422faf1717231fc888b, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
columns: 30 boardHolder: {fileID: 585332173}
rows: 20
startPosition: {fileID: 185746015} startPosition: {fileID: 185746015}
indestructibleWallPrefab: {fileID: 5959564480851182330, guid: dd25aabed7c6c491eb405f1e5c5f4d99, indestructibleWallPrefab: {fileID: 5959564480851182330, guid: dd25aabed7c6c491eb405f1e5c5f4d99,
type: 3} type: 3}
destructibleWallPrefab: {fileID: 4177940729222266681, guid: da559e681f8d34d58bb5f3457045664a, destructibleWallPrefab: {fileID: 4177940729222266681, guid: da559e681f8d34d58bb5f3457045664a,
type: 3} type: 3}
boardHolder: {fileID: 585332173}
--- !u!4 &840487112 --- !u!4 &840487112
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -8257,6 +8256,18 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 6 m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &840487113
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 840487110}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 697f5ae5656c74dcd8d7f5d619adb0ac, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1135949884 --- !u!1 &1135949884
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -0,0 +1,34 @@
using UnityEngine;
namespace src.Managers
{
public class GameManager : MonoBehaviour
{
public static GameManager Instance = null;
private LevelManager _levelManager;
public void Awake()
{
if (Instance == null)
{
Instance = this;
}
else if (Instance != null)
{
Destroy(gameObject);
}
/* Don't destroy when reloading the scene */
DontDestroyOnLoad(gameObject);
_levelManager = GetComponent<LevelManager>();
InitGame();
}
private void InitGame()
{
_levelManager.InitLevel();
}
}
}

View file

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

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using src.Base; using src.Base;
using src.Helpers; using src.Helpers;
using src.Wall; using src.Wall;
@ -11,75 +12,101 @@ namespace src.Managers
{ {
public class Count public class Count
{ {
public readonly int Min; private readonly int _min;
public readonly int Max; private readonly int _max;
public Count(int min, int max) public Count(int min, int max)
{ {
Min = min; _min = min;
Max = max; _max = max;
} }
public int RandomIntRange() public int RandomIntRange()
{ {
return Mathf.FloorToInt(Random.Range(Min, Max)); return Mathf.FloorToInt(Random.Range(_min, _max));
} }
} }
public int columns = 30; public Count DestructibleWallCount
public int rows = 20; {
get => _destructibleWallCount;
set => _destructibleWallCount = value;
}
/* Specifies how many objects we want per level. */ public Count UpgradesCount
public Count destructibleWallCount = new Count(150, 350); {
public Count upgradesCount = new Count(0, 5); get => _upgradesCount;
public Count enemyCount = new Count(20, 50); set => _upgradesCount = value;
}
public Count EnemyCount
{
get => _enemyCount;
set => _enemyCount = value;
}
/* Used to group spawned objects */
public Transform boardHolder;
/* Holds the starting position of the player */ /* Holds the starting position of the player */
public Transform startPosition; public Transform startPosition;
public GameObject indestructibleWallPrefab; public GameObject indestructibleWallPrefab;
public GameObject destructibleWallPrefab; public GameObject destructibleWallPrefab;
/* Used to group spawned objects */ /* Specifies how many objects we want per level. */
public Transform boardHolder; private Count _destructibleWallCount = new Count(150, 350);
private Count _upgradesCount = new Count(0, 5);
private Count _enemyCount = new Count(20, 50);
/* The size of the board. */
private const int Columns = 30;
private const int Rows = 20;
/* Holds the available positions */ /* Holds the available positions */
private readonly List<Vector3> _freeGridPositions = new List<Vector3>(); private readonly List<Vector3> _freeGridPositions = new List<Vector3>();
private readonly List<GameObject> _destructibleWalls = new List<GameObject>(); private readonly List<GameObject> _destructibleWalls = new List<GameObject>();
/* Test only */
public void Awake()
{
InitBoard();
SetupLevel();
SetupExit();
SetupUpgrades();
}
private void SetupUpgrades() private void SetupUpgrades()
{ {
var count = upgradesCount.RandomIntRange(); var count = _upgradesCount.RandomIntRange();
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
if (_destructibleWalls.Count == 0)
{
Debug.LogWarning("No destructible walls left, cannot spawn upgrade.");
continue;
}
/* Get the destructible wall script and make it to spawn the upgrade */ /* Get the destructible wall script and make it to spawn the upgrade */
var wall = _destructibleWalls.PopRandom().GetComponent<DestructibleWall>(); var wall = _destructibleWalls.PopRandom().GetComponent<DestructibleWall>();
Debug.Log($"Spawned upgrade at: x:{wall.XCoordinate} y:{wall.YCoordinate}");
wall.SpawnsUpgrade(); wall.SpawnsUpgrade();
} }
} }
private void SetupExit() private void SetupExit()
{ {
if (_destructibleWalls.Count == 0)
{
Debug.LogWarning("No destructible walls found, cannot spawn exit!");
return;
}
/* Get the destructible wall script and make it to spawn the exit */ /* Get the destructible wall script and make it to spawn the exit */
var wall = _destructibleWalls.PopRandom().GetComponent<DestructibleWall>(); var wall = _destructibleWalls.PopRandom().GetComponent<DestructibleWall>();
Debug.Log($"Spawned exit at: x:{wall.XCoordinate} y:{wall.YCoordinate}");
wall.SpawnsExit(); wall.SpawnsExit();
} }
public void InitBoard() /* Place the indestructible tiles on the board and saves the
* unused positions in a list. */
private void InitBoard()
{ {
_freeGridPositions.Clear(); _freeGridPositions.Clear();
/* We want to iterate over the X axis taking into consideration the startPosition's offset */ /* We want to iterate over the X axis taking into consideration the startPosition's offset */
for (var x = startPosition.position.x; x < columns; x++) for (var x = startPosition.position.x; x < Columns; x++)
{ {
for (var y = startPosition.position.y; y > rows * -1; y--) for (var y = startPosition.position.y; y > Rows * -1; y--)
{ {
/* We want the following positions to be a safe zone. */ /* We want the following positions to be a safe zone. */
/* Don't place anything on starting position */ /* Don't place anything on starting position */
@ -112,10 +139,10 @@ namespace src.Managers
} }
} }
public void SetupLevel() /* Randomly places destructible tiles on the level. */
private void SetupLevelDestructibleWalls()
{ {
var random = new Random(); var numberOfDestructilbeWallsToPlace = _destructibleWallCount.RandomIntRange();
var numberOfDestructilbeWallsToPlace = destructibleWallCount.RandomIntRange();
_freeGridPositions.ShuffleList(); _freeGridPositions.ShuffleList();
foreach (var nextPosition in _freeGridPositions) foreach (var nextPosition in _freeGridPositions)
@ -132,6 +159,7 @@ namespace src.Managers
private void PlaceDestructibleTile(Vector3 position) private void PlaceDestructibleTile(Vector3 position)
{ {
Debug.Log($"PlaceDestructibleTile: x:{position.x} y:{position.y}");
var instance = Instantiate(destructibleWallPrefab, position, Quaternion.identity); var instance = Instantiate(destructibleWallPrefab, position, Quaternion.identity);
_destructibleWalls.Add(instance); _destructibleWalls.Add(instance);
instance.transform.SetParent(boardHolder); instance.transform.SetParent(boardHolder);
@ -150,5 +178,14 @@ namespace src.Managers
instance.transform.SetParent(boardHolder); instance.transform.SetParent(boardHolder);
return true; return true;
} }
/* Initializes the level. */
public void InitLevel()
{
InitBoard();
SetupLevelDestructibleWalls();
SetupExit();
SetupUpgrades();
}
} }
} }

View file

@ -16,13 +16,17 @@ namespace src.Wall
{ {
_spawnUpgrade = true; _spawnUpgrade = true;
} }
public float XCoordinate => transform.position.x;
public float YCoordinate => transform.position.y;
public void OnDestroy() public void OnDestroy()
{ {
if (_spawnExit) if (_spawnExit)
{ {
// TODO Spawn an exit // TODO Spawn an exit
} else if (_spawnUpgrade) }
else if (_spawnUpgrade)
{ {
// TODO Spawn an upgrade, use composition to UpgradeManager // TODO Spawn an upgrade, use composition to UpgradeManager
// to get random / desired upgrade // to get random / desired upgrade

View file

@ -6,7 +6,7 @@ AnimatorController:
m_CorrespondingSourceObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_Name: MockPlayer m_Name: PlayerAnimator
serializedVersion: 5 serializedVersion: 5
m_AnimatorParameters: [] m_AnimatorParameters: []
m_AnimatorLayers: m_AnimatorLayers: