Skip to content

Commit 6be2bd7

Browse files
committed
added shrapnel particles
1 parent 85a1e24 commit 6be2bd7

File tree

12 files changed

+118
-32
lines changed

12 files changed

+118
-32
lines changed

Common/IImage.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface IImage
1515
void Draw(byte[] buffer);
1616
IImage Scale(float scale);
1717
IImage Rotate(float angle);
18-
IImage Fade(byte fade);
18+
IImage Fade(float fade);
1919
}
2020
}

MapEngine/Content/Particles/Explosion1.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"SpawnOffset": 5,
1616
"SpawnCount": 5,
1717
"InitialSize": 0.25,
18-
"PaletteTextureId": "ExplosionPalette"
18+
"PaletteTextureId": "ExplosionPalette",
19+
"PalleteSpeed": 40
1920
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Type": "Flash1",
3+
"TextureIds": [
4+
"Flash1"
5+
],
6+
"SpawnRate": 0,
7+
"Lifetime": 100000,
8+
"FadeRate": 40,
9+
"MinInitialRotation": -90,
10+
"MaxInitialRotation": 90,
11+
"SpawnCount": 1,
12+
"InitialSize": 0.5,
13+
"InitialFade": 0
14+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Type": "Shrapnel1",
3+
"TextureIds": [
4+
"projectile"
5+
],
6+
"Lifetime": 2000,
7+
"MinInitialRotation": 0,
8+
"MaxInitialRotation": 0,
9+
"SpawnCount": 30,
10+
"InitialSize": 1,
11+
"InitialFade": 0,
12+
"InitialVelocity": 5,
13+
"InitialSize": 0.7,
14+
"PaletteTextureId": "ExplosionPalette",
15+
"PalleteSpeed": 5
16+
}

MapEngine/Content/Textures/Flash1.png

12 KB
Loading

MapEngine/Entities/Components/ParticleComponent.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public class ParticleComponent : IComponent
1818
public int SpawnOffset { get; set; }
1919
public int? SpawnCount { get; set; }
2020
public float InitialSize { get; set; }
21+
public float InitialFade { get; set; }
22+
public int InitialVelocity { get; set; }
2123
public string PaletteTextureId { get; set; }
24+
public int PalleteSpeed { get; set; }
2225

2326
public IComponent Clone()
2427
{
@@ -37,7 +40,10 @@ public IComponent Clone()
3740
SpawnOffset = SpawnOffset,
3841
SpawnCount = SpawnCount,
3942
InitialSize = InitialSize,
40-
PaletteTextureId = PaletteTextureId
43+
InitialFade = InitialFade,
44+
InitialVelocity = InitialVelocity,
45+
PaletteTextureId = PaletteTextureId,
46+
PalleteSpeed = PalleteSpeed
4147
};
4248
}
4349
}

MapEngine/Handlers/CollisionHandler.cs

+52-20
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,58 @@ private void HandleCollisions(Entity entity)
7171
selfVelocity.Velocity = newSelfVelocity;
7272
}
7373

74-
//if (force > collider.MaxImpactForce)
75-
//{
76-
// ParticleFactory.TryGetParticle("Explosion1", out var particle);
77-
// var location = entity.GetComponent<LocationComponent>();
78-
// _messageHub.Post(new CreateEntityCommand
79-
// {
80-
// Entity = new Entity
81-
// {
82-
// Components = new List<IComponent>
83-
// {
84-
// particle,
85-
// new LocationComponent
86-
// {
87-
// Location = location.Location
88-
// }
89-
// }
90-
// }
91-
// });
92-
// _messageHub.Post(new DestroyEntityCommand { Entity = entity });
93-
//}
74+
if (force > collider.MaxImpactForce)
75+
{
76+
var location = entity.GetComponent<LocationComponent>();
77+
ParticleFactory.TryGetParticle("Flash1", out var particle1);
78+
_messageHub.Post(new CreateEntityCommand
79+
{
80+
Entity = new Entity
81+
{
82+
Components = new List<IComponent>
83+
{
84+
particle1,
85+
new LocationComponent
86+
{
87+
Location = location.Location
88+
}
89+
}
90+
}
91+
});
92+
93+
ParticleFactory.TryGetParticle("Shrapnel1", out var particle3);
94+
_messageHub.Post(new CreateEntityCommand
95+
{
96+
Entity = new Entity
97+
{
98+
Components = new List<IComponent>
99+
{
100+
particle3,
101+
new LocationComponent
102+
{
103+
Location = location.Location
104+
}
105+
}
106+
}
107+
});
108+
109+
ParticleFactory.TryGetParticle("Explosion1", out var particle2);
110+
_messageHub.Post(new CreateEntityCommand
111+
{
112+
Entity = new Entity
113+
{
114+
Components = new List<IComponent>
115+
{
116+
particle2,
117+
new LocationComponent
118+
{
119+
Location = location.Location
120+
}
121+
}
122+
}
123+
});
124+
_messageHub.Post(new DestroyEntityCommand { Entity = entity });
125+
}
94126
}
95127
}
96128
}

