예제 #1
0
    /// <summary>
    /// Used for initialisation, called before the Start() function.
    /// </summary>
    private void Awake()
    {
        // Intialise variables
        _game    = GameObject.Find("Game").GetComponent <Game>();
        _map     = _game.gameObject.GetComponent <Map2D>();
        Boosters = new HashSet <HappinessBooster>();

        _upgradeTimer = new CountdownTimer {
            Seconds = 5f
        };
        _upgradeTimer.Begin();

        Data = new BuildingData();

        switch (TileType)
        {
        case TileType.Residential:
            OccupantMinMax = new Vector2(0, 100);
            break;

        case TileType.Commercial:
            OccupantMinMax = new Vector2(0, 225);
            break;

        case TileType.Office:
            OccupantMinMax = new Vector2(0, 500);
            break;
        }
    }
예제 #2
0
    /// <summary>
    /// Updates this instance.
    /// </summary>
    private void Update()
    {
        // Check gamestate
        if (_game.GameState == GameState.Paused)
        {
            return;
        }

        // Check for keyboard input
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            // Disable destroy action
            if (DestroyMode)
            {
                ToggleDestroyMode();
            }
        }

        // Spawn traffic
        if (_timer != null)
        {
            if (_timer.IsDone())
            {
                SpawnTraffic();

                // Reset timer
                _timer.ResetClock();
                _timer.Begin();
            }
            else
            {
                _timer.Update();
            }
        }
    }
예제 #3
0
 public void ResetTimer()
 {
     // Check level
     _countdownTimer.ResetClock();
     _time = Level > 1f ? 60f : 99f;
     _countdownTimer.Seconds = _time;
     _countdownTimer.Begin();
 }
예제 #4
0
 /// <summary>
 /// Initialises this instance.
 /// </summary>
 private void Start()
 {
     // Initialisation
     _timer = new CountdownTimer {
         Seconds = Duration
     };
     _timer.Begin();
 }
예제 #5
0
    /// <summary>
    /// Finds a path between two roads.
    /// </summary>
    /// <param name="start">The starting point.</param>
    /// <param name="goal">The destination.</param>
    public void FindPath(Node start, Node goal)
    {
        // Check if the map needs updating
        Node[] nodes      = NodeGroup.GetComponentsInChildren <Node>();
        int    childCount = nodes.Length;

        if (childCount != noOfTiles)
        {
            UpdateMap();
            noOfTiles = childCount;
        }

        // Clear previous colour path
        if (ShowPath)
        {
            foreach (Node node in nodes)
            {
                SpriteRenderer sp = node.gameObject.GetComponent <SpriteRenderer>();
                sp.color = Color.white;
            }
        }

        _grid        = gameObject.GetComponent <Grid>();
        _grid.Layout = _map;
        _pathfinder  = new Pathfinder(_grid);
        _pathfinder.Start(start, goal);

        // Prevent infinite loop
        CountdownTimer timeoutTimer = new CountdownTimer {
            Seconds = 1f
        };

        timeoutTimer.Begin();

        while (!_pathfinder.PathFound)
        {
            _pathfinder.Step();

            if (timeoutTimer.IsDone())
            {
                return;
            }

            timeoutTimer.Update();
        }

        // Debug.Log("Search done, path length: " + _pathfinder.Path.Count + ", iterations: " + _pathfinder.Iterations);

        // Show path
        if (ShowPath)
        {
            foreach (Node node in _pathfinder.Path)
            {
                SpriteRenderer sp = GetTile(node.Label).gameObject.GetComponent <SpriteRenderer>();
                sp.color = Color.red;
            }
        }
    }
예제 #6
0
        public IEnumerator TimerComplete()
        {
            UnityEventListenerMock timerStartedMock      = new UnityEventListenerMock();
            UnityEventListenerMock timerCancelledMock    = new UnityEventListenerMock();
            UnityEventListenerMock timerCompleteMock     = new UnityEventListenerMock();
            UnityEventListenerMock timerStillRunningMock = new UnityEventListenerMock();
            UnityEventListenerMock timerNotRunningMock   = new UnityEventListenerMock();

            subject.Started.AddListener(timerStartedMock.Listen);
            subject.Cancelled.AddListener(timerCancelledMock.Listen);
            subject.Completed.AddListener(timerCompleteMock.Listen);
            subject.StillRunning.AddListener(timerStillRunningMock.Listen);
            subject.NotRunning.AddListener(timerNotRunningMock.Listen);

            subject.StartTime = 0.1f;

            Assert.IsFalse(timerStartedMock.Received);
            Assert.IsFalse(timerCancelledMock.Received);
            Assert.IsFalse(timerCompleteMock.Received);
            Assert.IsFalse(timerStillRunningMock.Received);
            Assert.IsFalse(timerNotRunningMock.Received);

            subject.Begin();

            Assert.IsTrue(timerStartedMock.Received);
            Assert.IsFalse(timerCancelledMock.Received);
            Assert.IsFalse(timerCompleteMock.Received);

            subject.EmitStatus();

            Assert.IsTrue(timerStillRunningMock.Received);
            Assert.IsFalse(timerNotRunningMock.Received);

            yield return(new WaitForSeconds(0.1f));

            Assert.IsFalse(timerCancelledMock.Received);
            Assert.IsTrue(timerCompleteMock.Received);

            timerStillRunningMock.Reset();
            timerNotRunningMock.Reset();

            subject.EmitStatus();

            Assert.IsFalse(timerStillRunningMock.Received);
            Assert.IsTrue(timerNotRunningMock.Received);
        }
