Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Digger
- {
- public class Terrain : ICreature
- {
- public CreatureCommand Act(int x, int y) => new CreatureCommand {DeltaX = 0, DeltaY = 0};
- public bool DeadInConflict(ICreature conflictedObject) => true;
- public int GetDrawingPriority() => 2;
- public string GetImageFileName() => "Terrain.png";
- }
- public class Player : ICreature
- {
- public CreatureCommand Act(int x, int y)
- {
- switch (Game.KeyPressed)
- {
- case System.Windows.Forms.Keys.Left:
- if (IsStepPossible(x - 1, y))
- return new CreatureCommand {DeltaX = -1, DeltaY = 0};
- break;
- case System.Windows.Forms.Keys.Up:
- if (IsStepPossible(x, y - 1))
- return new CreatureCommand {DeltaX = 0, DeltaY = -1};
- break;
- case System.Windows.Forms.Keys.Right:
- if (IsStepPossible(x, y + 1))
- return new CreatureCommand {DeltaX = 0, DeltaY = 1};
- break;
- case System.Windows.Forms.Keys.Down:
- if (IsStepPossible(x + 1, y))
- return new CreatureCommand {DeltaX = 1, DeltaY = 0};
- break;
- }
- return new CreatureCommand();
- }
- public bool DeadInConflict(ICreature conflictedObject)
- {
- if (conflictedObject is Gold)
- {
- Game.Scores += 10;
- return false;
- }
- return conflictedObject is Sack || conflictedObject is Monster;
- }
- public int GetDrawingPriority() => 1;
- public string GetImageFileName() => "Digger.png";
- private bool IsStepPossible(int x, int y)
- {
- if (!(x >= 0 && x < Game.MapWidth && y >= 0 && y < Game.MapHeight))
- return false;
- return Game.Map[x, y] == null || !(Game.Map[x, y] is Sack);
- }
- }
- public class Sack : ICreature
- {
- private int counter;
- public CreatureCommand Act(int x, int y)
- {
- if (y < Game.MapHeight - 1)
- {
- var map = Game.Map[x, y + 1];
- if (map == null || counter > 0 && (map is Player || map is Monster))
- {
- counter++;
- return new CreatureCommand {DeltaX = 0, DeltaY = 1};
- }
- }
- if (counter > 1)
- return new CreatureCommand {DeltaX = 0, DeltaY = 0, TransformTo = new Gold()};
- counter = 0;
- return new CreatureCommand {DeltaX = 0, DeltaY = 0};
- }
- public bool DeadInConflict(ICreature conflictedObject) => false;
- public int GetDrawingPriority() => 5;
- public string GetImageFileName() => "Sack.png";
- }
- public class Gold : ICreature
- {
- public CreatureCommand Act(int x, int y) => new CreatureCommand {DeltaX = 0, DeltaY = 0};
- public bool DeadInConflict(ICreature conflictedObject) =>
- conflictedObject is Player || conflictedObject is Monster;
- public int GetDrawingPriority() => 3;
- public string GetImageFileName() => "Gold.png";
- }
- public class Monster : ICreature
- {
- public CreatureCommand Act(int x, int y)
- {
- var directionToPlayer = GetDirectionToPlayer(x, y);
- if (IsStepPossible(x + directionToPlayer.DX, y + directionToPlayer.DY))
- return new CreatureCommand {DeltaX = directionToPlayer.DX, DeltaY = directionToPlayer.DY};
- return new CreatureCommand();
- }
- public bool DeadInConflict(ICreature conflictedObject) =>
- conflictedObject is Sack || conflictedObject is Monster;
- public int GetDrawingPriority() => 0;
- public string GetImageFileName() => "Monster.png";
- private (int DX, int DY) GetDirectionToPlayer(int x, int y)
- {
- var dx = 0;
- var dy = 0;
- var playerPosition = GetPlayerPosition();
- if (playerPosition.X == x)
- {
- if (playerPosition.Y < y) dy = -1;
- else if (playerPosition.Y > y) dy = 1;
- }
- else if (playerPosition.Y == y)
- {
- if (playerPosition.X < x) dx = -1;
- else if (playerPosition.X > x) dx = 1;
- }
- else
- {
- if (playerPosition.X < x) dx = -1;
- else if (playerPosition.X > x) dx = 1;
- }
- return (dx, dy);
- }
- private (int X, int Y) GetPlayerPosition()
- {
- for (var x = 0; x < Game.MapWidth; x++)
- for (var y = 0; y < Game.MapHeight; y++)
- {
- if (Game.Map[x, y] is Player)
- return (x, y);
- }
- return (-1, -1);
- }
- private bool IsStepPossible(int x, int y)
- {
- if (!(x >= 0 && x < Game.MapWidth && y >= 0 && y < Game.MapHeight))
- return false;
- var block = Game.Map[x, y];
- return block == null || block is Player || block is Gold;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement