-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.cs
311 lines (241 loc) · 7.87 KB
/
Scene.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StereoGame.Hitbox;
using System.Collections.ObjectModel;
using StereoGame.Entities;
namespace StereoGame
{
public class Scene : Entity
{
private const int DebugHitboxesThickness = 2;
private List<Entity> regularEntitiesList;
private List<CollisionEntity> collisionEntitiesList;
public ReadOnlyCollection<Entity> RegularEntitiesList { get; private set; }
public ReadOnlyCollection<CollisionEntity> CollisionEntitiesList { get; private set; }
public bool DebugModeEnabled { get; set; } = true;
public bool ShowHitboxes { get; set; } = true;
public float TimeElapsed { get; private set; } = 0f;
///<summary>
///The constructor, only creates the basic fields, does NOT load the entities
///</summary>
public Scene()
{
regularEntitiesList = new List<Entity>();
collisionEntitiesList = new List<CollisionEntity>();
RegularEntitiesList = new ReadOnlyCollection<Entity>(regularEntitiesList);
CollisionEntitiesList = new ReadOnlyCollection<CollisionEntity>(collisionEntitiesList);
}
/// <summary>
/// The function for creating all of the entities of the scene
/// </summary>
public virtual void Load()
{
//set some hardcoded mappings
InputHandler.CurrentHandler.AddAction("DebugToggle", new() { Keys.F });
}
/// <summary>
/// Adds a NON COLLISION entity to its proper list to be updated
/// </summary>
/// <param name="entity"></param>
public void AddEntity(Entity entity)
{
if (entity is not null)
{
entity.EntityEventCaller += HandleEntityEvent;
regularEntitiesList.Add(entity);
}
}
public void AddCollisionEntity(CollisionEntity entity)
{
entity.EntityEventCaller += HandleEntityEvent;
collisionEntitiesList.Add(entity);
}
public void RemoveEntity(Entity entity)
{
if (entity is CollisionEntity)
{
collisionEntitiesList.Remove(entity as CollisionEntity);
}
else
{
regularEntitiesList.Remove(entity);
}
}
public float GetTimeElapsed()
{
return TimeElapsed;
}
public new void RestartScene()
{
regularEntitiesList.Clear();
collisionEntitiesList.Clear();
Load();
}
// For now, this is only used to handle entities being able to destroy themselves
public void HandleEntityEvent(Entity sender, EntityEvent eventType){
switch (eventType)
{
case (EntityEvent.SELF_DESTRUCT):
RemoveEntity(sender);
break;
case (EntityEvent.RESTART_SCENE):
RestartScene();
break;
}
}
/// <summary>
/// Before custom update : logic common to all scenes that can't be gotten rid of
/// </summary>
/// <param name="gameTime"></param>
protected sealed override void PreUpdate(GameTime gameTime)
{
//update the input handler
InputHandler.CurrentHandler.Update();
//if debug enabled, allow the user to use debug keys
if (DebugModeEnabled && InputHandler.CurrentHandler.IsActionJustDown("DebugToggle"))
{
ShowHitboxes ^= true;
}
}
private void UpdateAllEntities(GameTime gameTime)
{
//updating all entities
foreach (Entity entity in regularEntitiesList)
{
//adapt the time scale for each entity ?
//(probably figure out something better here imo)
if (entity.TimeScale == 1f)
{
entity.Update(gameTime);
}
else
{
entity.Update(new GameTime(gameTime.TotalGameTime, gameTime.ElapsedGameTime * entity.TimeScale));
}
}
foreach (Entity collisionEntity in collisionEntitiesList)
{
if (collisionEntity.TimeScale == 1f)
{
collisionEntity.Update(gameTime);
}
else
{
collisionEntity.Update(new GameTime(gameTime.TotalGameTime, gameTime.ElapsedGameTime * collisionEntity.TimeScale));
}
}
}
/// <summary>
/// After custom update : update all of the entities, correct and notify entities of collisions
/// </summary>
/// <param name="gameTime">You know what GameTime is already</param>
protected sealed override void PostUpdate(GameTime gameTime)
{
//update timer
TimeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
//updating scene entities
UpdateAllEntities(gameTime);
//detecting then correcting collisions
for (int i = 0; i < collisionEntitiesList.Count-1; i++)
{
CollisionEntity e1 = collisionEntitiesList[i];
for (int j = i+1; j< collisionEntitiesList.Count; j++)
{
CollisionEntity e2 = collisionEntitiesList[j];
//start by checking for grid coordinate collision (cheapest check)
if (((e1.GridXPos & e2.GridXPos) | (e1.GridYPos & e2.GridYPos)) == 0u)
{
continue;
}
//next, check for bounding box collision
if (e1.GetHitbox().GetBoundingBox().Intersects(e2.GetHitbox().GetBoundingBox()))
{
//don't care about collisions between two static objects
if (e1.CollisionWeight.Equals(Double.PositiveInfinity) && e2.CollisionWeight.Equals(Double.PositiveInfinity))
{
return;
}
//perform the proper collision check routine
Vector2 penetrationVector = e1.GetHitbox().SolveCollision(e2.GetHitbox());
if (penetrationVector != Vector2.Zero)
{
//call the respective events
e1.OnCollision(e2);
e2.OnCollision(e1);
//compute which percentage of the displacement each entity will do
float w1 = e1.CollisionWeight;
float w2 = e2.CollisionWeight;
float normalConstant = (1 / w1) + (1 / w2);
float e1MoveIntensity = (1 / w1) / normalConstant;
float e2MoveIntensity = (1 / w2) / normalConstant;
e1.ShiftPosition(penetrationVector * e1MoveIntensity);
e2.ShiftPosition(-penetrationVector * e2MoveIntensity);
}
}
}
}
}
public virtual void CustomDraw(SpriteBatch spriteBatch)
{
}
/// <summary>
/// Draw all entities + handle debug display
/// </summary>
/// <param name="spriteBatch"></param>
public sealed override void Draw(SpriteBatch spriteBatch)
{
//drawing all held entities
foreach (Entity entity in regularEntitiesList)
{
if (entity.IsVisible)
{
entity.Draw(spriteBatch);
}
}
//call the scene's custom draw method if applicable
CustomDraw(spriteBatch);
//draw collision entities + hitboxes if needed
foreach (CollisionEntity collisionEntity in collisionEntitiesList)
{
collisionEntity.Draw(spriteBatch);
if (ShowHitboxes)
{
//draw the hitbox one depth step over the entity
IHitbox curHitbox = collisionEntity.GetHitbox();
if (curHitbox is not null)
{
if (curHitbox is RectangleHitbox){
SpritedEntity.HRectangleDraw(spriteBatch, curHitbox.GetBoundingBox(),
DebugHitboxesThickness, Color.LawnGreen,
SpritedEntity.DepthLayer(100));
}
else if (curHitbox is CircleHitbox circHitbox)
{
SpritedEntity.CircleDraw(spriteBatch, new Vector2(circHitbox.X, circHitbox.Y),
circHitbox.Radius, DebugHitboxesThickness, Color.LawnGreen,
SpritedEntity.DepthLayer(100));
}
else if (curHitbox is ConvexPHitbox polygon)
{
int numVertices = polygon.Vertices.Count;
for (int i = 0; i<numVertices; i++)
{
SpritedEntity.LineDraw(spriteBatch, polygon.Vertices[i], polygon.Vertices[(i + 1) % numVertices],
DebugHitboxesThickness, Color.LawnGreen, SpritedEntity.DepthLayer(100));
}
}
}
}
}
}
}
}