public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // Update based on phase
            if(phase == Phase.Initialize) {
                syncing = false;
                start = gameTime.TotalGameTime;
                phase = Phase.Sync;
            } else if(phase == Phase.Sync) {
                // Display banner before syncing
                if(banner == null) {
                    banner = new TextBanner(this, "WATCH THE STARS!");
                }

                if((gameTime.TotalGameTime - start) > BANNER_TIME) {
                    // Sync all players with server before beginning
                    if(!syncing) {
                        Random rand = new Random();
                        List<int> commands = new List<int>();
                        for(int i = 0; i < 20; i++) {
                            commands.Add(rand.Next(0, 4));
                        }
                        if(singlePlayer) {
                            RemoveActor(commandList);
                            for(int i = 0; i < commandNum; i++) {
                                commandList.Add(new Arrow(this, (Direction)(int)commands[i], textureList["twilight"], new Vector2(200 + 250 * (i % 7), 200 + 250 * (i / 7)), 1.4f));
                                commandList[commandList.Count - 1].Visible = false;
                            }
                            start = gameTime.TotalGameTime;
                            phase = Phase.Commands;
                        } else {
                            // Send ready packet to sync before starting
                            Sync((JArray data, double random) => {
                                RemoveActor(commandList);
                                //foreach(JToken commandArray in data) {
                                JToken commandArray = data[0]; // Temporary hack
                                JArray a = JArray.Parse((string)commandArray);
                                for(int i = 0; i < commandNum; i++) {
                                    commandList.Add(new Arrow(this, (Direction)(int)a[i], textureList["twilight"], new Vector2(200 + 250 * (i % 7), 200 + 250 * (i / 7)), 1.4f));
                                    commandList[commandList.Count - 1].Visible = false;

                                }
                                //}
                                start = gameTime.TotalGameTime;
                                phase = Phase.Commands;
                            }, JsonConvert.SerializeObject(commands));
                        }
                        syncing = true;
                    }
                }
            } else if(phase == Phase.Commands) {
                syncing = false;
                banner = null;
                TimeSpan newTime = COMMAND_TIME + (new TimeSpan(0, 0, (int)COMMAND_TIME_INCR.TotalSeconds * rounds));
                timer = newTime - gameTime.TotalGameTime + start;
                timerBar = timer.TotalMilliseconds / newTime.TotalMilliseconds;
                if(timer.TotalMilliseconds <= 0) {
                    foreach(Arrow a in commandList) {
                        a.State = ArrowStates.FadeOut;
                    }
                    start = gameTime.TotalGameTime;
                    phase = Phase.Input;
                    return;
                } else {
                    // Draw commands every 500ms
                    for(int i = 0; i < (gameTime.TotalGameTime - start).TotalMilliseconds / 500; i++) {
                        if(i < commandList.Count) {
                            commandList[i].Visible = true;
                        }
                    }
                }
            } else if(phase == Phase.Input) {
                TimeSpan newTime = INPUT_TIME + (new TimeSpan(0, 0, (int)INPUT_INCR.TotalSeconds * rounds));
                timer = newTime - gameTime.TotalGameTime + start;
                timerBar = timer.TotalMilliseconds / newTime.TotalMilliseconds;
                if(banner == null && timer > (newTime - BANNER_TIME)) {
                    banner = new TextBanner(this, "SWIPE!");
                } else if(timer <= (newTime - BANNER_TIME)) {
                    banner = null;
                }
                if(timer.TotalMilliseconds <= 0) {
                    start = gameTime.TotalGameTime;
                    phase = Phase.Timeup;
                    timerBar = 0;
                    return;
                }
            } else if(phase == Phase.Timeup) {
                if(banner == null) {
                    banner = new TextBanner(this, "TIME'S UP!");
                }

                // Switch to dancing phase when finished waiting for slow packets
                if((gameTime.TotalGameTime - start) > BANNER_TIME) {
                    currentMove = 0;
                    banner = null;
                    phase = Phase.Dancing;
                    return;
                }
            } else if(phase == Phase.Dancing) {
                // Check for LMS
                if(currentRank == 1 && !singlePlayer) {
                    foreach(Player p in players.Values) {
                        if(p.health > 0)
                            p.rank = 1;
                    }
                    phase = Phase.GameOver;
                    return;
                }

                // Animate each character dance
                if(currentMove == 0 || (gameTime.TotalGameTime - start) > MOVE_TIME) {
                    if(currentMove >= commandList.Count()) {
                        // Switch to show winner when finished
                        foreach(Player p in new List<Player>(players.Values)) {
                            p.State = PlayerDanceStates.Standing;
                        }

                        // Clear input list
                        foreach(Player p in new List<Player>(players.Values)) {
                            p.input.Clear();
                        }
                        RemoveActor(inputArrows);

                        phase = Phase.Close;
                        return;
                    }
                    List<Player> tieCheck = new List<Player>();
                    foreach(string s in new List<string>(players.Keys)) {
                        Player p = players[s];
                        bool error = false;
                        if(p.input.Count > currentMove) {
                            if(p.input[currentMove] != commandList[currentMove].dir) {
                                error = true;
                                if(s == playerName) {
                                    soundList["dong"].Play();
                                    inputArrows.ElementAt(currentMove).State = ArrowStates.Red;
                                }
                            } else if(s == playerName) {
                                soundList["ding"].Play();
                                score++;
                                inputArrows.ElementAt(currentMove).State = ArrowStates.Green;
                            }
                            p.State = PlayerDanceStates.DanceLeft + (int)p.input[currentMove];
                        } else {
                            error = true;
                            p.State = PlayerDanceStates.Standing;
                        }
                        if(error && p.health > 0) {
                            soundList["puff"].Play();
                            p.health--;
                            new Smoke(this, p.Coord + new Vector2((100 * (p.health % (p.maxHealth / 2 + 1)) + (p.health / (p.maxHealth / 2 + 1)) * 50), (p.health / (p.maxHealth / 2 + 1)) * 65));
                            if(p.health <= 0) {
                                p.rank = currentRank;
                                foreach(Player tiedPlayer in tieCheck) {
                                    tiedPlayer.rank--;
                                }
                                tieCheck.Add(p);
                                if(--currentRank <= 0) {
                                    phase = Phase.GameOver;
                                }
                            }

                        }

                    }
                    currentMove++;
                    start = gameTime.TotalGameTime; // Reset time for next move
                }
            } else if(phase == Phase.Close) {
                // Exit game when max number of rounds have been reached or a winner has been reached
                start = gameTime.TotalGameTime;
                if(++rounds >= MAX_ROUNDS) {
                    if(singlePlayer) {
                        rounds--;
                        phase = Phase.Sync;
                    } else {
                        List<Player> sortedPlayers = new List<Player>(players.Values);
                        sortedPlayers.Sort();
                        int lastHealth = 0, tieCount = 0;
                        for(int i = 0; i < sortedPlayers.Count; i++) {
                            if(sortedPlayers[i].health > 0) {
                                if(lastHealth == sortedPlayers[i].health) {
                                    tieCount++;
                                } else {
                                    tieCount = 0;
                                }
                                sortedPlayers[i].rank = i + 1 - tieCount;
                                lastHealth = sortedPlayers[i].health;
                            }
                        }
                        phase = Phase.GameOver;
                    }
                } else {
                    commandNum += COMMAND_INCR;
                    phase = Phase.Sync;
                }
            } else if(phase == Phase.GameOver) {
                if(banner == null) {
                    string bannerText;
                    if(singlePlayer)
                        bannerText = "SCORE: " + score;
                    else
                        bannerText = (players[playerName].rank == 1) ? "YOU WIN!" : "GAME OVER";
                    banner = new TextBanner(this, bannerText);
                }
                if((gameTime.TotalGameTime - start) > BANNER_TIME) {
                    if(singlePlayer) {
                        scoreReceived = true;
                    } else {
                        // Sync scores
                        JObject packet = new JObject(
                            new JProperty("action", "group"),
                            new JProperty("type", "report_score"),
                            new JProperty("score", new JObject(
                                from username in new List<string>(players.Keys)
                                select new JProperty(username, players[username].rank)
                            ))
                        );
                        conn.SendMessage(packet.ToString());
                    }
                    phase = Phase.Sleep;
                }
            } else if(phase == Phase.Sleep) {
                // Do nothing
            }
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if(phase == Phase.Sync) {
                if(!singlePlayer) {
                    // sync with all users before starting
                    Sync((JArray data, double rand) => {
                        foreach(string name in playerData.Keys) {
                            bool found = false;
                            foreach(JToken tok in data) {
                                if((string)tok == name)
                                    found = true;
                            }
                            if(!found)
                                RemovePlayer(name);
                        }
                        start = gameTime.TotalGameTime;
                        phase = Phase.Begin;
                    }, playerName);
                    phase = Phase.Limbo;
                } else {
                    phase = Phase.Begin;
                    start = gameTime.TotalGameTime;
                }
            } else if(phase == Phase.Limbo) {
                // do nothing while we wait for sync
            } else if(phase == Phase.Begin) {
                if(!exploded) {
                    // Randomly bring in pigs
                    TimeSpan trueDelay = (singlePlayer) ? new TimeSpan(PIG_DELAY.Ticks / 5) : PIG_DELAY;
                    if((gameTime.TotalGameTime - timeSincePig) > trueDelay && rand.Next(1000) < (gameTime.TotalGameTime - start).Seconds) {
                        if(singlePlayer) {
                            // lower rate of pigs upgrade
                            if(rand.Next(10) >= upgrades[(int)Upgrade.PigFreq]) {
                                if(rand.Next(4) == 0) {
                                    for(int i = 0; i < (gameTime.TotalGameTime - start).TotalMinutes; i++)
                                        AddFlyingPig(rand.Next(4));
                                    soundList["landing"].Play();
                                    avatars[playerName].State = true;
                                } else {
                                    mainPigs.Add(new Pig(this, PigStates.Entering, rand));
                                }
                            }
                        } else {
                            mainPigs.Add(new Pig(this, PigStates.Entering, rand));
                        }
                        timeSincePig = gameTime.TotalGameTime;
                    }

                    // Check if any sections are filled
                    for(int i = 0; i < pennedPigs.Count; i++) {
                        if(pennedPigs[i].Count >= MAX_PIG_COUNT) {
                            foreach(Pig p in pennedPigs[i]) {
                                p.State = PigStates.Flying;
                            }
                            soundList["launching"].Play();
                            pennedPigs[i].Clear();

                            if(!singlePlayer) {
                                // Send packet to send pigs to other players
                                JObject packet = new JObject(
                                    new JProperty("action", "game"),
                                    new JProperty("name", "jetpack_jamboree"),
                                    new JProperty("data", new JObject(
                                        new JProperty("action", "fly"),
                                        new JProperty("color", i)
                                    ))
                                );
                                conn.SendMessage(packet.ToString());
                            }
                        }
                    }
                    avatars[playerName].count = mainPigs.Count;
                } else {
                    if(banner == null)
                        banner = (singlePlayer) ? new TextBanner(this, "Score: " + score) : new TextBanner(this, "WAITING FOR OTHER PLAYERS");
                    for(int i = 0; i < 4; i++) {
                        foreach(Pig p in pennedPigs[i]) {
                            p.State = PigStates.Standing;
                            p.Velocity = Vector2.Zero;
                        }
                    }
                    foreach(Pig p in mainPigs) {
                        p.State = PigStates.Standing;
                        p.Velocity = Vector2.Zero;
                    }
                    if(singlePlayer)
                        phase = Phase.GameOver;
                }
                // Keep track of when the game switches to game over
                bannerStart = gameTime.TotalGameTime;
            } else if(phase == Phase.GameOver) {
                if(banner == null) {
                    banner = new TextBanner(this, "YOU WIN!");
                } else if(!singlePlayer) {
                    banner = new TextBanner(this, "GAME OVER");
                }
                if(gameTime.TotalGameTime - bannerStart > BANNER_TIME) {
                    if(singlePlayer) {
                        scoreReceived = true;
                        phase = Phase.Limbo;
                    } else {
                        lock(deadUsers) {
                            // Sync scores
                            JObject packet = new JObject(
                                new JProperty("action", "group"),
                                new JProperty("type", "report_score"),
                                new JProperty("score", new JObject(
                                    from username in deadUsers
                                    select new JProperty(username, deadUsers.IndexOf(username) + 1)
                                ))
                            );
                            conn.SendMessage(packet.ToString());
                            phase = Phase.Limbo;
                        }
                    }
                }
            }
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if(phase == Phase.Begin) {
                if(singlePlayer) {
                    ResetCrates();
                    phase = Phase.Main;
                } else if(!synced) {
                    Sync((JArray data, double random) => {
                        ResetCrates();
                        phase = Phase.Main;
                    });
                    synced = true;
                }
            } else if(phase == Phase.Main) {
                gameTimer -= gameTime.ElapsedGameTime;
                if(gameTimer.TotalMilliseconds < 0) {
                    bannerStart = gameTime.TotalGameTime;
                    gameTimer = new TimeSpan(0, 0, -1);
                    phase = Phase.End;
                    return;
                }
                if(crates.Count == 0) {
                    // bring in more crates
                    ResetCrates();
                    if(singlePlayer) {
                        gameTimer += new TimeSpan(0, 0, 0, 0, upgrades[1] * 500);
                    }
                    score += 10;
                }
            } else if(phase == Phase.End) {
                if(singlePlayer) {
                    if(banner == null) {
                        banner = new TextBanner(this, "Score: " + score);
                    }
                }
                if(gameTime.TotalGameTime - bannerStart > BANNER_TIME) {
                    if(singlePlayer) {
                        scoreReceived = true;
                        phase = Phase.Limbo;
                    }
                }
            } else if(phase == Phase.Limbo) {

            }
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            // randomly add shadows
            if(rand.Next(0, 1000) < 1) {
                if(gameTime.TotalGameTime - lastShadow > SHADOW_INTERVAL) {
                    new Shadow(this, (rand.Next(0, 2) == 0), rand.Next(450, 800), rand.Next(0, 2));
                    lastShadow = gameTime.TotalGameTime;
                }
            }
            if(phase == Phase.Begin) {
                if(round > roundData.Length) {
                    // End game when rounds are over
                    baseline = gameTime.TotalGameTime;

                    // Check if player has won to display appropriate banner
                    string text = "YOU WIN!";
                    foreach(int s in scores.Values) {
                        if(s > scores[playerName]) {
                            text = "GAME OVER";
                        }
                    }
                    if(singlePlayer)
                        text = "Score: " + score;
                    banner = new TextBanner(this, text);
                    phase = Phase.End;
                    return;
                }
                phase = Phase.Limbo;
                phaseFish(roundData[round - 1]);
            }
            if(phase == Phase.Limbo) {
                // Do nothing and wait for the sync to finish
            } else if(phase == Phase.Base) {
                baseline = gameTime.TotalGameTime;
                if(round == roundData.Length) {
                    banner = new TextBanner(this, "FINAL ROUND!");
                } else {
                    banner = new TextBanner(this, "Round " + round);
                }
                lastFish = baseline + BANNER_TIME + new TimeSpan(0, 0, 1);
                curFish = 0;
                phase = Phase.Banner;
            } else if(phase == Phase.Banner) {
                if(gameTime.TotalGameTime > BANNER_TIME + baseline) {
                    banner = null;
                    phase = Phase.Fishing;
                }
            } else if(phase == Phase.Fishing) {
                // remove fish that are out of bounds
                List<Fish> toRem = new List<Fish>();
                foreach(Fish f in fishies) {
                    if(f.Coord.X < -301 || f.Coord.X > 2201) { // 1px more than start
                        toRem.Add(f);
                    }
                }
                foreach(Fish f in toRem) {
                    fishies.Remove(f);
                    RemoveActor(f);
                }

                // swap backup hooks in
                List<string> backupRemoval = new List<string>();
                lock(backupHooked) {
                    lock(hooked) {
                        foreach(string s in backupHooked.Keys) {
                            if(!hooked.ContainsKey(s)) {
                                hooked[s] = backupHooked[s];
                                backupRemoval.Add(s);
                            }
                        }
                        foreach(String s in backupRemoval) {
                            backupHooked.Remove(s);
                        }
                    }
                }

                // add new fish
                if(curFish < fishData.Count) {
                    TimeSpan newFishTime = lastFish + new TimeSpan(0, 0, 0, 0, (int)(fishData[curFish].interval * 1000));
                    // time to add a fish
                    if(gameTime.TotalGameTime > newFishTime) {
                        fishies.Add(new Fish(this, (FishTypes)fishData[curFish].type, fishData[curFish].depth, fishData[curFish].dir, gameTime.TotalGameTime - newFishTime));
                        lastFish = newFishTime;
                        curFish++;
                    }
                }

                // check if round is done
                if(curFish >= fishData.Count) {
                    if(fishies.Count == 0) {
                        lock(hooked) {
                            hooked.Clear();
                        }
                        round++;
                        // remove all looped sounds before continuing
                        Helpers.GetSoundLibrary(this).StopAllSounds();
                        phase = Phase.Begin;
                    }
                }
            } else if(phase == Phase.End) {
                if(gameTime.TotalGameTime - baseline > BANNER_TIME) {
                    // send scores
                    if(!singlePlayer) {
                        Dictionary<string, int> ranks = new Dictionary<string, int>();
                        foreach(string name in playerData.Keys) {
                            int rank = 1;
                            foreach(int s in scores.Values) {
                                if(s > scores[name])
                                    rank++;
                            }
                            ranks.Add(name, rank);
                        }
                        JObject packet = new JObject(
                            new JProperty("action", "group"),
                            new JProperty("type", "report_score"),
                            new JProperty("score", new JObject(
                                from username in new List<string>(ranks.Keys)
                                select new JProperty(username, ranks[username])
                            ))
                        );
                        conn.SendMessage(packet.ToString());
                    } else {
                        scoreReceived = true;
                    }
                    phase = Phase.Sleep;
                }
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // Check for hooked fish in order
            List<Hook> temp = new List<Hook>();
            lock(hooked) {
                foreach(Hook hook in hooked.Values) {
                    int index = 0;
                    while(index < temp.Count && temp[index].start < hook.start) {
                        index++;
                    }
                    temp.Insert(index, hook);
                }
                foreach(Hook hook in temp) {
                    hook.checkHooked(gameTime);
                }
            }
        }