예제 #7
0
    public void ResetGame()
    {
        // Set variables
        Level = 1;
        Score = 0;

        // Set timer
        _countdownTimer = new CountdownTimer {
            Seconds = _time
        };
        _countdownTimer.Begin();
    }
예제 #8
0
    /// <summary>
    /// Updates this instance.
    /// </summary>
    private void Update()
    {
        // Check for pause
        if (GameState == GameState.Paused)
        {
            return;
        }

        // Collect taxes
        if (_gameTimer != null)
        {
            if (_gameTimer.IsDone())
            {
                // Collect tax
                Money += Mathf.RoundToInt(_map.Buildings.Sum(building => building.CollectTax()));

                // Calculate upkeep costs
                float upkeep =
                    _map.Decorations.Select(tile => tile.gameObject.GetComponent <PurchasableTile>())
                    .Where(purchasable => purchasable != null)
                    .Sum(purchasable => purchasable.Upkeep);
                upkeep +=
                    _map.Roads.Select(tile => tile.gameObject.GetComponent <PurchasableTile>())
                    .Where(purchasable => purchasable != null)
                    .Sum(purchasable => purchasable.Upkeep);

                // Subtract upkeep costs
                Money -= Mathf.RoundToInt(upkeep);

                _gameTimer.ResetClock();
                _gameTimer.Begin();
            }
            else
            {
                _gameTimer.Update();
            }
        }

        // Check for gameover
        if (Money < -1000)
        {
            // Pause game
            SetPause(true);
            // Display game over screen
            GameOverScreen.SetActive(true);
        }
    }
예제 #9
0
    /// <summary>
    /// Initialises variables, called before Start().
    /// </summary>
    private void Awake()
    {
        // Initialise tax timer
        _gameTimer = new CountdownTimer {
            Seconds = 10f
        };
        _gameTimer.Begin();

        // Initialise map
        _map = gameObject.GetComponent <Map2D>();

        // Get audio sources
        _musicSource = transform.FindChild("Music").GetComponent <AudioSource>();
        _sfxSource   = transform.FindChild("SFX").GetComponent <AudioSource>();

        // Initialise the game
        InitGame();
    }
예제 #10
0
        private void Countdown()
        {
            TextValue.Opacity = 0;
            int x = Int32.Parse(CountdownText.Text) - 1;

            CountdownText.Text = x.ToString();
            if (x > 0)
            {
                CountdownTimer.Begin();
            }
            else
            {
                StartTimestamp = DateTime.Now;
                dt.Start();
                IsGameRunning = true;
                StartGame();
            }
        }
예제 #11
0
    /// <summary>
    /// Initialises this instance, called before Start().
    /// </summary>
    private void Awake()
    {
        // Set TimeScale | Bug Fix
        Time.timeScale = 1f;

        // Variable initialisation
        GroundTiles = new Tile[XSize, YSize];
        Buildings   = new List <Building>();
        Roads       = new List <Tile>();
        Decorations = new List <Tile>();

        _roadPathFinder = gameObject.GetComponent <RoadPathFinder>();
        _timer          = new CountdownTimer {
            Seconds = 5f
        };
        _timer.Begin();

        _game = GameObject.Find("Game").GetComponent <Game>();
    }
예제 #12
0
    /// <summary>
    /// Called once per frame, used to process building upgrades.
    /// </summary>
    private void Update()
    {
        // Check timer and stats to see if this building needs upgrading, don't bother checking if the building is already at it's max level
        if (_upgradeTimer.IsDone() && Level != (int)LevelMinMax.y)
        {
            // Check if building is eligible for an upgrade
            if (Level == 1 && Happiness >= 50 || Level == 2 && Happiness >= 80)
            {
                // Upgrade
                _map.SpawnBuilding(TileType, Level + 1, transform.position, transform.rotation);
                // Destroy current object
                _map.Buildings.Remove(this);
                Destroy(gameObject);
            }

            // Reset timer
            _upgradeTimer.ResetClock();
            _upgradeTimer.Begin();
        }
        else
        {
            _upgradeTimer.Update();
        }
    }