Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Digger
  9. {
  10.     public class GameState
  11.     {
  12.         public static DiggerWindow Window = new DiggerWindow();
  13.         public Game Game = Window.game;
  14.         public List<CreatureAnimation> animations = new List<CreatureAnimation>();
  15.  
  16.         public const int ElementSize = 40;
  17.  
  18.         public void BeginAct()
  19.         {
  20.             animations.Clear();
  21.             for (int x = 0; x < Game.MapWidth; x++)
  22.                 for (int y = 0; y < Game.MapHeight; y++)
  23.                 {
  24.                     var creature = Game.Map[x, y];
  25.                     if (creature == null) continue;
  26.                     var command = creature.Act(x, y);
  27.  
  28.                     if (x + command.DeltaX < 0 || x + command.DeltaX >= Game.MapWidth || y + command.DeltaY < 0 || y + command.DeltaY >= Game.MapHeight)
  29.                         throw new Exception($"The object {creature.GetType()} falls out of the game field");
  30.  
  31.                     animations.Add(new CreatureAnimation
  32.                     {
  33.                         Command = command,
  34.                         Creature = creature,
  35.                         Location = new Point(x * ElementSize, y * ElementSize),
  36.                         TargetLogicalLocation = new Point(x + command.DeltaX, y + command.DeltaY)
  37.                     });
  38.                 }
  39.             animations = animations.OrderByDescending(z => z.Creature.DrawingPriority).ToList();
  40.         }
  41.  
  42.         public void EndAct()
  43.         {
  44.             for (int x = 0; x < Game.MapWidth; x++)
  45.                 for (int y = 0; y < Game.MapHeight; y++)
  46.                     Game.Map[x, y] = null;
  47.  
  48.             foreach (var e in animations)
  49.             {
  50.                 var x = e.TargetLogicalLocation.X;
  51.                 var y = e.TargetLogicalLocation.Y;
  52.                 var nextCreature = e.Command.TransformTo == null ? e.Creature : e.Command.TransformTo;
  53.                 if (Game.Map[x, y] == null) Game.Map[x, y] = nextCreature;
  54.                 else
  55.                 {
  56.                     bool newDead = nextCreature.DeadInConflict(Game.Map[x, y]);
  57.                     bool oldDead = Game.Map[x, y].DeadInConflict(nextCreature);
  58.                     if (newDead && oldDead)
  59.                         Game.Map[x, y] = null;
  60.                     else if (!newDead && oldDead)
  61.                         Game.Map[x, y] = nextCreature;
  62.                     else if (!newDead && !oldDead)
  63.                         throw new Exception(string.Format("Существа {0} и {1} претендуют на один и тот же участок карты", nextCreature.GetType().Name, Game.Map[x, y].GetType().Name));
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement