Skip to content

Instantly share code, notes, and snippets.

@Purexo
Created February 12, 2016 10:09
Show Gist options
  • Save Purexo/4c98527ab3c37258579d to your computer and use it in GitHub Desktop.
Save Purexo/4c98527ab3c37258579d to your computer and use it in GitHub Desktop.
Superpowers-script-ModuleParticule
module Particule {
class Color extends Sup.Color {
static random () {
let Random = Sup.Math.Random;
return new Color(
Random.integer(0, 100) / 100,
Random.integer(0, 100) / 100,
Random.integer(0, 100) / 100
);
}
}
class Borne {
min : any;
max : any;
constructor(min : any, max : any) {
this.min = min;
this.max = max;
}
}
interface ContructorParticule {
scale ? : Sup.Math.XYZ;
color ? : Color;
rotation ? : Sup.Math.XYZ;
dureeMax ? : number;
sprite ? : Sup.Sprite;
opacity ? : number;
}
interface ConstructorActor {
name: string;
parent?: Sup.Actor;
options?: { visible?: boolean; layer?: number };
}
export class Particule extends Sup.Actor {
private static get MAXNUMBER() { return 1000 };
private static number = 0;
private scale : Sup.Math.XYZ;
private color : Color;
// private direction : Sup.Math.Vector3;
private rotation : Sup.Math.XYZ;
private dureeMax : number;
public get DUREEMAX() {return this.dureeMax;}
private duree : number;
private sprite : Sup.Sprite;
private opacity : number;
constructor (a : ConstructorActor, p : ContructorParticule) {
super(a.name, a.parent, a.options);
Particule.number++;
if (Particule.number > Particule.MAXNUMBER)
this.destroy();
// Initialisation de l'état de la particule
this.dureeMax = p.dureeMax ? p.dureeMax : 60; // une paticule dure 1 seconde par défaut
this.duree = 0;
this.rotation = p.rotation ? p.rotation : {x:0, y:0, z:0}; // pas de otation par défaut
this.scale = p.scale ? p.scale : {x:1, y:1, z:1}; // scale de 1 par défaut
this.sprite = p.sprite ? p.sprite : Sup.get('Assets/Design/Particules/pixel', Sup.Sprite); // sprite de pixel appliqué par défaut
this.color = p.color;
this.opacity = p.opacity ? p.opacity : 1;
// application des états
this.setLocalScale(this.scale);
this.spriteRenderer = new Sup.SpriteRenderer(this, this.sprite);
this.spriteRenderer.setColor(this.color);
this.setEulerAngles(this.rotation);
this.spriteRenderer.setOpacity(this.opacity);
}
tick() {
this.duree++;
if (this.duree > this.DUREEMAX) {
this.destroy();
}
}
}
export class BehaviorParticle extends Sup.Behavior {
// toute ces variables seront utilisé seulement si elle sont initialisé, pas de valeur par défaut
public deltaPos : Sup.Math.XYZ;
public toScale : Sup.Math.XYZ;
public toColor : Color;
public toRotation : Sup.Math.XYZ;
public toOpacity : number;
start() {
let self = this; // binding
let particule : Particule;
if (this.actor.constructor == Particule)
particule = <Particule>this.actor;
else
this.destroy();
if (this.toScale) {
let s_tween = new TWEEN.Tween(particule.getLocalScale())
.to(new Sup.Math.Vector3(this.toScale.x, this.toScale.y, this.toScale.z), particule.DUREEMAX)
.onUpdate(function() {
// this : tween
// self : behavior
// particule : particule
particule.setLocalScale(this.x, this.y, this.z);
})
.easing(TWEEN.Easing.Bounce.InOut)
.start();
}
if (this.toRotation) {
let r_tween = new TWEEN.Tween(particule.getEulerAngles())
.to(new Sup.Math.Vector3(this.toRotation.x, this.toRotation.y, this.toRotation.z), particule.DUREEMAX)
.onUpdate(function() {
particule.setEulerAngles(this.x, this.y, this.z);
})
.easing(TWEEN.Easing.Bounce.InOut)
.start();
}
if (this.toColor) {
let c_tween = new TWEEN.Tween(particule.spriteRenderer.getColor)
.to(new Color(this.toColor.r, this.toColor.g, this.toColor.b), particule.DUREEMAX)
.onUpdate(function() {
particule.spriteRenderer.setColor(new Color(this.r, this.g, this.b))
})
.easing(TWEEN.Easing.Bounce.InOut)
.start();
}
if (this.toOpacity) {
let c_tween = new TWEEN.Tween({opacity : particule.spriteRenderer.getOpacity()})
.to({opacity: this.toOpacity}, particule.DUREEMAX)
.onUpdate(function() {
particule.spriteRenderer.setOpacity(this.opacity);
})
.easing(TWEEN.Easing.Bounce.InOut)
.start();
}
}
update() {
if (this.deltaPos) {
let pos = this.actor.getPosition();
this.actor.setPosition(pos.x + this.deltaPos.x, pos.y + this.deltaPos.y, pos.z + this.deltaPos.z);
}
TWEEN.update(); // ? ou le moteur le lance lui même une seule fois à chaque tick
}
}
interface toParticules {
deltaPos ? : Sup.Math.XYZ;
toScale ? : Sup.Math.XYZ;
toColor ? : Color;
toRotation ? : Sup.Math.XYZ;
toOpacity ? : number;
}
interface constructorEmitter {
particules ? : ContructorParticule[];
toParticules ? : toParticules[];
}
export class Emitter extends Sup.Actor {
particules : ContructorParticule[];
toParticules : toParticules[];
constructor (a : ConstructorActor, e : constructorEmitter) {
super(a.name, a.parent, a.options);
this.particules = e.particules;
this.toParticules = e.toParticules;
}
}
export class BehaviorEmitter extends Sup.Behavior {
start() {
let emitter : Emitter;
if (this.actor.constructor == Emitter)
emitter = <Emitter>this.actor;
else
this.destroy();
}
}
export module Systems {
export module Meteor {
class BehaviorMeteorEmitter extends BehaviorEmitter {
tick : number = 3;
static TICKOK : number = 3;
update () {
if (this.tick === BehaviorMeteorEmitter.TICKOK) {
let emitter = <Emitter>this.actor;
let lengthEmitterParticules = emitter.particules.length;
let i = Sup.Math.Random.integer(0, lengthEmitterParticules);
let lengthEmitterToParticules = emitter.toParticules.length;
let j = Sup.Math.Random.integer(0, lengthEmitterToParticules);
let particule = new Particule({name : 'MeteorParticule'}, emitter.particules[i]);
particule.addBehavior(BehaviorParticle, emitter.toParticules[j]);
this.tick = 0;
return
}
this.tick++;
}
}
export function meteor (pos : Sup.Math.XYZ, deltaPosMin : Sup.Math.XYZ, deltaPosMax : Sup.Math.XYZ) : Emitter {
let Random = Sup.Math.Random;
let cParticles : ContructorParticule[] = [];
let pParticulesBehavior : toParticules[] = [];
for (let i = 0; i < 50; i++) {
cParticles[i] = {
scale : {
x : Random.float(0.9, 1.2),
y : Random.float(0.9, 1.2),
z : 1,
},
color : new Color(1,1,1),
dureeMax : Random.integer(120, 180),
sprite : Sup.get('Assets/Design/Particules/buble', Sup.Sprite),
opacity : 1
}
pParticulesBehavior[i] = {
deltaPos : {
x: Random.float(deltaPosMin.x, deltaPosMax.x),
y: Random.float(deltaPosMin.y, deltaPosMax.y),
z: Random.float(deltaPosMin.z, deltaPosMax.z),
},
toScale : {
x: Random.float(0.1, 0.3),
y: Random.float(0.1, 0.3),
z: 1,
},
toColor : new Color(parseInt('#ADD8E6', 16)),
toOpacity : 1
}
}
let meteor = new Emitter({name: "MeteorEmitter"}, {particules : cParticles, toParticules : pParticulesBehavior});
meteor.setPosition(pos);
meteor.addBehavior(BehaviorMeteorEmitter);
return meteor;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment