Skip to content

Commit

Permalink
Add particle system
Browse files Browse the repository at this point in the history
Star collecting particles
  • Loading branch information
crt09 committed Mar 3, 2019
1 parent bdf9e68 commit 2fa39d5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
5 changes: 5 additions & 0 deletions ColorSwitch.Windows/Content/Content.mgcb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
/processorParam:TextureFormat=Compressed
/build:font.spritefont

#begin Particles/star_death.pex
/importer:ParticleDesignerImporter
/processor:ParticleDesignerProcessor
/build:Particles/star_death.pex

#begin Player/player_circle.png
/importer:TextureImporter
/processor:TextureProcessor
Expand Down
39 changes: 39 additions & 0 deletions ColorSwitch.Windows/Content/Particles/star_death.pex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<particleEmitterConfig>
<sourcePosition x="0" y="0"></sourcePosition>
<sourcePositionVariance x="20" y="20"></sourcePositionVariance>
<speed value="0"></speed>
<speedVariance value="0.00"></speedVariance>
<particleLifeSpan value="1.0"></particleLifeSpan>
<particleLifespanVariance value="0"></particleLifespanVariance>
<angle value="90"></angle>
<angleVariance value="0"></angleVariance>
<gravity x="40" y="0"></gravity>
<radialAcceleration value="0.00"></radialAcceleration>
<tangentialAcceleration value="0.00"></tangentialAcceleration>
<radialAccelVariance value="0.00"></radialAccelVariance>
<tangentialAccelVariance value="0.00"></tangentialAccelVariance>
<startColor red="1.00" green="1.00" blue="1.00" alpha="0.6"></startColor>
<startColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"></startColorVariance>
<finishColor red="1.00" green="1.00" blue="1.00" alpha="0.6"></finishColor>
<finishColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"></finishColorVariance>
<maxParticles value="8"></maxParticles>
<startParticleSize value="16.00"></startParticleSize>
<startParticleSizeVariance value="8"></startParticleSizeVariance>
<finishParticleSize value="8"></finishParticleSize>
<finishParticleSizeVariance value="0.00"></finishParticleSizeVariance>
<duration value="-1.00"></duration>
<emitterType value="0"></emitterType>
<maxRadius value="100"></maxRadius>
<maxRadiusVariance value="0"></maxRadiusVariance>
<minRadius value="0"></minRadius>
<minRadiusVariance value="0.00"></minRadiusVariance>
<rotatePerSecond value="0.00"></rotatePerSecond>
<rotatePerSecondVariance value="0.00"></rotatePerSecondVariance>
<blendFuncSource value="770"></blendFuncSource>
<blendFuncDestination value="1"></blendFuncDestination>
<rotationStart value="0.00"></rotationStart>
<rotationStartVariance value="0.00"></rotationStartVariance>
<rotationEnd value="0.00"></rotationEnd>
<rotationEndVariance value="0.00"></rotationEndVariance>
<texture name="../ColorEntities/star.png"/>
</particleEmitterConfig>
3 changes: 2 additions & 1 deletion ColorSwitch.Windows/GameCore/Components/PlayerPhysics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void HandleTouchableEntities(List<TouchableEntity> touchableEntities) {
}
}
foreach (var touchableEntity in touchableEntities) {
touchableEntity.SendState(entity);
if(touchableEntity.getComponent<Collider>() != null)
touchableEntity.SendState(entity);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ColorSwitch.Windows/GameCore/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static uint score {
}
private static GameText scoreText;

public List<TouchableEntity> touchableEntities;
public static List<TouchableEntity> touchableEntities;

private bool gameStarted = false;
private Texture2D playerTexture;
Expand Down
36 changes: 27 additions & 9 deletions ColorSwitch.Windows/GameCore/Entities/Special/Star.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Particles;
using Nez.Sprites;
using Nez.Tweens;

Expand All @@ -10,6 +11,8 @@ public class Star : TouchableEntity {

private Texture2D starTexture;
private Sprite starSprite;
private ParticleEmitterConfig particleConfig;
private float deathDuration = 0.7f;

public override Vector2 realSize => starTexture.Bounds.Size.ToVector2();

Expand All @@ -21,7 +24,10 @@ public override void onAddedToScene() {
addComponent(starSprite);

var collider = new CircleCollider(starTexture.Height / 2);
addComponent(collider);
addComponent(collider);

particleConfig = scene.content.Load<ParticleEmitterConfig>("Particles/star_death");
particleConfig.duration = deathDuration;
}

public override void SendState(Entity sender) {
Expand All @@ -35,26 +41,38 @@ public override void SendState(Entity sender) {
}
}

private new void destroy() {
float duration = 0.7f;
private new void destroy() {
showScore();
spawnParticles();
base.destroy();
}

private void showScore() {
var scoreText = new GameText("+1");
scoreText.position = position;
scene.addEntity(scoreText);

var moveTween = scoreText.tweenPositionTo(position + new Vector2(0, -40), duration);
var moveTween = scoreText.tweenPositionTo(position + new Vector2(0, -40), deathDuration);
moveTween.setEaseType(EaseType.QuadOut);
moveTween.start();

var opacityTween = new FloatTween(scoreText, 0f, duration - 0.3f);
var opacityTween = new FloatTween(scoreText, 0f, deathDuration - 0.3f);
opacityTween.setEaseType(EaseType.QuadOut);
opacityTween.setDelay(0.3f);
opacityTween.start();
Core.schedule(duration, t => {
opacityTween.start();

Core.schedule(deathDuration, t => {
if (scene != null) scoreText?.destroy();
});
base.destroy();
}

private void spawnParticles() {
var index = Player.touchableEntities.IndexOf(this) + 1;
var particles = Player.touchableEntities[index];

particleConfig.sourcePosition = position;
var particleEmitter = particles.addComponent(new ParticleEmitter(particleConfig));
particleEmitter.play();
}
}
}
3 changes: 1 addition & 2 deletions ColorSwitch.Windows/GameCore/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ColorSwitch.Windows.GameCore.Entities;
using ColorSwitch.Windows.GameCore.UiLib.Entities;
using Microsoft.Xna.Framework;
using ColorSwitch.Windows.GameCore.Helpers;

namespace ColorSwitch.Windows.GameCore.Scenes {
public class GameScene : Scene {
Expand All @@ -17,7 +16,7 @@ public override void initialize() {
player = new Player();
addEntity(player);

var entityBuilder = new EntityBuilder(player.touchableEntities);
var entityBuilder = new EntityBuilder(Player.touchableEntities);
addEntity(entityBuilder);

Player.score = 0;
Expand Down

0 comments on commit 2fa39d5

Please sign in to comment.