/
dashytka
/
Monster
Обзор
Документация
Войти
/
dashytka
/
Monster
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
c
260 строк
6 KB
dashytka
create c#
17 дек 2025, 17:04
17 дек 2025, 17:04
d74f667
Код
Авторство
О чём код?
using System; using System.Collections.Generic; using Avalonia.Input; using Digger.Architecture; namespace Digger; public class Terrain : ICreature { public CreatureCommand Act(int x, int y) { return new CreatureCommand(); } public bool DeadInConflict(ICreature other) { return other is Player; } public int GetDrawingPriority() { return 0; } public string GetImageFileName() { return "Terrain.png"; } } public class Player : ICreature { public CreatureCommand Act(int x, int y) { var cmd = new CreatureCommand(); if (Game.KeyPressed == Key.Left && CanGoTo(x - 1, y)) cmd.DeltaX = -1; else if (Game.KeyPressed == Key.Right && CanGoTo(x + 1, y)) cmd.DeltaX = 1; else if (Game.KeyPressed == Key.Up && CanGoTo(x, y - 1)) cmd.DeltaY = -1; else if (Game.KeyPressed == Key.Down && CanGoTo(x, y + 1)) cmd.DeltaY = 1; cmd.TransformTo = null; return cmd; } private bool CanGoTo(int targetX, int targetY) { if (targetX < 0 || targetX >= Game.MapWidth || targetY < 0 || targetY >= Game.MapHeight) return false; var cell = Game.Map[targetX, targetY]; return cell == null || !(cell is Sack); } public bool DeadInConflict(ICreature other) { return other is Sack || other is Monster; } public int GetDrawingPriority() { return 10; } public string GetImageFileName() { return "Digger.png"; } } public class Sack : ICreature { private int fallCount = 0; private bool falling = false; public CreatureCommand Act(int x, int y) { var cmd = new CreatureCommand(); if (ShouldFall(x, y)) { BeginFall(cmd); } else { EndFall(cmd); } return cmd; } private bool ShouldFall(int x, int y) { if (y + 1 >= Game.MapHeight) return false; var below = Game.Map[x, y + 1]; if (below == null) return true; return falling && (below is Player || below is Monster); } private void BeginFall(CreatureCommand cmd) { cmd.DeltaY = 1; fallCount++; falling = true; } private void EndFall(CreatureCommand cmd) { if (falling && fallCount > 1) { cmd.TransformTo = new Gold(); } falling = false; fallCount = 0; } public bool DeadInConflict(ICreature other) { return false; } public int GetDrawingPriority() { return 5; } public string GetImageFileName() { return "Sack.png"; } } public class Gold : ICreature { public CreatureCommand Act(int x, int y) { return new CreatureCommand(); } public bool DeadInConflict(ICreature other) { if (other is Player) { Game.Scores += 10; return true; } else if (other is Monster) { return true; } return false; } public int GetDrawingPriority() { return 7; } public string GetImageFileName() { return "Gold.png"; } } public class Monster : ICreature { public CreatureCommand Act(int x, int y) { var cmd = new CreatureCommand(); var diggerPos = LocateDigger(); if (diggerPos.HasValue) { var move = FindBestMove(x, y, diggerPos.Value); if (move.HasValue) { cmd.DeltaX = move.Value.X - x; cmd.DeltaY = move.Value.Y - y; } } return cmd; } private (int X, int Y)? LocateDigger() { for (var i = 0; i < Game.MapWidth; i++) { for (var j = 0; j < Game.MapHeight; j++) { if (Game.Map[i, j] is Player) { return (i, j); } } } return null; } private (int X, int Y)? FindBestMove(int mx, int my, (int X, int Y) target) { var options = new List<(int, int)>(); if (target.X > mx && IsAccessible(mx + 1, my)) options.Add((mx + 1, my)); else if (target.X < mx && IsAccessible(mx - 1, my)) options.Add((mx - 1, my)); if (target.Y > my && IsAccessible(mx, my + 1)) options.Add((mx, my + 1)); else if (target.Y < my && IsAccessible(mx, my - 1)) options.Add((mx, my - 1)); foreach (var option in options) { var cell = Game.Map[option.Item1, option.Item2]; if (cell == null || !(cell is Monster)) { return option; } } return null; } private bool IsAccessible(int tx, int ty) { if (tx < 0 || tx >= Game.MapWidth || ty < 0 || ty >= Game.MapHeight) return false; var cell = Game.Map[tx, ty]; return cell == null || cell is Player || cell is Gold || cell is Monster; } public bool DeadInConflict(ICreature other) { return other is Sack || other is Monster; } public int GetDrawingPriority() { return 8; } public string GetImageFileName() { return "Monster.png"; } }