Skip to content

Commit

Permalink
simplify pillar generation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon committed Jun 13, 2020
1 parent c616a17 commit 84acb11
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 66 deletions.
126 changes: 61 additions & 65 deletions client/src/Game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,61 +191,55 @@ class PillarGroupObject extends GameObject {
};

async loadAsync() {
const target = await this.add(new PlatformObject());
target.x = 0;
target.z = Settings.ballDistance;
this.pillars.push(target);
for (let i = 0; i < this.getVisiblePillarCount(); i++) {
await this.addTarget();
}
this.getCurrentPillar().becomeCurrent();
await this.reset();
return super.loadAsync();
}

reset = async () => {
for (const target of this.pillars) {
target.destroy();
for (const pillar of this.pillars) {
pillar.destroy();
}
this.pillars = [];
const target = await this.add(new PlatformObject());
if (target) {
target.x = 0;
target.z = Settings.ballDistance;
this.pillars.push(target);
}
for (let i = 0; i < this.getVisiblePillarCount(); i++) {
await this.addTarget();
}

// const pillar = await this.add(new PlatformObject());
// pillar.x = 0;
// pillar.z = Settings.ballDistance;
// this.pillars.push(pillar);
await this.ensureTargetsAreCreatedAsync();
this.getCurrentPillar().becomeCurrent();
this.getNextPillar().becomeTarget();
};

addTarget = async (score) => {
addPillarAsync = async (score) => {
const lastPillar = this.getLastPillar();
const startX = lastPillar.x;
const startZ = lastPillar.z;
const target = await this.add(new PlatformObject());
this.pillars.push(target);

if (!lastPillar) {
// is the first pillar
target.z = Settings.ballDistance;
} else {
// find a random location relative to the last pillar
const startX = lastPillar.x;
const startZ = lastPillar.z;
const radians = this.generateRandomAngleForPillar();
target.x = startX + Settings.ballDistance * Math.sin(radians);
target.z = startZ + Settings.ballDistance * Math.cos(radians);
target.lastAngle = Math.atan2(startZ - target.z, startX - target.x);
// This seems to cause lag
// target.alpha = this.alphaForTarget(this.pillars.length, score);
target.animateIn();
}
};

generateRandomAngleForPillar() {
const range = 90;
const randomAngle = randomRange(
Settings.angleRange[0] + range,
Settings.angleRange[1] + range
);

const radians = THREE.Math.degToRad(randomAngle);
target.x = startX + Settings.ballDistance * Math.sin(radians);
target.z = startZ + Settings.ballDistance * Math.cos(radians);

this.pillars.push(target);

target.lastAngle = Math.atan2(
lastPillar.z - target.z,
lastPillar.x - target.x
);
// This seems to cause lag
// target.alpha = this.alphaForTarget(this.pillars.length, score);
target.animateIn();
};
return radians;
}

alphaForTarget = (i, score) => {
const inverse = i - 1;
Expand All @@ -261,7 +255,7 @@ class PillarGroupObject extends GameObject {

ensureTargetsAreCreatedAsync = async (score) => {
while (this.pillars.length < this.getVisiblePillarCount(score)) {
await this.addTarget(score);
await this.addPillarAsync(score);
}
};
}
Expand Down Expand Up @@ -420,42 +414,44 @@ class Game extends GameObject {

const landedPillarRadius = targetPlatform.radius;

if (distanceFromTarget < landedPillarRadius) {
const accuracy = 1 - distanceFromTarget / landedPillarRadius;
dispatch.game.play();
dispatch.score.increment();
this.score += 1;
playHaptics(accuracy);
if (distanceFromTarget > landedPillarRadius) {
// Missed the pillar
this.gameOver();
return;
}

if (this.particles) {
this.particles.impulse();
}
const accuracy = 1 - distanceFromTarget / landedPillarRadius;
dispatch.game.play();
dispatch.score.increment();
this.score += 1;
playHaptics(accuracy);

const previousPillar = this.pillarGroup.pillars.shift();
if (this.particles) {
this.particles.impulse();
}

this.generateDirection();
const previousPillar = this.pillarGroup.pillars.shift();

if (this.pillarGroup.getCurrentPillar()) {
this.pillarGroup.getCurrentPillar().updateDirection(this.direction);
}
this.generateDirection();

previousPillar.animateOut();
this.pillarGroup.getCurrentPillar().becomeCurrent();
if (this.pillarGroup.getCurrentPillar()) {
this.pillarGroup.getCurrentPillar().updateDirection(this.direction);
}

if (Settings.gemsEnabled && this.score > 3) {
const gemCount = Math.floor(accuracy * 6);
this.pillarGroup.getCurrentPillar().showGems(gemCount);
}
this.pillarGroup.getNextPillar().becomeTarget();
previousPillar.animateOut();
this.pillarGroup.getCurrentPillar().becomeCurrent();

this.playerObject.landed(accuracy, landedPillarRadius);
if (Settings.gemsEnabled && this.score > 3) {
const gemCount = Math.floor(accuracy * 6);
this.pillarGroup.getCurrentPillar().showGems(gemCount);
}
this.pillarGroup.getNextPillar().becomeTarget();

await this.pillarGroup.ensureTargetsAreCreatedAsync(this.score);
this.playerObject.landed(accuracy, landedPillarRadius);

// this.pillarGroup.syncTargetsAlpha(this.score);
} else {
this.gameOver();
}
await this.pillarGroup.ensureTargetsAreCreatedAsync(this.score);

// this.pillarGroup.syncTargetsAlpha(this.score);
};

takeScreenshot = async () => {
Expand Down
1 change: 0 additions & 1 deletion client/src/Game/engine/entities/Platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TweenMax } from "gsap";

import { Color, Mesh, CylinderBufferGeometry, MeshPhongMaterial } from "three";
import GameObject from "../core/GameObject";
import FlatMaterial from "../utils/flatMaterial";
import randomRange from "../utils/randomRange";
import Gem from "./Gem";
import Settings from "../../../constants/Settings";
Expand Down

0 comments on commit 84acb11

Please sign in to comment.