-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
158 lines (119 loc) · 4.83 KB
/
Game1.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using System;
namespace Woods
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private World world;
private Camera cam;
private Player player;
public static readonly int GridSize = 16;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
world = new World(16);
ToggleBorderless();
cam = new Camera(GraphicsDevice.Viewport);
player = new Player(new Vector2(9, 9), world);
cam.Track(player);
world.entities.Add(player);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
Player.LoadSpriteSheet(LoadTex2D(Content, "player"));
Player.craftingRecipes = Content.Load<CraftingRecipe[]>("crafting");
world.SetContent(LoadTex2D(Content, "blocks"), LoadTex2D(Content, "bases"), Content.Load<Block[]>("block-data"));
Tile.SetContent(LoadTex2D(Content, "damage"));
player.Start();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (gameTime.IsRunningSlowly)
Console.WriteLine("Slow");
player.Update(cam.Transform, gameTime);
cam.UpdateCamera(GraphicsDevice.Viewport);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin(blendState: BlendState.NonPremultiplied, transformMatrix: cam.Transform, samplerState: SamplerState.PointClamp);
//world.LightUpdate();
world.Draw(_spriteBatch, cam.AdjustedVisibleArea);
foreach (Entity e in world.entities) {
Vector2 entityPosition = e.GetPosition();
e.Draw(_spriteBatch, cam.AdjustedVisibleArea, world.GetTile((int)entityPosition.X, (int)entityPosition.Y).lightLevel);
}
player.DrawCarrying(_spriteBatch);
_spriteBatch.Draw(Content.Load<Texture2D>("cursor"), player.cursorPosition, null, Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
_spriteBatch.End();
base.Draw(gameTime);
}
private static Texture2D LoadTex2D(ContentManager c, string assetName) {
return c.Load<Texture2D>(assetName);
}
bool _isFullscreen = false;
bool _isBorderless = false;
int _width = 0;
int _height = 0;
public void ToggleFullscreen() {
bool oldIsFullscreen = _isFullscreen;
if (_isBorderless) {
_isBorderless = false;
} else {
_isFullscreen = !_isFullscreen;
}
ApplyFullscreenChange(oldIsFullscreen);
}
public void ToggleBorderless() {
bool oldIsFullscreen = _isFullscreen;
_isBorderless = !_isBorderless;
_isFullscreen = _isBorderless;
ApplyFullscreenChange(oldIsFullscreen);
}
private void ApplyFullscreenChange(bool oldIsFullscreen) {
if (_isFullscreen) {
if (oldIsFullscreen) {
ApplyHardwareMode();
} else {
SetFullscreen();
}
} else {
UnsetFullscreen();
}
}
private void ApplyHardwareMode() {
_graphics.HardwareModeSwitch = !_isBorderless;
_graphics.ApplyChanges();
}
private void SetFullscreen() {
_width = Window.ClientBounds.Width;
_height = Window.ClientBounds.Height;
_graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
_graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
_graphics.HardwareModeSwitch = !_isBorderless;
_graphics.IsFullScreen = true;
_graphics.ApplyChanges();
}
private void UnsetFullscreen() {
_graphics.PreferredBackBufferWidth = _width;
_graphics.PreferredBackBufferHeight = _height;
_graphics.IsFullScreen = false;
_graphics.ApplyChanges();
}
}
}