void lifePackDisappearTimer_Elapsed(object sender, ElapsedEventArgs e) { if (this.nextLifePackToDisappear != null) { Monitor.Enter(gameEng.DisappearLifePackList); Monitor.Enter(gameEng.AvailableLifePackList); LifePack lifePackNextInLine = null; int currentIndex = gameEng.DisappearLifePackList.IndexOf(this.nextLifePackToDisappear); if (gameEng.AvailableLifePackList.Contains(this.nextLifePackToDisappear)) { Console.WriteLine("Life Pack @ " + this.nextLifePackToDisappear.Position + " VANISHED"); gui.RemoveMapItem(this.nextLifePackToDisappear.Position.X, this.nextLifePackToDisappear.Position.Y); gameEng.AvailableLifePackList.Remove(this.nextLifePackToDisappear); } if (currentIndex < gameEng.DisappearLifePackList.Count - 2) { lifePackNextInLine = gameEng.DisappearLifePackList[currentIndex + 1]; int updateTimeSpan = lifePackNextInLine.DisappearTime - this.nextLifePackToDisappear.DisappearTime; if (updateTimeSpan > 0) this.lifePackDisappearTimer.Interval = updateTimeSpan; else this.lifePackDisappearTimer.Interval = 500; this.nextLifePackToDisappear = lifePackNextInLine; } else { int updateTimeSpan = gameEng.DisappearLifePackList[gameEng.DisappearLifePackList.Count - 1].DisappearTime - gameEng.DisappearLifePackList[gameEng.DisappearLifePackList.Count - 2].DisappearTime; if (updateTimeSpan != 0) this.lifePackDisappearTimer.Interval = updateTimeSpan; else this.lifePackDisappearTimer.Interval = 500; this.nextLifePackToDisappear = null; } Monitor.Exit(gameEng.AvailableLifePackList); Monitor.Exit(gameEng.DisappearLifePackList); } else { if (gameEng.AvailableLifePackList.Contains(gameEng.DisappearLifePackList[gameEng.DisappearLifePackList.Count - 1])) { Monitor.Enter(gameEng.DisappearLifePackList); Monitor.Enter(gameEng.AvailableLifePackList); Console.WriteLine("LifePack @ " + gameEng.DisappearLifePackList[gameEng.DisappearLifePackList.Count - 1].Position + " VANISHED"); gui.RemoveMapItem(gameEng.DisappearLifePackList[gameEng.DisappearLifePackList.Count - 1].Position.X, gameEng.DisappearLifePackList[gameEng.DisappearLifePackList.Count - 1].Position.Y); gameEng.AvailableLifePackList.Remove(this.nextLifePackToDisappear); Monitor.Exit(gameEng.AvailableLifePackList); Monitor.Exit(gameEng.DisappearLifePackList); //No need to call game finish here since the lifepacks have no impact on the endgame conditions } } }
private MapItem CreateMapItem(Point p, Boolean isCoinPile) { RandomGen r = new RandomGen(); MapItem tempMapItem; if (isCoinPile) { tempMapItem = new CoinPile(p.X, p.Y); ((CoinPile)tempMapItem).Price = r.randomD(700, 2000); } else { tempMapItem = new LifePack(p.X, p.Y); } tempMapItem.LifeTime = r.randomD(Convert.ToInt32((Constant.MAP_SIZE / 4) * Constant.PLAYER_DELAY), Constant.LIFE_TIME / 10); tempMapItem.AppearTime = r.randomD(0, (Constant.LIFE_TIME - tempMapItem.LifeTime)); tempMapItem.DisappearTime = tempMapItem.AppearTime + tempMapItem.LifeTime; return tempMapItem; }
/// <summary> /// Initializing the timers /// </summary> private void InitializeTimers() { this.updateCoinPileTimer = new System.Timers.Timer(); this.playerUpdateTimer = new System.Timers.Timer(); this.lifeTimer = new System.Timers.Timer(); this.CoinPileDisappearTimer = new System.Timers.Timer(); this.updateLifePackTimer = new System.Timers.Timer(); this.lifePackDisappearTimer = new System.Timers.Timer(); this.plunderCoinPileDisappearTimer = new System.Timers.Timer(); this.updateCoinPileTimer.Elapsed += new ElapsedEventHandler(updateCoinPileTimer_Elapsed); this.playerUpdateTimer.Elapsed += new ElapsedEventHandler(playerUpdateTimer_Elapsed); this.lifeTimer.Elapsed += new ElapsedEventHandler(lifeTimer_Elapsed); this.CoinPileDisappearTimer.Elapsed += new ElapsedEventHandler(CoinPileDisappearTimer_Elapsed); this.updateLifePackTimer.Elapsed += new ElapsedEventHandler(updateLifePackTimer_Elapsed); this.lifePackDisappearTimer.Elapsed += new ElapsedEventHandler(lifePackDisappearTimer_Elapsed); this.plunderCoinPileDisappearTimer.Elapsed += new ElapsedEventHandler(plunderCoinPileDisappearTimer_Elapsed); this.playerUpdateTimer.Enabled = true; this.lifeTimer.Enabled = true; this.updateCoinPileTimer.Enabled = true; this.CoinPileDisappearTimer.Enabled = true; this.updateLifePackTimer.Enabled = true; this.lifePackDisappearTimer.Enabled = true; this.plunderCoinPileDisappearTimer.Enabled = true; this.playerUpdateTimer.Interval = Constant.UPDATE_PERIOD; this.lifeTimer.Interval = Constant.LIFE_TIME; this.plunderCoinPileDisappearTimer.Interval = 1; if (gameEng.CoinPileList.Count > 0) { gameEng.NextCoinPileSend = gameEng.CoinPileList[gameEng.CoinPilesToDisclose - 1].AppearTime; this.updateCoinPileTimer.Interval = gameEng.NextCoinPileSend; this.CoinPileDisappearTimer.Interval = gameEng.DisappearCoinPileList[0].DisappearTime; this.nextCoinPileToDisappear = gameEng.DisappearCoinPileList[0]; } else { this.CoinPileDisappearTimer.Interval = Constant.LIFE_TIME; this.nextCoinPileToDisappear = new CoinPile(-1, -1); this.nextCoinPileToDisappear.AppearTime = Constant.LIFE_TIME; this.nextCoinPileToDisappear.DisappearTime = Constant.LIFE_TIME; } if (gameEng.LifePackList.Count > 0) { gameEng.NextLifePackSend = gameEng.LifePackList[gameEng.LifePacksToDisclose - 1].AppearTime; this.updateLifePackTimer.Interval = gameEng.NextLifePackSend; this.lifePackDisappearTimer.Interval = gameEng.DisappearLifePackList[0].DisappearTime; this.nextLifePackToDisappear = gameEng.DisappearLifePackList[0]; } else { this.lifePackDisappearTimer.Interval = Constant.LIFE_TIME; this.nextLifePackToDisappear = new LifePack(-1, -1); this.nextLifePackToDisappear.AppearTime = Constant.LIFE_TIME; this.nextLifePackToDisappear.DisappearTime = Constant.LIFE_TIME; } this.updateCoinPileTimer.Stop(); this.CoinPileDisappearTimer.Stop(); this.updateLifePackTimer.Stop(); this.lifePackDisappearTimer.Stop(); this.playerUpdateTimer.Stop(); this.lifeTimer.Stop(); }