public Head(Neat nnw) : this() { //if (bots < 10) KeyCode = (Keys) new KeysConverter().ConvertFromString("D" + bots); KeyCode = (Keys) new KeysConverter().ConvertFromString("F" + (bots + 1)); tail = new Tail(KeyCode); if (bots < 10) { DisplayKey = "B" + bots; } else { DisplayKey = "B" + (char)(bots + 55); } color_creation = color = Color.FromArgb(255, 0, 255, 255); window.writeln(DisplayKey); this.key = new IKey(KeyCode, DisplayKey, color); ActiveKeys.Add(KeyCode); nnw.Key = KeyCode; nnw.Fire += Action; nnw.KeyUp += key.Release; this.bot = true; bots++; }
public void Stage2() { text = "Right before hitting an obstacle,\ndash to avoid it\n "; Head player = HEADS[key_player]; player.act = Activities.DEFAULT; player.pos = new IPoint(W * .6d, W * .19d); player.v = speed * IVector.Left; lock (Orb.OrbLock) new Orb(true).pos = new IPoint(W * 7 / 8, W * .19d); // introduce the dash with a black wall if (!HEADS.Keys.Contains(Keys.F13)) { bot = new Head(Keys.F13); bot.DisplayKey = ""; bot.color = Color.Red; HEADS.Add(Keys.F13, bot); lock (ActiveLock) ActiveKeys.Add(Keys.F13); bot1 = new Head(Keys.F14); bot1.DisplayKey = ""; bot1.color = Color.Red; HEADS.Add(Keys.F14, bot1); lock (ActiveLock) ActiveKeys.Add(Keys.F14); } bot.act = Activities.DEFAULT; bot.pos = new IPoint(W * 3 / 4, W / 4); bot.v = IVector.Down; lock (Orb.OrbLock) for (int i = 1; i < 12; i++) { new Orb(true).pos = new IPoint(W * 3 / 4, W / 2 * i / 13); } bot1.act = Activities.DEFAULT; bot1.pos = new IPoint(W * 1 / 4, W / 4); bot1.v = IVector.Up; lock (Orb.OrbLock) for (int i = 1; i < 12; i++) { new Orb(true).pos = new IPoint(W * 1 / 4, W / 2 * i / 13); } }
private void window_keydown(object sender, KeyEventArgs e) { if (keydown.Contains(e.KeyCode) || e.KeyCode == Keys.NumLock || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu) { return; } else { keydown.Add(e.KeyCode); } Keys ekey = e.KeyCode; if (e.KeyCode == Keys.Delete) { ekey = Keys.Decimal; } else if (e.KeyCode == Keys.Insert) { ekey = Keys.NumPad0; } else if (e.KeyCode == Keys.Clear) { ekey = Keys.NumPad5; } else if (e.KeyCode == Keys.F2) { ContrastMode = !ContrastMode; return; } else if (e.KeyCode == Keys.F3) { if (state == States.NEWGAME && Neat.All.Count < 24) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { List <byte> bytes = new List <byte>(File.ReadAllBytes(dialog.FileName)); Neat.remove(bytes, 8); Neat n = Neat.decompile(bytes); n.SetupGenZero(); n.AddKey(); IKey.UpdateAll(); Map.SetMaxPoints(); // Set color Program.HEADS[n.Key].color = n.color.GetValueOrDefault(Entities.Head.GenerateColor()); } } return; } else if (e.KeyCode == Keys.F1) { if (state != States.NEWGAME) { return; } if (ActiveKeys.Count > 0) { new AI(ActiveKeys[ActiveKeys.Count - 1]); MVP.Flash($"AI added to {HEADS[ActiveKeys[ActiveKeys.Count - 1]].DisplayKey}"); HEADS[ActiveKeys[ActiveKeys.Count - 1]].DisplayKey = "AI"; } return; } else if (e.KeyCode == Keys.F4 && !e.Alt) { if (state == States.NEWGAME) { if (Neat.All.Count > 0) { Neat toRemove = Neat.All.Last(); toRemove.Remove(); IKey.UpdateAll(); } Map.SetMaxPoints(); } return; } else if (e.KeyCode == Keys.F9 && e.Alt) { if (ActiveKeys.Count == 0 || InactiveKeys.Count == 0) { return; } HEADS[InactiveKeys[0]].Reward(0, ActiveKeys[0]); HEADS[ActiveKeys[0]].Die(); return; } else if (e.KeyCode == Keys.F10) { if (SyncUpdate) { SyncUpdate = false; UpdateThread.Stop(); NeuralThread.Stop(); StartAsyncUpdate(); } else { SyncUpdate = true; UpdateThread.Start(); NeuralThread.Start(); } return; } else if (e.KeyCode == Keys.F7) { SlowMo = true; return; } else if (e.KeyCode == Keys.F8) { SpeedMo = true; return; } else if (e.KeyCode == Keys.F11) { FullScreen = !FullScreen; if (FullScreen) { Width = Screen.PrimaryScreen.Bounds.Width; FormBorderStyle = FormBorderStyle.None; WindowState = FormWindowState.Maximized; Cursor.Hide(); } else { Width = 1024; Height = 600; FormBorderStyle = FormBorderStyle.Sizable; WindowState = FormWindowState.Normal; Cursor.Show(); } return; } else if (e.KeyCode == Keys.F12) { if (!e.Alt) { if (map is BotArena) { List <byte> bytes = new List <byte>(); bytes.AddRange(((BotArena)map).GetGeneration()); foreach (Neat bot in ((BotArena)map).bots) { bytes.AddRange(Neat.compile(bot)); } SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { File.WriteAllBytes(dialog.FileName, bytes.ToArray()); } } else if (ActiveKeys.Count == 0) { Cursor.Show(); OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { List <byte> bytes = new List <byte>(File.ReadAllBytes(dialog.FileName)); BotArena arena = new BotArena(BotArena.Type.MAX_POINTS); arena.SetGeneration(bytes); map = arena; while (bytes.Count > 0) { arena.bots.Add(Neat.decompile(bytes)); } arena.AddBots(false); Map.StartGame(); Ingame = true; state = States.INGAME; } if (FullScreen) { Cursor.Hide(); } } } else if (state == States.NEWGAME && !TutorialActive) { if (ActiveKeys.Count != 1) { return; } TUTO = new Tutorial(Map); TutorialActive = true; Map.StartGame(); if (SyncUpdate) { NeuralThread.Start(); } } return; // trigger tutorial } else if (e.KeyCode == Keys.F6) { if (ActiveKeys.Count != 0) { return; } BotArena arena = new BotArena(7, BotArena.Type.MAX_POINTS); /// < - - - - - - - - - - - - - - - - map = arena; arena.AddBots(false); Map.StartGame(); Ingame = true; state = States.INGAME; return; // trigger bot arena } else if (e.KeyCode == Keys.F5) { if (state != States.NEWGAME) { return; } switch (Gamemode) { case Gamemodes.CLASSIC: MVP.SetText("Red chaos"); Gamemode = Gamemodes.CHAOS_RED; foreach (Head head in HEADS.Values) { head.NewColor(true); } break; case Gamemodes.CHAOS_RED: MVP.SetText("Rainbow chaos"); Gamemode = Gamemodes.CHAOS_RAINBOW; foreach (Head head in HEADS.Values) { head.NewColor(); } break; case Gamemodes.CHAOS_RAINBOW: MVP.SetText("King of the hill"); foreach (Head head in HEADS.Values) { head.NewColor(false); } Map.MaxOrbs = 7; Gamemode = Gamemodes.KING_OF_THE_HILL; break; case Gamemodes.KING_OF_THE_HILL: MVP.SetText("Hidden"); Map.MaxOrbs = 8; Gamemode = Gamemodes.HIDDEN; break; case Gamemodes.HIDDEN: MVP.SetText("Yeet Mode"); Gamemode = Gamemodes.YEET_MODE; Map.blastSpawn = BlastSpawn.RARE; break; case Gamemodes.YEET_MODE: MVP.SetText("Classic"); Map.MaxOrbs = 255; Gamemode = Gamemodes.CLASSIC; break; } UpdateColors?.Invoke(); return; } switch (state) { case States.NEWGAME: //add new player if (e.KeyCode == Keys.Enter && ActiveKeys.Count > 0) // (re)start game { if (e.Shift) { Map.ResumeGame(); } else { Map.StartGame(); } if (SyncUpdate) { NeuralThread.Start(); } Ingame = true; state = States.INGAME; } else if (e.KeyCode == Keys.Escape) // clear all keys { HashSet <Neat> NeatCopy = new HashSet <Neat>(Neat.All); foreach (Neat neat in NeatCopy) { neat.Remove(); } HashSet <Head> HeadCopy = new HashSet <Head>(HEADS.Values); foreach (Head head in HeadCopy) { head.Remove(); } Map.MaxPoints = 0; Leader = Keys.None; } else if (0 <= e.KeyValue && e.KeyValue < 256) //add key { lock (ActiveLock) { if (ActiveKeys.Contains(ekey)) { HEADS[ekey].Remove(); } else { if (HEADSOnPause.ContainsKey(ekey)) { HEADS.Add(ekey, new Head(HEADSOnPause[ekey])); } else { HEADS.Add(ekey, new Head(ekey)); } ActiveKeys.Add(ekey); Map.SetMaxPoints(); } } IKey.UpdateAll(); } break; case States.INGAME: if (e.KeyCode == Keys.Escape) //pause game { ApplicationPause(); } else if (ActiveKeys.Contains(ekey)) //default action { HEADS[ekey].Action(); } break; case States.PAUSED: if (e.KeyCode == Keys.Escape) // unpause { state = States.INGAME; Ingame = true; if (SyncUpdate) { UpdateThread.Start(); NeuralThread.Start(); } else { StartAsyncUpdate(); } VisualThread.Start(); //ForcePaused = false; } else if (e.KeyCode == Keys.Enter) // let players join { HEADSOnPause.Clear(); foreach (KeyValuePair <Keys, Head> player in HEADS) { HEADSOnPause.Add(player.Key, player.Value); } Map.EndGame(); Map.Clear(); Map.phase = Phases.NONE; state = States.NEWGAME; MVP.Show("Prepare!"); lock (ActiveLock) foreach (Keys key in ActiveKeys) { HEADS[key].v = IVector.Up; } Ingame = false; VisualThread.Start(); if (SyncUpdate) { UpdateThread.Start(); } else { StartAsyncUpdate(); } } break; } }
public void Die() { lock (lock_die) { if (Died) { return; } this.act = Activities.DEAD; } OnUpdate -= e_update; if (AnimationsEnabled) { window.DrawHead -= draw_event; } if (Tick < 60 + Map.StartRoundTime) { MVP.Add(MVPTypes.EARLY_KILL, DisplayKey); } lock (ActiveLock) { ActiveKeys.Remove(this.KeyCode); if (KingOfTheHill) { InactiveKeys.Add(this.KeyCode); } else { InactiveKeys.Insert(0, this.KeyCode); } } key.Die(); tail.Die(); z = 0; DashHideText = false; if (isBounty) { new Animation(pos, 30, HeadR - 1, HeadR - 5, 5, 0, Color.Red, Color.Red, AnimationTypes.CUBED); } isBounty = false; isDoubleBounty = false; if (AnimationsEnabled && ActiveKeys.Count <= 12 && !(Map is BotArena)) { _ = new Animation(pos, 80, 0, W, HeadR, (float)PHI * HeadR, Color.FromArgb(150, this.color), 0); } if (AnimationsEnabled) { _ = new Animation(pos, 12, 0, 0, HeadR, HeadR, this.color, 32, AnimationTypes.CUBED); } else { window.writeln(DisplayKey + " died: " + Points + " pts"); } if (KingOfTheHill) { IKey.UpdateAll(); new Thread(() => { int tick = Tick; SpinWait.SpinUntil(() => Tick >= tick + 120 || Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning); if (Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning) { return; } new Coin(IPoint.Center + 3 * IVector.Random(), "3", this.color); SpinWait.SpinUntil(() => Tick >= tick + 180 || Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning); if (Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning) { return; } new Coin(IPoint.Center + 3 * IVector.Random(), "2", this.color); SpinWait.SpinUntil(() => Tick >= tick + 240 || Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning); if (Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning) { return; } new Coin(IPoint.Center + 3 * IVector.Random(), "1", this.color); SpinWait.SpinUntil(() => Tick >= tick + 300 || Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning); if (Map.phase != Phases.NONE || state == States.NEWGAME || !ApplicationRunning) { return; } lock (updatinglocker) { this.clear(); this.Revive(); this.act = Activities.DEFAULT; lock (ActiveLock) { ActiveKeys.Add(KeyCode); InactiveKeys.Remove(KeyCode); } this.INVINCIBLE = true; } SpinWait.SpinUntil(() => (Tick >= tick + 360 && !is_about_to_die()) || !ApplicationRunning); this.INVINCIBLE = false; SpinWait.SpinUntil(() => (Tick >= tick + 372 || Died) || !ApplicationRunning); if (Tick <= tick + 372 && Died) { MVP.Flash("Spawnkill!"); } }).Start(); } else { Map.Sort(); IKey.UpdateAll(); if (ActiveKeys.Count == 1 && Map.phase != Phases.ENDROUND) { new Thread(Map.EndRound).Start(); } } if (OnDie != null) { Parallel.Invoke(OnDie); } }
private static void CheckKeyState(KeyboardState previousState, KeyboardState currentState) { for (int i = 0; i < currentState.GetPressedKeys().Length; i++) { KeyToCheck = currentState.GetPressedKeys()[i]; if (previousState.IsKeyUp(KeyToCheck) && currentState.IsKeyDown(KeyToCheck)) { // pressedKeys.Add(KeyToCheck); // pressedKeysStates.Add(KeyState.Clicked); ActiveKeys.Add(new KeyboardButtonState(KeyToCheck)); } else if (previousState.IsKeyDown(KeyToCheck) && currentState.IsKeyDown(KeyToCheck)) { // pressedKeysStates[pressedKeys.IndexOf(KeyToCheck)] = KeyState.Held; foreach (KeyboardButtonState key in ActiveKeys) { if (key.Button == KeyToCheck) { key.ButtonState = KeyboardButtonState.KeyState.Held; } } } } for (int i = 0; i < previousState.GetPressedKeys().Length; i++) { KeyToCheck = previousState.GetPressedKeys()[i]; if (previousState.IsKeyDown(KeyToCheck) && currentState.IsKeyUp(KeyToCheck)) { // pressedKeysStates[pressedKeys.IndexOf(KeyToCheck)] = KeyState.Released; foreach (KeyboardButtonState key in ActiveKeys) { if (key.Button == KeyToCheck) { key.ButtonState = KeyboardButtonState.KeyState.Released; } } } } for (int i = 0; i < ActiveKeys.Count; i++) { if (previousState.IsKeyUp(ActiveKeys[i].Button) && currentState.IsKeyUp(ActiveKeys[i].Button)) { ActiveKeys[i].Button = Keys.None; ActiveKeys[i].ButtonState = KeyboardButtonState.KeyState.None; } } // while (ActiveKeys.Contains(Keys.None)) // { // pressedKeysStates.RemoveAt(pressedKeys.IndexOf(Keys.None)); // pressedKeys.RemoveAt(pressedKeys.IndexOf(Keys.None)); // } while (ActiveKeys.Contains(new KeyboardButtonState(Keys.None))) { ActiveKeys.Remove(new KeyboardButtonState(Keys.None)); } // for (int i = 0; i < ActiveKeys.Count; i++) // { // if (ActiveKeys[i].Button == Keys.None) // { // // } // } }