Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
code cleanup and fix up particle definitions
tweak example game effects
  • Loading branch information
KilledByAPixel committed Apr 16, 2024
1 parent 563324f commit c5fd993
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 56 deletions.
27 changes: 15 additions & 12 deletions examples/breakout/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ function gameRender()
{
// draw a the background
drawRect(cameraPos, levelSize.scale(2), new Color(.5,.5,.5));
drawRect(cameraPos, levelSize, new Color(.05,.05,.05));
drawRect(cameraPos, levelSize, new Color(.02,.02,.02));
}

///////////////////////////////////////////////////////////////////////////////
function gameRenderPost()
{
// use built in image font for text
const font = new FontImage;
font.drawText('Score: ' + score, cameraPos.add(vec2(0,9.6)), .1, 1);
font.drawText('Score: ' + score, cameraPos.add(vec2(0,9.6)), .15, 1);
if (!brickCount)
font.drawText('You Win!', cameraPos.add(vec2(0,-5)), .2, 1);
else if (!ball)
Expand All @@ -93,34 +93,37 @@ function initPostProcess()
}
void mainImage(out vec4 c, vec2 p)
{
// put uv in texture pixel space
p /= iResolution.xy;
// apply fuzz as horizontal offset
const float fuzz = .001;
const float fuzz = .0005;
const float fuzzScale = 800.;
p.x += fuzz*(noise(vec2(p.y*fuzzScale, iTime*9.))*2.-1.);
const float fuzzSpeed = 9.;
p.x += fuzz*(noise(vec2(p.y*fuzzScale, iTime*fuzzSpeed))*2.-1.);
// init output color
c = texture(iChannel0, p);
// chromatic aberration
const float chromatic = .003;
c.r = texture(iChannel0, p - vec2(chromatic, 0)).r;
c.b = texture(iChannel0, p + vec2(chromatic, 0)).b;
const float chromatic = .002;
c.r = texture(iChannel0, p - vec2(chromatic,0)).r;
c.b = texture(iChannel0, p + vec2(chromatic,0)).b;
// tv static noise
const float staticNoise = .1;
c += staticNoise * hash(vec2(p + mod(iTime, 1e3)));
c += staticNoise * hash(p + mod(iTime, 1e3));
// scan lines
const float scanlineScale = 800.;
const float scanlineScale = 1e3;
const float scanlineAlpha = .1;
c *= 1. + scanlineAlpha*sin(p.y*scanlineScale);
// black vignette around the outside
// black vignette around edges
const float vignette = 2.;
float dx = 2.*p.x - 1., dy = 2.*p.y - 1.;
c *= 1.-pow((dx*dx + dy*dy)/vignette, 6.);
const float vignettePow = 6.;
float dx = 2.*p.x-1., dy = 2.*p.y-1.;
c *= 1.-pow((dx*dx + dy*dy)/vignette, vignettePow);
}`;

const includeOverlay = true;
Expand Down
22 changes: 12 additions & 10 deletions examples/breakout/gameObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ class Brick extends EngineObject
const color1 = this.color;
const color2 = color1.lerp(new Color, .5);
new ParticleEmitter(
this.pos, 0, // emitPos, emitAngle
this.pos, 0, // pos, angle
this.size, .1, 200, PI, // emitSize, emitTime, emitRate, emiteCone
tile(0, 16), // tileIndex, tileSize
color1, color2, // colorStartA, colorStartB
color1.scale(1,0), color2.scale(1,0), // colorEndA, colorEndB
.5, .3, 1, .02, .05,// time, sizeStart, sizeEnd, speed, angleSpeed
.99, .95, .4, PI, // damping, angleDamping, gravityScale, cone
.1, .8, 0, 1 // fadeRate, randomness, collide, additive
.3, .8, .3, .05, .05,// time, sizeStart, sizeEnd, speed, angleSpeed
.99, .95, .4, PI, // damp, angleDamp, gravity, cone
.1, .8, 0, 1 // fade, randomness, collide, additive
);

// set ball trail color
if (o.trailEffect)
o.trailEffect.colorStartA =
o.trailEffect.colorStartB = this.color.scale(.8);
{
o.trailEffect.colorStartA = this.color;
o.trailEffect.colorStartB = this.color.lerp(new Color, .5);
}

return 1;
}
Expand All @@ -90,20 +92,20 @@ class Ball extends EngineObject

// make a bouncy ball
this.setCollision();
this.velocity = vec2(randSign(), -1).scale(.1);
this.velocity = vec2(0, -.1);
this.elasticity = 1;

// attach a trail effect
const color = hsl(0,0,.2);
this.trailEffect = new ParticleEmitter(
this.pos, 0, // emitPos, emitAngle
this.pos, 0, // pos, angle
this.size, 0, 80, PI, // emitSize, emitTime, emitRate, emiteCone
tile(0, 16), // tileIndex, tileSize
color, color, // colorStartA, colorStartB
color.scale(0), color.scale(0), // colorEndA, colorEndB
2, .4, 1, .001, .05,// time, sizeStart, sizeEnd, speed, angleSpeed
.99, .95, 0, PI, // damping, angleDamping, gravityScale, cone
.1, .5, 0, 1 // fadeRate, randomness, collide, additive
.99, .95, 0, PI, // damp, angleDamp, gravity, cone
.1, .5, 0, 1 // fade, randomness, collide, additive
);
this.addChild(this.trailEffect);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/breakoutTutorial/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ class Brick extends EngineObject
this.pos, 0, // pos, angle
this.size, .1, 200, PI, // emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
color, color, // colorStartA, colorStartB
color, color, // colorStartA, colorStartB
color.scale(1,0), color.scale(1,0), // colorEndA, colorEndB
.2, .5, 1, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
.99, .95, .4, PI, // damping, angleDamping, gravityScale, cone
.1, .5, 0, 1 // fadeRate, randomness, collide, additive
.99, .95, .4, PI, // damp, angleDamp, gravity, cone
.1, .5, 0, 1 // fade, randomness, collide, additive
);

return 1; // allow object to collide
Expand Down
2 changes: 1 addition & 1 deletion examples/particles/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
addSetting('emitRate', '', 0, 1e9, 1, 'How many particles per second to spawn, does not emit if 0');
addSetting('emitConeAngle', '', 0, 1e9, .01,'Local angle to apply velocity to particles from emitter');
addSetting('tileIndex', '', -1, 1e9, 1, 'Tile to use to render object (-1 is untextured)');
addSetting('tileSize', '', 0, 1e9, 1, 'Size of texture tile in source pixelss');
addSetting('tileSize', '', 0, 1e9, 1, 'Size of texture tile in source pixels');
addSetting('colorStartA', 'color',0,0,0, 'Color at start of life 1');
addSetting('colorStartA_alpha', 'alpha', 0, 1, .01, 'Alpha at start of life 1');
addSetting('colorStartB', 'color',0,0,0, 'Color at start of life 2');
Expand Down
34 changes: 18 additions & 16 deletions examples/platformer/gameEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ function makeDebris(pos, color = new Color, amount = 100, size=.2, elasticity =
const color2 = color.lerp(new Color, .5);
const emitter = new ParticleEmitter(
pos, 0, 1, .1, 100, PI, // pos, angle, emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
color, color2, // colorStartA, colorStartB
color, color2, // colorEndA, colorEndB
3, size,size, .1, .05, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
1, .95, .4, PI, 0, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.5, 1 // randomness, collide, additive, randomColorLinear, renderOrder
0, // tileInfo
color, color2, // colorStartA, colorStartB
color, color2, // colorEndA, colorEndB
3, size,size, .1, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
1, .95, .4, PI, 0, // damp, angleDamp, gravity, particleCone, fade
.5, 1 // randomness, collide, additive, colorLinear, renderOrder
);
emitter.elasticity = elasticity;
emitter.particleDestroyCallback = persistentParticleDestroyCallback;
Expand Down Expand Up @@ -99,24 +99,26 @@ function explosion(pos, radius=3)

// smoke
new ParticleEmitter(
pos, 0, radius/2, .2, 50*radius, PI, // pos, angle, emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
new Color(0,0,0), new Color(0,0,0), // colorStartA, colorStartB
pos, 0, // pos, angle
radius/2, .2, 50*radius, PI, // emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
new Color(0,0,0), new Color(0,0,0), // colorStartA, colorStartB
new Color(0,0,0,0), new Color(0,0,0,0), // colorEndA, colorEndB
1, .5, 2, .1, .05, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
.9, 1, -.3, PI, .1, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.5, 0, 0, 0, 1e8 // randomness, collide, additive, randomColorLinear, renderOrder
1, .5, 2, .2, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
.9, 1, -.3, PI, .1, // damp, angleDamp, gravity, particleCone, fade
.5, 0, 0, 0, 1e8 // randomness, collide, additive, colorLinear, renderOrder
);

// fire
new ParticleEmitter(
pos, 0, radius/2, .1, 100*radius, PI, // pos, angle, emitSize, emitTime, emitRate, emiteCone
pos, 0, // pos, angle
radius/2, .1, 100*radius, PI, // emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
new Color(1,.5,.1), new Color(1,.1,.1), // colorStartA, colorStartB
new Color(1,.5,.1,0), new Color(1,.1,.1,0), // colorEndA, colorEndB
.5, .5, 2, .1, .05, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
.9, 1, 0, PI, .05, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.5, 0, 1, 0, 1e9 // randomness, collide, additive, randomColorLinear, renderOrder
.7, .8, .2, .2, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
.9, 1, -.2, PI, .05, // damp, angleDamp, gravity, particleCone, fade
.5, 0, 1, 0, 1e9 // randomness, collide, additive, colorLinear, renderOrder
);
}

Expand Down
14 changes: 7 additions & 7 deletions examples/platformer/gameObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Crate extends GameObject
return;

sound_destroyObject.play(this.pos);
makeDebris(this.pos, this.color);
makeDebris(this.pos, this.color, 200);
this.destroy();
}
}
Expand Down Expand Up @@ -214,9 +214,9 @@ class Weapon extends EngineObject
0, // tileInfo
new Color(1,.8,.5), new Color(.9,.7,.5), // colorStartA, colorStartB
new Color(1,.8,.5), new Color(.9,.7,.5), // colorEndA, colorEndB
3, .1, .1, .15, .1, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
1, .95, 1, 0, 0, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.1, 1 // randomness, collide, additive, randomColorLinear, renderOrder
3, .1, .1, .15, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
1, .95, 1, 0, 0, // damp, angleDamp, gravity, particleCone, fade
.1, 1 // randomness, collide, additive, colorLinear, renderOrder
), vec2(.1,0), -.8);
this.shellEmitter.elasticity = .5;
this.shellEmitter.particleDestroyCallback = persistentParticleDestroyCallback;
Expand Down Expand Up @@ -325,9 +325,9 @@ class Bullet extends EngineObject
0, // tileInfo
new Color(1,1,0), new Color(1,0,0), // colorStartA, colorStartB
new Color(1,1,0), new Color(1,0,0), // colorEndA, colorEndB
.2, .2, 0, .1, .1, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
1, 1, .5, PI, .1, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.5, 1, 1 // randomness, collide, additive, randomColorLinear, renderOrder
.2, .2, 0, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
1, 1, .5, PI, .1, // damp, angleDamp, gravityScale, particleCone, fade,
.5, 1, 1 // randomness, collide, additive, colorLinear, renderOrder
);
emitter.trailScale = 1;
emitter.elasticity = .3;
Expand Down
13 changes: 7 additions & 6 deletions examples/puzzle/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function gameInit()
// setup game
cameraPos = levelSize.scale(.5).add(cameraOffset);
cameraScale = 900/levelSize.y;
gravity = -.005;
gravity = -.004;
fallTimer = new Timer;
comboCount = score = 0;
}
Expand Down Expand Up @@ -288,13 +288,14 @@ function clearMatches()
const color1 = tileColors[data];
const color2 = color1.lerp(new Color, .5);
new ParticleEmitter(
pos.add(vec2(.5)), 0, 1, .1, 100, PI,// pos, angle, emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
pos.add(vec2(.5)), 0, // pos, angle
.5, .1, 200, PI, // emitSize, emitTime, emitRate, emiteCone
0, // tileInfo
color1, color2, // colorStartA, colorStartB
color1.scale(1,0), color2.scale(1,0),// colorEndA, colorEndB
.5, .3, .3, .05, .05, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
.99, 1, 1, PI, .05, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.5, 0, 1 // randomness, collide, additive, randomColorLinear, renderOrder
.5, .3, .2, .05, .05, // particleTime, sizeStart, sizeEnd, speed, angleSpeed
.99, 1, 1, PI, .05, // damp, angleDamp, gravityScale, particleCone, fadeRate
.5, 0, 1 // randomness, collide, additive, colorLinear, renderOrder
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/engineDraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let drawCount;
* @param {Number} [textureIndex=0] - Texture index to use
* @return {TileInfo}
* @example
* tile(2) // a tile at index 2 using the default tile size (16)
* tile(2) // a tile at index 2 using the default tile size of 16
* tile(5, 8) // a tile at index 5 using a tile size of 8
* tile(1, 16, 3) // a tile at index 1 of size 16 on texture 3
* tile(vec2(4,8), vec2(30,10)) // a tile at pixel location (4,8) with a size of (30,10)
Expand Down

0 comments on commit c5fd993

Please sign in to comment.