Example #1
0
        private static void HandleUIMessage(UIMessage message)
        {
            switch (message.type)
            {
            case UIMessage.MessageType.SplashContinue:
            {
                Assert.IsTrue(_gameState == GameState.Bootstrap);

                _gameState = GameState.MainMenu;
                Utilities.LoadScene(SceneName.MainMenu);
            } break;

            case UIMessage.MessageType.StartNewGame:
            {
                Assert.IsTrue(_gameState == GameState.MainMenu);

                var difficulty = (Difficulty)message.data[0].DropdownIndex;
                var numTeams   = message.data[1].DropdownIndex + 1;
                StartNewGame(difficulty, numTeams);
            } break;

            case UIMessage.MessageType.QuitGame:
            {
                Assert.IsTrue(_gameState == GameState.GameInProgress);

                EndGame();
            } break;
            }
        }
Example #2
0
 private void HandleUIMessage(UIMessage message)
 {
     if (message.type == _message.type)
     {
         _isKeyActive = true;
     }
 }
Example #3
0
        private void HandleUIMessage(UIMessage message)
        {
            switch (message.type)
            {
            case UIMessage.MessageType.MoveUp:
                _moving.x = 0f;
                _moving.z = 1f;
                _speed    = 1f;
                break;

            case UIMessage.MessageType.MoveDown:
                _moving.x = 0f;
                _moving.z = -1f;
                _speed    = 1f;
                break;

            case UIMessage.MessageType.MoveLeft:
                _moving.x = -1f;
                _moving.z = 0f;
                _speed    = 1f;
                break;

            case UIMessage.MessageType.MoveRight:
                _moving.x = 1f;
                _moving.z = 0f;
                _speed    = 1f;
                break;

            case UIMessage.MessageType.LookUp:
                _facing = 0f;
                break;

            case UIMessage.MessageType.LookDown:
                _facing = 180f;
                break;

            case UIMessage.MessageType.LookLeft:
                _facing = 270f;
                break;

            case UIMessage.MessageType.LookRight:
                _facing = 90f;
                break;

            case UIMessage.MessageType.Shoot:
                Shoot();
                break;
            }
        }
Example #4
0
        private void OnHealthChanged(Entity e, HealthModule h, int delta)
        {
            GameUI.BroadcastMessage(UIMessage.Create(UIMessage.MessageType.LoseLife,
                                                     new UIMessage.Datum()
            {
                type = UIMessage.Datum.Type.Int, value = h.HitPoints.ToString()
            }));

            if (h.HitPoints <= 0)
            {
                _agent.Disable();

                GameUI.BroadcastMessage(UIMessage.Create(UIMessage.MessageType.GameOver));
            }
        }
Example #5
0
        private void HandleMessage(UIMessage message)
        {
            switch (message.type)
            {
            case UIMessage.MessageType.PauseGame:
            {
                SetPaused(!_isPaused);
                _parentPause.SetActive(_isPaused);
                _parentInProgress.SetActive(!_isPaused);
            } break;

            case UIMessage.MessageType.GameOver:
            {
                SetPaused(false);
                _parentPause.SetActive(false);
                _parentInProgress.SetActive(false);
                _parentGameOver.SetActive(true);
            } break;

            case UIMessage.MessageType.QuitGame:
            {
                SetPaused(false);
            } break;

            case UIMessage.MessageType.LoseLife:
            {
                for (int i = 0; i < _parentLives.Length; i++)
                {
                    _parentLives[i].SetActive(i < message.data[0].IntValue);
                }
            } break;

            case UIMessage.MessageType.LevelCleared:
            {
                _parentInProgress.SetActive(false);
                _parentLevelCleared.SetActive(true);
            } break;
            }
        }
