Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Digger
- {
- public class GameState
- {
- public static DiggerWindow Window = new DiggerWindow();
- public Game Game = Window.game;
- public List<CreatureAnimation> animations = new List<CreatureAnimation>();
- public const int ElementSize = 40;
- public void BeginAct()
- {
- animations.Clear();
- for (int x = 0; x < Game.MapWidth; x++)
- for (int y = 0; y < Game.MapHeight; y++)
- {
- var creature = Game.Map[x, y];
- if (creature == null) continue;
- var command = creature.Act(x, y);
- if (x + command.DeltaX < 0 || x + command.DeltaX >= Game.MapWidth || y + command.DeltaY < 0 || y + command.DeltaY >= Game.MapHeight)
- throw new Exception($"The object {creature.GetType()} falls out of the game field");
- animations.Add(new CreatureAnimation
- {
- Command = command,
- Creature = creature,
- Location = new Point(x * ElementSize, y * ElementSize),
- TargetLogicalLocation = new Point(x + command.DeltaX, y + command.DeltaY)
- });
- }
- animations = animations.OrderByDescending(z => z.Creature.DrawingPriority).ToList();
- }
- public void EndAct()
- {
- for (int x = 0; x < Game.MapWidth; x++)
- for (int y = 0; y < Game.MapHeight; y++)
- Game.Map[x, y] = null;
- foreach (var e in animations)
- {
- var x = e.TargetLogicalLocation.X;
- var y = e.TargetLogicalLocation.Y;
- var nextCreature = e.Command.TransformTo == null ? e.Creature : e.Command.TransformTo;
- if (Game.Map[x, y] == null) Game.Map[x, y] = nextCreature;
- else
- {
- bool newDead = nextCreature.DeadInConflict(Game.Map[x, y]);
- bool oldDead = Game.Map[x, y].DeadInConflict(nextCreature);
- if (newDead && oldDead)
- Game.Map[x, y] = null;
- else if (!newDead && oldDead)
- Game.Map[x, y] = nextCreature;
- else if (!newDead && !oldDead)
- throw new Exception(string.Format("Существа {0} и {1} претендуют на один и тот же участок карты", nextCreature.GetType().Name, Game.Map[x, y].GetType().Name));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement