Skip to content

Commit

Permalink
Article #6
Browse files Browse the repository at this point in the history
  • Loading branch information
山田元康m_yamada@spicysoft.com committed Feb 26, 2019
1 parent 0ba7b90 commit 77ebfd2
Showing 1 changed file with 99 additions and 17 deletions.
116 changes: 99 additions & 17 deletions src/Main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const PLATFORM_BLOCK_WIDTH = 32;

enum PlatformGenerationMode {
Flat,
SlopeAscent,
SlopeDescent,
LevelsAscent,
LevelsDescnet,
Cliff,
Pits,
LargePit,
}

function random(min:number,max:number) {
return min + Math.random() * (max -min);
}

class Main extends eui.UILayer {

Expand Down Expand Up @@ -55,6 +69,7 @@ class Main extends eui.UILayer {
private colors: Array<number> = [0xbbbbbb,0x660000];
private generationIndex: number = 0;
private lastDrawnX: number;
private lastDrawnY: number;
private platforms: Array<egret.Shape> = [];
private ball: egret.Shape;
private tapped: boolean;
Expand All @@ -68,6 +83,7 @@ class Main extends eui.UILayer {

this.cameraX = - this.width;
this.lastDrawnX = this.cameraX - this.width / 2;
this.lastDrawnY = 0;

this.createBall();
}
Expand All @@ -83,7 +99,9 @@ class Main extends eui.UILayer {
private createBall() {
this.ball = new egret.Shape();
this.ball.x = 0;
this.ball.y = 0;
this.ball.y = -5;
this.ballVy = 0;
this.ballVx = 3;
{
const g = this.ball.graphics;
g.beginFill(0xcccc00);
Expand Down Expand Up @@ -121,7 +139,7 @@ class Main extends eui.UILayer {
if (isBallOnBlock) {
this.jumpCount = 0;
} else {
this.ballVy += 0.1;
this.ballVy += 0.15;
}
if ((isBallOnBlock || this.jumpCount < 2) && this.tapped) {
this.ballVy -= 5;
Expand All @@ -142,27 +160,91 @@ class Main extends eui.UILayer {
});
}

private drawPlatformGenerationMode() : PlatformGenerationMode{
const patterns = [PlatformGenerationMode.Flat,PlatformGenerationMode.SlopeDescent,PlatformGenerationMode.SlopeAscent];
return patterns[this.generationIndex % 3];
}

private nextColor() : number {
const c = this.colors[this.generationIndex % 2];
this.generationIndex++;;
return c;
}

private generateNewBlocks() {
const generationBorder = this.cameraX + this.width;
while(this.lastDrawnX < generationBorder) {
const p = new egret.Shape();
p.x = this.lastDrawnX;
p.y = 0;
this.lastDrawnX += PLATFORM_BLOCK_WIDTH;

const c = this.colors[this.generationIndex];
p.graphics.beginFill(c);
p.graphics.drawRect(0, 0, PLATFORM_BLOCK_WIDTH,PLATFORM_BLOCK_WIDTH);
p.graphics.endFill();
this.world.addChild(p);
this.platforms.push(p);
this.generationIndex = (this.generationIndex + 1) % 2;

switch(this.drawPlatformGenerationMode()) {
case PlatformGenerationMode.Flat:
{
const len = random(3,10);
let x = 0;
for (let n = 0; n < len; n++) {
const p = new egret.Shape();
p.x = this.lastDrawnX;
p.y = this.lastDrawnY + random(-5,5);
p.graphics.beginFill(this.nextColor());
p.graphics.drawRect(0, 0, 32,32);
p.graphics.endFill();
this.world.addChild(p);
this.platforms.push(p);
this.lastDrawnX += p.width;
this.lastDrawnY = p.y;
}
}
break;
case PlatformGenerationMode.SlopeDescent:
{
const len = random(10,30);
const descent = random(7,10);
let x = 0;
for (let n = 0; n < len; n++) {
const p = new egret.Shape();
p.x = this.lastDrawnX;
p.y = this.lastDrawnY + descent + random(-5,5);
p.graphics.beginFill(this.nextColor());
p.graphics.drawRect(0, 0, 32,32);
p.graphics.endFill();
this.world.addChild(p);
this.platforms.push(p);
this.lastDrawnX += p.width;
this.lastDrawnY = p.y;
}
}
case PlatformGenerationMode.SlopeAscent:
{
const len = random(10,30);
const ascent = random(-10,-7);
let x = 0;
for (let n = 0; n < len; n++) {
const p = new egret.Shape();
p.x = this.lastDrawnX;
p.y = this.lastDrawnY + ascent + random(-5,5);
p.graphics.beginFill(this.nextColor());
p.graphics.drawRect(0, 0, 32,32);
p.graphics.endFill();
this.world.addChild(p);
this.platforms.push(p);
this.lastDrawnX += p.width;
this.lastDrawnY = p.y;
}
}
break;
case PlatformGenerationMode.LevelsAscent:
case PlatformGenerationMode.LevelsDescnet:
case PlatformGenerationMode.Cliff:
case PlatformGenerationMode.Pits:
case PlatformGenerationMode.LargePit:
break;
}
}
}

private updateCamera(): void {
const toMove = this.ball.x - this.cameraX;
this.cameraX += Math.min(10,toMove / 60);
// const toMove = this.ball.x - this.cameraX;
// this.cameraX += Math.min(10,toMove / 60);
this.cameraX = this.ball.x;
this.world.x = this.width / 2 - this.cameraX;
this.world.y = this.height / 2;
}
Expand Down

0 comments on commit 77ebfd2

Please sign in to comment.