-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
175 lines (156 loc) · 6.04 KB
/
Main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Slip
{
public class Main : Game
{
internal static Main instance;
public static Random rand = new Random();
internal GraphicsDeviceManager graphics;
internal SpriteBatch spriteBatch;
private Screen screen;
public Screen changeScreen = null;
private static Keys[] controlsToKeys = new Keys[(int)KeyControl.Count];
private static bool[] controlPress = new bool[(int)KeyControl.Count];
private static bool[] controlHold = new bool[(int)KeyControl.Count];
private static bool[] controlRelease = new bool[(int)KeyControl.Count];
public static Point mousePos;
public static Point mouseMove;
public static bool leftMouseClick;
public static bool rightMouseClick;
public static bool middleMouseClick;
public static bool leftMousePress;
public static bool rightMousePress;
public static bool middleMousePress;
public static bool leftMouseRelease;
public static bool rightMouseRelease;
public static bool middleMouseRelease;
public static int mouseWheel;
public static int mouseScrollSpeed;
public Main()
{
instance = this;
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1000;
graphics.PreferredBackBufferHeight = 600;
this.IsMouseVisible = true;
Content.RootDirectory = "Content";
SetupDefaultControls();
}
protected override void Initialize()
{
base.Initialize();
ChangeScreen(new MainMenu());
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
Content.Unload();
}
protected override void Update(GameTime gameTime)
{
if (IsControlPressed(KeyControl.Escape))
{
if (screen is MainMenu)
{
base.Exit();
return;
}
ChangeScreen(new MainMenu());
}
UpdateKeyState();
UpdateMouseState();
screen.Update(this);
if (changeScreen != null)
{
ChangeScreen(changeScreen);
changeScreen = null;
}
base.Update(gameTime);
}
private static void SetupDefaultControls()
{
MapKey(KeyControl.Escape, Keys.Escape);
MapKey(KeyControl.Up, Keys.Up);
MapKey(KeyControl.Left, Keys.Left);
MapKey(KeyControl.Down, Keys.Down);
MapKey(KeyControl.Right, Keys.Right);
MapKey(KeyControl.Action, Keys.Z);
MapKey(KeyControl.Focus, Keys.LeftShift);
MapKey(KeyControl.Debug, Keys.I);
}
public static void MapKey(KeyControl control, Keys key)
{
controlsToKeys[(int)control] = key;
}
private static void UpdateKeyState()
{
for (int k = 0; k < (int)KeyControl.Count; k++)
{
bool keyDown = Keyboard.GetState().IsKeyDown(controlsToKeys[k]);
bool keyUp = Keyboard.GetState().IsKeyUp(controlsToKeys[k]);
controlPress[k] = !controlHold[k] && keyDown;
controlRelease[k] = controlHold[k] && keyUp;
controlHold[k] = keyDown;
}
}
public static bool IsControlPressed(KeyControl control)
{
return controlPress[(int)control];
}
public static bool IsControlHeld(KeyControl control)
{
return controlHold[(int)control];
}
public static bool IsControlReleased(KeyControl control)
{
return controlRelease[(int)control];
}
private static void UpdateMouseState()
{
mouseMove = Mouse.GetState().Position - mousePos;
mousePos = Mouse.GetState().Position;
leftMouseClick = !leftMousePress && Mouse.GetState().LeftButton == ButtonState.Pressed;
leftMouseRelease = leftMousePress && Mouse.GetState().LeftButton == ButtonState.Released;
leftMousePress = Mouse.GetState().LeftButton == ButtonState.Pressed;
rightMouseClick = !rightMousePress && Mouse.GetState().RightButton == ButtonState.Pressed;
rightMouseRelease = rightMousePress && Mouse.GetState().RightButton == ButtonState.Released;
rightMousePress = Mouse.GetState().RightButton == ButtonState.Pressed;
middleMouseClick = !middleMousePress && Mouse.GetState().MiddleButton == ButtonState.Pressed;
middleMouseRelease = middleMousePress && Mouse.GetState().MiddleButton == ButtonState.Released;
middleMousePress = Mouse.GetState().MiddleButton == ButtonState.Pressed;
mouseScrollSpeed = Mouse.GetState().ScrollWheelValue - mouseWheel;
mouseWheel = Mouse.GetState().ScrollWheelValue;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(screen.BackgroundColor());
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap);
screen.Draw(this);
spriteBatch.End();
base.Draw(gameTime);
}
public Vector2 Size()
{
return new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
}
public Vector2 Center()
{
return Size() / 2f;
}
private void ChangeScreen(Screen newScreen)
{
Content.Unload();
Textures.LoadContent(Content);
screen = newScreen;
newScreen.LoadContent(Content);
newScreen.Initialize(this);
}
}
}