This repository was archived by the owner on May 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cs
More file actions
126 lines (103 loc) · 3.6 KB
/
Engine.cs
File metadata and controls
126 lines (103 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using roguelike.render;
using roguelike.player;
using System.Collections.Generic;
using roguelike.action;
using roguelike.ai;
using System;
using roguelike.core;
using roguelike.level;
namespace roguelike;
public class Engine : Game {
public GraphicsDeviceManager Graphics { get; }
public SpriteBatch SpriteBatch { get; private set; }
public Queue<GameAction> aiActions;
public Queue<GameAction> playerActions;
public AsciiTile[,] tiles = new AsciiTile[150, 60];
private AsciiTile[,] baseMapTiles;
public static AsciiTile[,] tileMap;
private Map map;
private MapGenerator generator;
private readonly Player player;
private readonly List<Monster> monsters;
private readonly List<Villager> villagers;
private readonly Pienus pienus;
private GameLoop gameLoop;
private KeyboardState _prevKeyboardState;
private readonly NameGenerator nameGenerator = new NameGenerator();
private Point cameraPosition = Point.Zero;
private const int ViewportWidthInTiles = 60;
private const int ViewportHeightInTiles = 40;
public Engine() {
Window.AllowUserResizing = true;
Window.ClientSizeChanged += new EventHandler<EventArgs>(WindowClientSizeChanged);
Graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
nameGenerator.InputFile("./Content/names.txt");
nameGenerator.ProcessFile();
player = new Player("Gamer", 100, 2, 2);
pienus = new Pienus("Pienus", 100, player.x - 1, player.y - 1);
playerActions = new Queue<GameAction>();
aiActions = new Queue<GameAction>();
monsters = new List<Monster>() {
new Monster("Mummy", 20, 4, 5),
new Monster("Mummy2", 20, 6, 8),
};
villagers = new List<Villager>() {
new Villager(nameGenerator.OutputName(), 20, 20, 10),
new Villager(nameGenerator.OutputName(), 20, 22, 15),
};
}
protected override void Initialize() {
generator = new MapGenerator();
tiles = generator.GenerateMap();
baseMapTiles = Map.CloneTiles(tiles);
tileMap = baseMapTiles;
gameLoop = new GameLoop();
GameLoop.instance = gameLoop;
gameLoop.AddActor(player);
gameLoop.AddActor(pienus);
foreach (Actor monster in monsters) {
gameLoop.AddActor(monster);
}
foreach (Actor villager in villagers) {
gameLoop.AddActor(villager);
}
base.Initialize();
}
protected override void LoadContent() {
SpriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Update(GameTime gameTime) {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
int halfWidth = ViewportWidthInTiles / 2;
int halfHeight = ViewportHeightInTiles / 2;
cameraPosition.X = Math.Clamp(player.x - halfWidth, 0, 150 - ViewportWidthInTiles);
cameraPosition.Y = Math.Clamp(player.y - halfHeight, 0, 60 - ViewportHeightInTiles);
var keyboardState = Keyboard.GetState();
player.SetInput(keyboardState, _prevKeyboardState);
_prevKeyboardState = keyboardState;
gameLoop.Process();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
base.Draw(gameTime);
tiles = Map.CloneTiles(baseMapTiles);
DrawActors();
map = new Map(tiles);
map.LoadContent(this);
map.Draw(gameTime, this, cameraPosition, ViewportWidthInTiles, ViewportHeightInTiles);
}
private void DrawActors() {
foreach (Actor actor in gameLoop.GetActors()) {
if (actor.x >= 0 && actor.x < 150 && actor.y >= 0 && actor.y < 60) {
tiles[actor.x, actor.y] = actor.tile;
}
}
}
private void WindowClientSizeChanged(object sender, EventArgs e) {}
}