public void Act(Hero hero, Map dMap) { //check for hero in sight if (Vector2.Distance(new Vector2(hero.mapX, hero.mapY), new Vector2(mapX, mapY)) < 6 && dMap.LoSLine(mapX, mapY, hero.mapX, hero.mapY)) { curAIMode = AIMode.Aggro; curTarLoc = new Point(hero.mapX, hero.mapY); if (Math.Abs(mapX - hero.mapX) + Math.Abs(mapY - hero.mapY) != 1) { mQueue = dMap.PathTo(new Point(mapX, mapY), curTarLoc, false, false); if (mQueue.Count > 20 || mQueue.Count == 0) { //Console.WriteLine(string.Format("{0} found unusable path (length {1}) - calculating direct path",name,mQueue.Count)); mQueue = dMap.PathTo(new Point(mapX, mapY), curTarLoc, false, true); //Console.WriteLine(string.Format("{0} found direct path (length {1})", name, mQueue.Count)); } } } //act accordingly if (curAIMode == AIMode.Wander) { while (mQueue.Count == 0) { curTarLoc = dMap.openSquares[rando.Next(dMap.openSquares.Count)]; if (dMap.blocks[curTarLoc.X, curTarLoc.Y]) { mQueue = dMap.PathTo(new Point(mapX, mapY), curTarLoc, false); } } MoveFromQueue(dMap); } if (curAIMode == AIMode.Alert) { MoveFromQueue(dMap); if (mQueue.Count == 0) curAIMode = AIMode.Wander; } if (curAIMode == AIMode.Aggro) { if (dMap.LoSLine(mapX, mapY, hero.mapX, hero.mapY)) { //Console.WriteLine(string.Format("Moving from {0}, {1} to {2}, {3}", mapX, mapY, mQueue[0].X, mQueue[0].Y)); if (Math.Abs(mapX - hero.mapX) + Math.Abs(mapY - hero.mapY) == 1) { Attack(hero); } else MoveFromQueue(dMap); } else { curAIMode = AIMode.Alert; } } }
public void SaveHero(Hero heroToSave) { //try //{ if (!System.IO.Directory.Exists(heroToSave.name)) System.IO.Directory.CreateDirectory(heroToSave.name); BinaryFormatter bf = new BinaryFormatter(); FileStream outstream = File.OpenWrite(string.Format("{0}\\hero.mob", heroToSave.name)); bf.Serialize(outstream, heroToSave); outstream.Close(); //} //catch (Exception) //{ //} }
public void Attack(Hero hero) { delay += 1; Console.WriteLine(string.Format("{0} Attacked a hero!", name)); }
public void DrawAll(GameState gameState, SpriteBatch screen, Hero hero, Map dMap) { if (gameState == GameState.Running) { //map overlays if (hero.mQueue.Count > 0) { Color olcol = new Color(70, 210, 100, 50); screen.Draw(overlays[1], new Rectangle(hero.mQueue[hero.mQueue.Count - 1].X * dMap.tileSizeX + dMap.xOffset, hero.mQueue[hero.mQueue.Count - 1].Y * dMap.tileSizeY + dMap.yOffset, dMap.tileSizeX, dMap.tileSizeY), olcol); if (hero.mQueue.Count > 1) { for (int i = 0; i < hero.mQueue.Count - 1; i++) { screen.Draw(overlays[0], new Rectangle(hero.mQueue[i].X * dMap.tileSizeX + dMap.xOffset, hero.mQueue[i].Y * dMap.tileSizeY + dMap.yOffset, dMap.tileSizeX, dMap.tileSizeY), olcol); } } } //box backgrounds foreach (Box thisbox in boxes) { thisbox.Draw(screen); } //log int firstL = logLines.Count - 6 - logOff; if (firstL < 0) firstL = 0; int lastL = logLines.Count - 1 - logOff; for (int i = firstL; i <= lastL; i++) { Vector2 FontPos = new Vector2(boxes[0].contRect.Left + 3, boxes[0].contRect.Top + (15 * (i - firstL))); screen.DrawString(logFont, logLines[i], FontPos, logColors[i]); } //minimap int lX, lY, uX, uY; int tX = hero.mapX - (boxes[1].contRect.Width / 8); if (tX < 0) { lX = ((-4 * tX) + boxes[1].contRect.Left); tX = 0; } else lX = (boxes[1].contRect.Left); int tY = hero.mapY - (boxes[1].contRect.Height / 8); if (tY < 0) { lY = ((-4 * tY) + boxes[1].contRect.Top); tY = 0; } else lY = (boxes[1].contRect.Left); uX = boxes[1].contRect.Left + boxes[1].contRect.Width; uY = boxes[1].contRect.Top + boxes[1].contRect.Height; Rectangle prect; int dx = lX; int dy = lY; while (tX < dMap.bounds.X && dx < uX) { while (tY < dMap.bounds.Y && dy < uY) { if (dMap.vision[tX, tY] > 0) { prect = new Rectangle(dx, dy, 4, 4); if (dMap.floor[tX, tY] == FloorTypes.Wall) screen.Draw(mmtile, prect, Color.DarkSlateGray); else if (dMap.floor[tX, tY] == FloorTypes.Floor) screen.Draw(mmtile, prect, Color.LightGray); else if (dMap.floor[tX, tY] == FloorTypes.Feature) screen.Draw(mmblock, prect, Color.Gray); } dy += 4; tY += 1; } dy = lY; tY = hero.mapY - (boxes[1].contRect.Height / 8); if (tY < 0) tY = 0; tX += 1; dx += 4; } foreach (Item wonder in dMap.items) { int mx = boxes[1].contRect.Center.X - (4 * (hero.mapX - wonder.MapX)); int my = boxes[1].contRect.Center.Y - (4 * (hero.mapY - wonder.MapY)); if (dMap.vision[wonder.MapX, wonder.MapY] == Vision.Visible && boxes[1].contRect.Contains(new Point(mx, my))) { prect = new Rectangle(mx, my, 4, 4); screen.Draw(mmmark, prect, Color.Gold); } } foreach (Monster fiend in dMap.monsters) { int mx = boxes[1].contRect.Center.X - (4 * (hero.mapX - fiend.mapX)); int my = boxes[1].contRect.Center.Y - (4 * (hero.mapY - fiend.mapY)); if (dMap.vision[fiend.mapX, fiend.mapY] == Vision.Visible && boxes[1].contRect.Contains(new Point(mx, my))) { prect = new Rectangle(mx, my, 4, 4); screen.Draw(mmmark, prect, Color.Red); } } prect = new Rectangle(boxes[1].contRect.Center.X, boxes[1].contRect.Center.Y, 4, 4); screen.Draw(mmmark, prect, Color.Lime); //stats //screen.Draw(statBar, new Rectangle(boxes[2].contRect.Left + 5, boxes[2].contRect.Top + 5, boxes[2].contRect.Width - 10, 20), Color.Green); //screen.Draw(statBar, new Rectangle(boxes[2].contRect.Left + 5, boxes[2].contRect.Top + 30, boxes[2].contRect.Width - 10, 20), Color.BlueViolet); screen.Draw(statglobe, new Rectangle(statsLoc.X, statsLoc.Y - 40, 60, 60), Color.Red); screen.DrawString(statFont, hero.stats.Health.ToString(), new Vector2(statsLoc.X + 30 - (int)statFont.MeasureString(hero.stats.Health.ToString()).X / 2, statsLoc.Y - 10 - (int)statFont.MeasureString(hero.stats.Health.ToString()).Y / 2), Color.LightGray); screen.Draw(statglobe, new Rectangle(statsLoc.X - 35, statsLoc.Y + 20, 60, 60), Color.Green); screen.DrawString(statFont, hero.stats.Vitality.ToString(), new Vector2(statsLoc.X - 5 - (int)statFont.MeasureString(hero.stats.Vitality.ToString()).X / 2, statsLoc.Y + 50 - (int)statFont.MeasureString(hero.stats.Vitality.ToString()).Y / 2), Color.LightGray); screen.Draw(statglobe, new Rectangle(statsLoc.X + 35, statsLoc.Y + 20, 60, 60), Color.Blue); screen.DrawString(statFont, hero.stats.Sanity.ToString(), new Vector2(statsLoc.X + 65 - (int)statFont.MeasureString(hero.stats.Sanity.ToString()).X / 2, statsLoc.Y + 50 - (int)statFont.MeasureString(hero.stats.Sanity.ToString()).Y / 2), Color.LightGray); //action bar for (int i = 0; i < 10; i++) { if (i == selAct) screen.Draw(itemcircle, new Rectangle(boxes[3].contRect.Left + (50 * i), boxes[3].contRect.Top, 50, 50), Color.DarkGreen); else screen.Draw(itemcircle, new Rectangle(boxes[3].contRect.Left + (50 * i), boxes[3].contRect.Top, 50, 50), Color.Blue); Vector2 FontPos = new Vector2(boxes[3].contRect.Left + 1 + (50 * i), boxes[3].contRect.Top + 1); screen.DrawString(logFont, ((i + 1) % 10).ToString(), FontPos, Color.White); } for (int i = 0; i < actIcons.Count && i < 10; i++) { screen.Draw(actIcons[i], new Rectangle(boxes[3].contRect.Left + 5 + (50 * i), boxes[3].contRect.Top + 5, 40, 40), Color.White); } } else if (gameState == GameState.MainMenu) { mainMenu.DrawMenu(screen, hlCol); } else if (gameState == GameState.Graveyard) { for (int i = graves.Count - 1; i >= 0; i--) { int row = (int)Math.Floor((double)i / 4); int col = i % 4; Color drawcol = new Color(220 - (40 * row), 220 - (40 * row), 220 - (40 * row)); screen.Draw(graveTex, new Rectangle(20 + (200 - (30 * row)) * col + (60 * row), (1500 / (10 + row ^ 2)), 180 - 50 * row, 360 - 100 * row), new Rectangle(10 * (graves[i].level / 4), 0, 10, 20), drawcol); } } }
public void ProcessInput(GameLogic gamelogic, GameState gameState, KeyboardState oKS, KeyboardState nKS, MouseState oMS, MouseState nMS, Hero hero, Map dMap) { if (gameState == GameState.Running) { if (nKS.IsKeyDown(Keys.Escape) && !oKS.IsKeyDown(Keys.Escape)) { gamelogic.OpenMainMenu("blank"); } //adjust log if (nKS.IsKeyDown(Keys.OemMinus) && oKS.IsKeyUp(Keys.OemMinus)) LogScrollUp(); else if (nKS.IsKeyDown(Keys.OemPlus) && oKS.IsKeyUp(Keys.OemPlus)) LogScrollDown(); //select actions if (nKS.IsKeyDown(Keys.D1) && oKS.IsKeyUp(Keys.D1)) selAct = 0; else if (nKS.IsKeyDown(Keys.D2) && oKS.IsKeyUp(Keys.D2)) selAct = 1; else if (nKS.IsKeyDown(Keys.D3) && oKS.IsKeyUp(Keys.D3)) selAct = 2; else if (nKS.IsKeyDown(Keys.D4) && oKS.IsKeyUp(Keys.D4)) selAct = 3; else if (nKS.IsKeyDown(Keys.D5) && oKS.IsKeyUp(Keys.D5)) selAct = 4; else if (nKS.IsKeyDown(Keys.D6) && oKS.IsKeyUp(Keys.D6)) selAct = 5; else if (nKS.IsKeyDown(Keys.D7) && oKS.IsKeyUp(Keys.D7)) selAct = 6; else if (nKS.IsKeyDown(Keys.D8) && oKS.IsKeyUp(Keys.D8)) selAct = 7; else if (nKS.IsKeyDown(Keys.D9) && oKS.IsKeyUp(Keys.D9)) selAct = 8; else if (nKS.IsKeyDown(Keys.D0) && oKS.IsKeyUp(Keys.D0)) selAct = 9; //Mouse Input Point mPoint = new Point(nMS.X, nMS.Y); Point mapMLoc = dMap.XYtoMap(nMS.X, nMS.Y); if (gD.Viewport.Bounds.Contains(mPoint)) { if (oMS.LeftButton != ButtonState.Pressed && nMS.LeftButton == ButtonState.Pressed) { if (CatchClick(mPoint, nMS)) { } else { //clicks on map //Console.WriteLine(string.Format("Click at {0}, {1}",tarpoint.X, tarpoint.Y)); if (dMap.IsInMap(mapMLoc) && dMap.vision[mapMLoc.X, mapMLoc.Y] > 0 && dMap.blocks[mapMLoc.X, mapMLoc.Y] && !(hero.mapX == mapMLoc.X && hero.mapY == mapMLoc.Y)) { hero.mQueue = dMap.PathTo(new Point(hero.mapX, hero.mapY), mapMLoc); } } } if (oMS.RightButton != ButtonState.Pressed && nMS.RightButton == ButtonState.Pressed) { if (CatchClick(mPoint, nMS)) { } else { //clicks on map //Console.WriteLine(string.Format("Click at {0}, {1}",tarpoint.X, tarpoint.Y)); if (dMap.IsInMap(mapMLoc) && dMap.blocks[mapMLoc.X, mapMLoc.Y] && !(hero.mapX == mapMLoc.X && hero.mapY == mapMLoc.Y)) { dMap.monsters.Add(new Monster(string.Format("M{0}", dMap.monsters.Count), 0, mapMLoc.X, mapMLoc.Y)); dMap.monsters[dMap.monsters.Count - 1].Move(mapMLoc.X, mapMLoc.Y, dMap); } else { foreach (Monster fiend in dMap.monsters) { if (fiend.mapX == mapMLoc.X && fiend.mapY == mapMLoc.Y) { dMap.alts[fiend.mapX, fiend.mapY] = 17 + rando.Next(3); fiend.Kill(dMap); } } } } } } } else if (gameState == GameState.MainMenu) { mainMenu.ClearHighlight(); mainMenu.MenuHover(nMS.X, nMS.Y); if (nMS.LeftButton == ButtonState.Pressed && oMS.LeftButton == ButtonState.Released) { mainMenu.MenuClick(nMS.X, nMS.Y); } } else if (gameState == GameState.Graveyard) { if (nKS.IsKeyDown(Keys.Escape) && !oKS.IsKeyDown(Keys.Escape)) { gamelogic.OpenMainMenu("blank"); } } }
/// <summary> /// Centers the map; called when window size is changed. /// </summary> /// <param name="refmob">The hero to center the map on.</param> private void AlignMap(Hero refmob) { dMap.xOffset = GraphicsDevice.Viewport.Width / 2 - ((GraphicsDevice.Viewport.Width / 2) % dMap.tileSizeX) - (hero.mapX * dMap.tileSizeX); dMap.yOffset = GraphicsDevice.Viewport.Height / 2 - ((GraphicsDevice.Viewport.Height / 2) % dMap.tileSizeY) - (hero.mapY * dMap.tileSizeY); }
/// <summary> /// Handles keyboard and mouse input. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> private void UpdateInput(GameTime gameTime) { KeyboardState newKState = Keyboard.GetState(); MouseState newMState = Mouse.GetState(); //gui interaction Point mpoint = new Point(0,0); gui.ProcessInput(this, gameState, oldKState, newKState, oldMState, newMState, hero, dMap); if (gameState == GameState.Running) { //save & menu functions if (newKState.IsKeyDown(Keys.F5) && !oldKState.IsKeyDown(Keys.F5)) { heroGen.SaveHero(hero); mapGen.SaveMap(hero.name, dMap); gui.LogEntry(string.Format("Saving {0}...", hero.name), Color.Brown); } if (newKState.IsKeyDown(Keys.F9) && !oldKState.IsKeyDown(Keys.F9)) { dMap = mapGen.LoadMap(hero.name, 0); hero = heroGen.LoadHero(hero.name); AlignMap(hero); FixMap(); gui.LogEntry(string.Format("Loading {0}...", hero.name), Color.Brown); } //movement if ((gameTime.TotalGameTime - lastTurnTime).Milliseconds >= repTime) { if (newKState.IsKeyDown(Keys.Down) || newKState.IsKeyDown(Keys.S)) { hero.Move(hero.mapX, hero.mapY + 1, dMap); } if (newKState.IsKeyDown(Keys.Up) || newKState.IsKeyDown(Keys.W)) { hero.Move(hero.mapX, hero.mapY - 1, dMap); } if (newKState.IsKeyDown(Keys.Left) || newKState.IsKeyDown(Keys.A)) { hero.Move(hero.mapX - 1, hero.mapY, dMap); } if (newKState.IsKeyDown(Keys.Right) || newKState.IsKeyDown(Keys.D)) { hero.Move(hero.mapX + 1, hero.mapY, dMap); } if (newKState.IsKeyDown(Keys.Space)) { if (hero.delay == 0) hero.delay += 1; } } //queued moves if (hero.mQueue.Count > 0 && (gameTime.TotalGameTime - lastTurnTime).Milliseconds >= repTime) { hero.MoveFromQueue(dMap); } // Realign the map if (hero.delay > 0) { FixMap(); EndTurn(gameTime); } } // Update saved state. oldKState = newKState; oldMState = newMState; }
public void NewGame(string hclass) { dMap = mapGen.GenerateMap(); gui.LogEntry("Welcome to Sprite Strife!", Color.LightGray); //create the hero hero = new Hero("testchar", 0); hero.mapX = dMap.startingLocation.X; hero.mapY = dMap.startingLocation.Y; FixMap(); mapGen.SaveMap("testchar", dMap); heroGen.SaveHero(hero); gameState = GameState.Running; }
public void ContinueGame(string hname) { if (System.IO.Directory.Exists("testchar")) { dMap = mapGen.LoadMap("testchar", 0); hero = heroGen.LoadHero("testchar"); FixMap(); gameState = GameState.Running; } }
/// <summary> /// Calculates LoS based on a pregenerated ray table. /// </summary> public void DoSight(Hero refmob, bool globalSight = false) { for (int x = 0; x < bounds.X; x++) { for (int y = 0; y < bounds.Y; y++) { if (vision[x, y] == Vision.Visible) { vision[x, y] = Vision.Explored; } //DEBUG SIGHT if (globalSight) vision[x, y] = Vision.Visible; } } Point cpoint = new Point(refmob.mapX, refmob.mapY); foreach (IEnumerable<Point> thisray in rayList) { foreach (Point thispoint in thisray) { Point tpoint = new Point(thispoint.X + cpoint.X, thispoint.Y + cpoint.Y); if (IsInMap(tpoint)) { vision[tpoint.X, tpoint.Y] = Vision.Visible; if (!trans[tpoint.X, tpoint.Y]) { break; } } } } //IEnumerable<Point> visarea = rasterCircle(cpoint, 5); //foreach (Point thispoint in visarea) //{ // if (IsInMap(thispoint)) { dMap.vision[thispoint.X, thispoint.Y] = 1; } //} }
/// <summary> /// Handles keyboard and mouse input. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> private void UpdateInput(GameTime gameTime) { KeyboardState newKState = Keyboard.GetState(); MouseState newMState = Mouse.GetState(); //gui interaction Point mpoint = new Point(0, 0); gui.ProcessInput(this, gameState, oldKState, newKState, oldMState, newMState, hero, dMap); if (gameState == GameState.Running) { //save & menu functions if (newKState.IsKeyDown(Keys.F5) && !oldKState.IsKeyDown(Keys.F5)) { heroGen.SaveHero(hero); mapGen.SaveMap(hero.name, dMap); gui.LogEntry(string.Format("Saving {0}...", hero.name), Color.Brown); } if (newKState.IsKeyDown(Keys.F9) && !oldKState.IsKeyDown(Keys.F9)) { dMap = mapGen.LoadMap(hero.name, 0); hero = heroGen.LoadHero(hero.name); AlignMap(hero); FixMap(); gui.LogEntry(string.Format("Loading {0}...", hero.name), Color.Brown); } //movement if ((gameTime.TotalGameTime - lastTurnTime).Milliseconds >= repTime) { if (newKState.IsKeyDown(Keys.Down) || newKState.IsKeyDown(Keys.S)) { hero.Move(hero.mapX, hero.mapY + 1, dMap); } if (newKState.IsKeyDown(Keys.Up) || newKState.IsKeyDown(Keys.W)) { hero.Move(hero.mapX, hero.mapY - 1, dMap); } if (newKState.IsKeyDown(Keys.Left) || newKState.IsKeyDown(Keys.A)) { hero.Move(hero.mapX - 1, hero.mapY, dMap); } if (newKState.IsKeyDown(Keys.Right) || newKState.IsKeyDown(Keys.D)) { hero.Move(hero.mapX + 1, hero.mapY, dMap); } if (newKState.IsKeyDown(Keys.Space)) { if (hero.delay == 0) { hero.delay += 1; } } } //queued moves if (hero.mQueue.Count > 0 && (gameTime.TotalGameTime - lastTurnTime).Milliseconds >= repTime) { hero.MoveFromQueue(dMap); } // Realign the map if (hero.delay > 0) { FixMap(); EndTurn(gameTime); } } // Update saved state. oldKState = newKState; oldMState = newMState; }