MapEngine/Handlers/ParticleHandler/Particle.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ namespace MapEngine.Handlers.ParticleHandler
55
public class Particle
66
{
77
public Vector2 Location { get; set; }
8+
public Vector2 Velocity { get; set; }
89
public string TextureId { get; set; }
910
public float Lifetime { get; set; }
1011
public float FacingAngle { get; set; }
11-
public byte Fade { get; set; }
12+
public float Fade { get; set; }
1213
public float Size { get; set; }
1314
public int HueIndex { get; set; }
1415
public string PaletteTextureId { get; set; }
16+
public int PalleteSpeed { get; set; }
1517
}
1618
}

MapEngine/Handlers/ParticleHandler/ParticleEmitter.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,25 @@ public void Update()
3737
// Check initial conditions emission
3838
while (ShouldEmit(elapsed, movementComponent, particleComponent))
3939
{
40+
4041
// Spawn new particles
4142
_previousSpawn = DateTime.Now;
43+
var velocity = GetVelocity(rng, particleComponent);
4244
var rotation = GetRotation(rng, locationComponent, particleComponent);
4345
var location = GetLocation(rng, locationComponent, particleComponent);
4446
var textureId = particleComponent.TextureIds[rng.Next(particleComponent.TextureIds.Length)];
4547

4648
_particles.Add(new Particle
4749
{
4850
Location = location,
51+
Velocity = velocity,
4952
Lifetime = 0,
50-
Fade = 0,
53+
Fade = particleComponent.InitialFade,
5154
Size = particleComponent.InitialSize,
5255
TextureId = textureId,
5356
FacingAngle = rotation,
5457
PaletteTextureId = particleComponent.PaletteTextureId,
58+
PalleteSpeed = particleComponent.PalleteSpeed,
5559
});
5660
}
5761

@@ -60,6 +64,7 @@ public void Update()
6064
{
6165
p.Lifetime += (float)elapsed;
6266
p.Size *= particleComponent.GrowRate;
67+
p.Location += p.Velocity;
6368

6469
if (p.Lifetime > particleComponent.FadeDelay)
6570
p.Fade = (byte) Math.Min(255, p.Fade + particleComponent.FadeRate); // todo: this should be timebased
@@ -94,7 +99,7 @@ public void Draw(Rectangle viewport, IGraphics graphics)
9499
if (TextureFactory.TryGetTexture(p.PaletteTextureId, out var palette))
95100
{
96101
// todo: change this to stay on the last colour?
97-
var hue = palette.Image.GetPalette(p.HueIndex++, 40);
102+
var hue = palette.Image.GetPalette(p.HueIndex++, p.PalleteSpeed);
98103
rotated.ChangeHue(hue);
99104
}
100105

@@ -106,6 +111,13 @@ public void Draw(Rectangle viewport, IGraphics graphics)
106111
}
107112
}
108113

114+
private static Vector2 GetVelocity(Random rng, ParticleComponent particleComponent)
115+
{
116+
var jitterX = rng.Next(-particleComponent.InitialVelocity, particleComponent.InitialVelocity);
117+
var jitterY = rng.Next(-particleComponent.InitialVelocity, particleComponent.InitialVelocity);
118+
return new Vector2(jitterX, jitterY);
119+
}
120+
109121
private static Vector2 GetLocation(Random rng, LocationComponent locationComponent, ParticleComponent particleComponent)
110122
{
111123
var jitterX = rng.Next(-particleComponent.SpawnOffset, particleComponent.SpawnOffset);

MapEngine/ResourceLoading/ParticleLoader.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ public static ParticleComponent LoadParticleDefinition(string filename)
2121
{
2222
ParticleType = particleData.Type,
2323
TextureIds = textureIds,
24-
SpawnRate = particleData.SpawnRate,
24+
SpawnRate = particleData?.SpawnRate ?? 0,
2525
MinVelocity = particleData.MinVelocity,
2626
Lifetime = particleData.Lifetime,
2727
FadeDelay = particleData?.FadeDelay ?? 0,
28-
FadeRate = particleData.FadeRate,
28+
FadeRate = particleData?.FadeRate ?? 0,
2929
MinInitialRotation = particleData.MinInitialRotation,
3030
MaxInitialRotation = particleData.MaxInitialRotation,
31-
GrowRate = particleData.GrowRate,
31+
GrowRate = particleData?.GrowRate ?? 1,
3232
SpawnOffset = particleData?.SpawnOffset ?? 0,
3333
SpawnCount = particleData.SpawnCount,
3434
InitialSize = particleData.InitialSize,
35-
PaletteTextureId = particleData.PaletteTextureId
35+
InitialFade = particleData?.InitialFade ?? 0,
36+
InitialVelocity = particleData?.InitialVelocity ?? 0,
37+
PaletteTextureId = particleData.PaletteTextureId,
38+
PalleteSpeed = particleData?.PalleteSpeed ?? 1
3639
};
3740

3841
return particle;

MapEngine/WpfImage.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public IImage Scale(float scale)
5757
return new WpfImage(scaled);
5858
}
5959

60-
public IImage Fade(byte fade)
60+
public IImage Fade(float fade)
6161
{
6262
for (int i = 3; i < Buffer.Length; i += 4)
6363
{

SoftEngine/Texture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public IImage Rotate(float angle)
6565
throw new NotImplementedException();
6666
}
6767

68-
public IImage Fade(byte fade)
68+
public IImage Fade(float fade)
6969
{
7070
throw new NotImplementedException();
7171
}

0 commit comments

Comments
 (0)