-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRpgGame.cs
103 lines (91 loc) · 3.27 KB
/
RpgGame.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
using Comora;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace RPG;
public class RpgGame : Game
{
private readonly GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private GameSprites _sprites;
private State _state;
public RpgGame()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
SetUpCanvasSize();
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_sprites = CreateGameSprites();
_state = new(
camera: new(_graphics.GraphicsDevice),
player: new(LoadPlayerAnimation()),
projectileFactory: new(_sprites.Ball),
enemyController: new(
factory: new(Content),
positionGenerator: new(
canvasSize: new(
_graphics.PreferredBackBufferWidth,
_graphics.PreferredBackBufferHeight
),
enemyRadius: _sprites.Skull.Bounds.Width / 2
)
)
);
Sounds.projectileSound = Content.Load<SoundEffect>("Sounds/blip");
Sounds.backgroundMusic = Content.Load<Song>("Sounds/nature");
MediaPlayer.Play(Sounds.backgroundMusic);
}
protected override void Update(GameTime gameTime)
{
ExitIfRequired();
_state.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
_spriteBatch.Begin(_state.Camera);
_spriteBatch.Draw(_sprites.Background, new Vector2(-500, -500), Color.White);
_state.Draw(_spriteBatch);
_spriteBatch.End();
base.Draw(gameTime);
}
private void SetUpCanvasSize()
{
_graphics.PreferredBackBufferWidth = 1280;
_graphics.PreferredBackBufferHeight = 720;
_graphics.ApplyChanges();
}
private GameSprites CreateGameSprites() => new(
player: CreatePlayerSprites(),
background: Content.Load<Texture2D>("background"),
ball: Content.Load<Texture2D>("ball"),
skull: Content.Load<Texture2D>("skull")
);
private PlayerSprites CreatePlayerSprites() => new(
player: Content.Load<Texture2D>("Player/player"),
walkDown: Content.Load<Texture2D>("Player/walkDown"),
walkUp: Content.Load<Texture2D>("Player/walkUp"),
walkRight: Content.Load<Texture2D>("Player/walkRight"),
walkLeft: Content.Load<Texture2D>("Player/walkLeft")
);
private Animation LoadPlayerAnimation() => new(
moveDown: AnimationLoader.LoadPlayer(Content, "Player/walkDown"),
moveUp: AnimationLoader.LoadPlayer(Content, "Player/walkUp"),
moveRight: AnimationLoader.LoadPlayer(Content, "Player/walkRight"),
moveLeft: AnimationLoader.LoadPlayer(Content, "Player/walkLeft")
);
private void ExitIfRequired()
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
}
}