Example #6
0
        private static IEnumerator InitializeLevel(Difficulty difficulty, int numTeams)
        {
            int   worldSize, desiredEnemies;
            float tileSize = GameConfig.TERRAIN_TILE_SIZE;

            // generate terrain data
            WorldTileConfig[,] worldSeed;
            switch (difficulty)
            {
            case Difficulty.Easy:
                worldSeed      = WorldGenSeed.SEED_MAZE1;
                worldSize      = 7;
                desiredEnemies = 3;
                break;

            case Difficulty.Normal:
                worldSeed      = WorldGenSeed.SEED_MAZE1;
                worldSize      = 9;
                desiredEnemies = 5;
                break;

            case Difficulty.Hard:
                worldSeed      = WorldGenSeed.SEED_MAZE1;
                worldSize      = 11;
                desiredEnemies = 20;
                break;

            default:
                throw new Exception($"Unhandled difficulty level: {difficulty}");
            }

            int randomSeed = UnityEngine.Random.Range(0, 9999999);
            var config     = new TerrainGenerator.WorldGenConfig(randomSeed, WorldType.Prim, worldSize, worldSize,
                                                                 new Vector2(tileSize, tileSize), worldSeed);

            _terrainGenerator = new TerrainGenerator();
            _terrainGenerator.InitWorldFromConfig(config);
            while (!_terrainGenerator.IsInitialized)
            {
                yield return(null);
            }

            _terrainGenerator.GenerateWorld();
            while (!_terrainGenerator.IsGenerated)
            {
                yield return(null);
            }

            // center the camera over the terrain
            Camera.main.transform.position = new Vector3(
                (worldSize - 1) * (tileSize * 0.5f),
                (worldSize - 1) * (tileSize * 2f),
                (worldSize - 1) * (tileSize * 0.5f)
                );

            // spawn terrain tiles
            for (int yTile = 0; yTile < worldSize; yTile++)
            {
                for (int xTile = 0; xTile < worldSize; xTile++)
                {
                    bool isRoadTile = _terrainGenerator.GetTile(xTile, yTile).IsOpen();
                    var  tileType   = GetTerrainType(_terrainGenerator, xTile, yTile);
                    var  worldPos   = GetTileWorldPosition(xTile, yTile);
                    var  terrain    = EntityFactory.CreateTerrain($"Terrain_{xTile}_{yTile}", worldPos, tileType, isRoadTile, tileSize, xTile, yTile);
                    _terrainEntities.Add(terrain);

                    if (!isRoadTile)
                    {
                        // randomly spawn obstacles
                        if (UnityEngine.Random.value > 0.75f)
                        {
                            var prop = EntityFactory.CreateDestructible($"Prop_Tree_{_propEntities.Count}", "PropTree", worldPos, GameConfig.DESTRUCTIBLE_HIT_POINTS);
                            _propEntities.Add(prop);
                        }
                    }
                }
            }

            // surround the world with invisible walls
            for (int yTile = -1; yTile <= worldSize; yTile++)
            {
                var wall1 = EntityFactory.CreateProp("Blocker", "PropInvisibleWall", GetTileWorldPosition(-1, yTile));
                _wallEntities.Add(wall1);

                var wall2 = EntityFactory.CreateProp("Blocker", "PropInvisibleWall", GetTileWorldPosition(worldSize, yTile));
                _wallEntities.Add(wall2);
            }

            for (int xTile = -1; xTile <= worldSize; xTile++)
            {
                var wall1 = EntityFactory.CreateProp("Blocker", "PropInvisibleWall", GetTileWorldPosition(xTile, -1));
                _wallEntities.Add(wall1);

                var wall2 = EntityFactory.CreateProp("Blocker", "PropInvisibleWall", GetTileWorldPosition(xTile, worldSize));
                _wallEntities.Add(wall2);
            }

            // place the player on a road
            int xPlayer = -1;
            int yPlayer = -1;

            for (int yTile = 0; yTile < worldSize; yTile++)
            {
                for (int xTile = 0; xTile < worldSize; xTile++)
                {
                    int index   = _terrainGenerator.GetLinearIndex(xTile, yTile);
                    var terrain = _terrainEntities[index].GetModule <PrefabModule <TerrainBehaviour> >(ModuleType.Prefab);
                    if (terrain.component.isOpen)
                    {
                        _playerEntity = EntityFactory.CreatePlayerTank("Player", GetTileWorldPosition(xTile, yTile), GameConfig.PLAYER_HIT_POINTS);
                        xPlayer       = xTile;
                        yPlayer       = yTile;
                        break;
                    }
                }

                if (_playerEntity != null)
                {
                    break;
                }
            }

            // iterate backwards over the terrain to place enemies preferentially far from the player
            _numEnemiesRemaining = 0;
            for (int yTile = worldSize - 1; yTile >= 0; yTile--)
            {
                for (int xTile = worldSize - 1; xTile >= 0; xTile--)
                {
                    int index   = _terrainGenerator.GetLinearIndex(xTile, yTile);
                    var terrain = _terrainEntities[index].GetModule <PrefabModule <TerrainBehaviour> >(ModuleType.Prefab);

                    if (terrain.component.isOpen &&
                        !(xTile == xPlayer && yTile == yPlayer) &&
                        _enemyEntities.Count < desiredEnemies &&
                        UnityEngine.Random.value > 0.8f)
                    {
                        var team = Team.Red;
                        if (numTeams == 2 && UnityEngine.Random.value > 0.5f)
                        {
                            team = Team.Blue;
                        }

                        var enemy = EntityFactory.CreateNpcTank($"Enemy_{_enemyEntities.Count}", GetTileWorldPosition(xTile, yTile), GameConfig.ENEMY_HIT_POINTS, team);
                        _enemyEntities.Add(enemy);

                        if (team != Team.Blue)
                        {
                            _numEnemiesRemaining++;
                        }

                        enemy.GetModule <HealthModule>(ModuleType.Health).OnHealthChanged += (e, h, delta) =>
                        {
                            if (e.GetModule <NpcAgentModule>(ModuleType.NpcAgent).Team == Team.Red && h.HitPoints <= 0)
                            {
                                _numEnemiesRemaining--;
                                if (_numEnemiesRemaining <= 0)
                                {
                                    GameUI.BroadcastMessage(UIMessage.Create(UIMessage.MessageType.LevelCleared));
                                }
                            }
                        };
                    }
                }
            }
        }