-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
128 lines (104 loc) · 4.24 KB
/
Game.cs
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
127
128
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ZooBurst.Core.Levels;
using ZooBurst.View.Graphics;
using ZooBurst.Core;
using ZooBurst.Core.Input;
using ZooBurst.Utils;
using ZooBurst.View;
using ZooBurst.View.Fonts;
namespace ZooBurst
{
internal class Game : Microsoft.Xna.Framework.Game
{
public GraphicsDeviceManager Graphics { get; set; }
public SpriteBatch SpriteBatch { get; set; }
private Controller _controller;
private SpriteBitmapFont _font;
public Game()
{
Graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
Graphics.PreferredBackBufferWidth = 800;
Graphics.PreferredBackBufferHeight = 800;
Graphics.SynchronizeWithVerticalRetrace = false;
}
protected override void Initialize()
{
base.Initialize();
var mersenneTwister = new MersenneTwister();
var loader = new LevelLoader();
var firstLevel = new Level(loader.Load("Content/first"), mersenneTwister);
var view = new GameView(new Point(Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight), new Point(64, 64), firstLevel);
_controller = new Controller(firstLevel, view);
var input = new MouseInputHandler(_controller, view);
input.Swap += _controller.SwapAnimals;
}
protected override void LoadContent()
{
SpriteBatch = new SpriteBatch(GraphicsDevice);
Assets.Initialize(GraphicsDevice, Content.RootDirectory);
Assets.RegisterTextureCollection("animals", new[]
{
"monkey.png",
"parrot.png",
"penguin.png",
"pig.png",
"snake.png",
"rabbit.png",
"giraffe.png"
},
true);
Assets.RegisterTexture("starburst_1024_blue.png", "background");
Assets.RegisterFont("Content/Fonts/dsfont.fnt", "dsfont");
_font = Assets.GetFont("dsfont");
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
InputManager.Update(gameTime);
_controller.Update(delta);
base.Update(gameTime);
}
private void DrawText(string text, Vector2 position)
{
SpriteBatch.DrawString(_font, text, new Vector2((int)position.X, (int)position.Y + 1), Color.Black);
SpriteBatch.DrawString(_font, text, new Vector2((int)position.X, (int)position.Y), Color.White);
}
private void DrawFullscreenMessage(SpriteBatch spriteBatch, string text)
{
spriteBatch.DrawRectangle(new Rectangle(0, 0, _controller.View.ViewSize.X, _controller.View.ViewSize.Y), Color.Black * 0.75F);
var textSize = _font.Measure(text);
DrawText(text, new Vector2((int)((_controller.View.ViewSize.X / 2.0F) - (textSize.X / 2.0F)),
(int)((_controller.View.ViewSize.Y / 2.0F) - textSize.Y)));
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin();
_controller.View.Draw(gameTime, SpriteBatch);
DrawText($"{_controller.MovesLeft} moves left", new Vector2(6, 6));
DrawText($"{_controller.Score}/{_controller.Level.TargetScore} points", new Vector2(6, 18));
switch (_controller.State)
{
case PlayState.Success:
DrawFullscreenMessage(SpriteBatch, "You won! Press Enter to play again.");
break;
case PlayState.Failure:
DrawFullscreenMessage(SpriteBatch, "You lost... Press Enter to try again.");
break;
}
SpriteBatch.End();
base.Draw(gameTime);
}
internal static void Main(string[] args)
{
using (var game = new Game())
{
game.Run();
}
}
}
}