Ejemplo n.º 5
0
 public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
 {
     if(phase == Phase.Begin) {
         if(singlePlayer) {
             gameStart = gameTime.TotalGameTime;
             phase = Phase.Main;
         } else if(!synced) {
             Sync((JArray data, double random) => {
                 gameStart = gameTime.TotalGameTime;
                 phase = Phase.Main;
             });
             synced = true;
         }
     } else if(phase == Phase.Main) {
         // randomly drop paint on the canvas
         if(singlePlayer) {
             if(rand.Next(0, 1000) < 20 - 3 * upgrades[(int)Upgrade.EnemyFreq]) {
                 Splatter splatter = new Splatter(this, players[""], rand);
                 splatter.Scale = (float)rand.NextDouble() + 1;
                 players[""].StartThrow(splatter);
                 players[""].ThrowPaint(gameTime.TotalGameTime - gameStart, new Vector2(rand.Next(BOUNDS.Left, BOUNDS.Right), rand.Next(BOUNDS.Top, BOUNDS.Bottom)));
             }
         }
         if(gameTime.TotalGameTime - gameStart > PAINT_TIME) {
             Dictionary<Color, float> areas = CalculatePaint();
             foreach(Avatar a in players.Values) {
                 a.score = areas[a.color] / (BOUNDS.Width * BOUNDS.Height);
             }
             gameEnd = gameTime.TotalGameTime;
             banner = new TextBanner(this, "TIME'S UP");
             foreach(Avatar a in players.Values) {
                 if(a.currentPaint != null) {
                     RemoveActor(a.currentPaint);
                     splatters.Remove(a.currentPaint);
                     a.currentPaint = null;
                     a.State = AvatarStates.Standing;
                 }
             }
             if(crosshair != null) {
                 RemoveActor(crosshair);
                 crosshair = null;
             }
             if(singlePlayer) {
                 score = (int)(players[playerName].score * 10000);
             }
             phase = Phase.Calculation;
         }
     } else if(phase == Phase.Calculation) {
         if(gameTime.TotalGameTime - gameEnd > BANNER_TIME + BANNER_TIME + SCORE_TIME) {
             if(singlePlayer) {
                 phase = Phase.Limbo;
             } else {
                 // Sync scores
                 JObject packet = new JObject(
                     new JProperty("action", "group"),
                     new JProperty("type", "report_score"),
                     new JProperty("score", new JObject(
                         from username in players.Keys
                         select new JProperty(username, (int)(players[username].score * -10000))
                     ))
                 );
                 conn.SendMessage(packet.ToString());
                 phase = Phase.Limbo;
             }
         } else if(gameTime.TotalGameTime - gameEnd > BANNER_TIME + SCORE_TIME) {
             banner = singlePlayer ?
                 banner ?? new TextBanner(this, "Score: " + score) :
                 banner ?? new TextBanner(this, "GAME OVER");
         } else if(gameTime.TotalGameTime - gameEnd > BANNER_TIME) {
             banner = null;
         }
     } else if(phase == Phase.Limbo) {
         // Do nothing
     }
     base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
 }