Implement package reveal and active handling

This commit is contained in:
Dennis Schoepf 2021-07-26 19:15:58 +02:00
parent 3b7beecf8c
commit 2297bbb562
4 changed files with 64 additions and 15 deletions

View file

@ -1,6 +1,8 @@
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject, Subject } from 'rxjs';
import { mp5 } from '../main'; import { mp5 } from '../main';
export const playerHeadPosition$ = new Subject<{ x: number; y: number }>();
export const revealedArea$ = new BehaviorSubject<{ x: number; y: number; w: number }>({ export const revealedArea$ = new BehaviorSubject<{ x: number; y: number; w: number }>({
x: 0, x: 0,
y: 0, y: 0,

View file

@ -21,7 +21,7 @@ export class DetailScene {
(contributor) => new Contributor(100, 200, 100) (contributor) => new Contributor(100, 200, 100)
); );
this.legacy = state.currLegacy.map((legacy) => new Legacy(200, 300, 100)); this.legacy = state.currLegacy.map((legacy) => new Legacy(200, 300, 100));
this.packages = state.currPackages.map((currPackage) => new Package(400, 300, 100)); this.packages = state.currPackages.map((currPackage) => new Package(400, 300, 50));
}); });
} }
@ -39,7 +39,7 @@ export class DetailScene {
}); });
this.packages.forEach((packageObj) => { this.packages.forEach((packageObj) => {
packageObj.place(); packageObj.place();
packageObj.drawOnReveal(); packageObj.draw();
}); });
} }

View file

@ -1,6 +1,6 @@
import { map } from 'rxjs'; import { combineLatest, map } from 'rxjs';
import { mp5 } from '../../main'; import { mp5 } from '../../main';
import { pointIsRevealed, revealedArea$ } from '../area'; import { playerHeadPosition$, pointIsRevealed, revealedArea$ } from '../area';
import { colors } from '../constants/colors'; import { colors } from '../constants/colors';
export class Package { export class Package {
@ -9,27 +9,72 @@ export class Package {
size: number; size: number;
name: string; name: string;
revealed: boolean; revealed: boolean;
active: boolean = false;
startSize: number = 5;
currentSize: number = 5;
constructor(x: number, y: number, size: number, name?: string, profileURL?: string) { constructor(x: number, y: number, size: number, name?: string, profileURL?: string) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.size = size; this.size = size;
revealedArea$ combineLatest([revealedArea$, playerHeadPosition$]).subscribe(
.pipe(map((revealedArea) => pointIsRevealed({ x: this.x, y: this.y }, revealedArea))) ([revealedArea, playerHeadPosition]) => {
.subscribe((isRevealed) => { const isRevealed = pointIsRevealed({ x: this.x, y: this.y }, revealedArea);
const isTouched = isRevealed
? pointIsRevealed(
{ x: this.x, y: this.y },
{
x: playerHeadPosition.x,
y: playerHeadPosition.y,
w: 30,
}
)
: false;
this.revealed = isRevealed; this.revealed = isRevealed;
});
if (this.active === false && isTouched) {
this.active = true;
console.log(this.active);
}
}
);
} }
public place() { public place() {
mp5.fill(mp5.color(colors.redDark)); mp5.noStroke();
mp5.ellipse(this.x, this.y, 10); mp5.rectMode('center');
mp5.fill(mp5.color(colors.greyLight));
mp5.rect(this.x, this.y, this.startSize, this.startSize);
} }
public drawOnReveal() { public draw() {
if (this.revealed) { if (this.revealed && !this.active) {
mp5.ellipse(this.x, this.y, this.size); this.drawRevealedShape();
} else if ((this.active && this.revealed) || (!this.revealed && this.active)) {
this.drawActiveShape();
} else {
this.drawUnrevealedShape();
} }
} }
private drawUnrevealedShape() {
mp5.rect(this.x, this.y, this.startSize, this.startSize);
}
private drawRevealedShape() {
if (this.currentSize < this.size) {
this.currentSize++;
} else {
this.currentSize = this.size;
}
mp5.rect(this.x, this.y, this.currentSize, this.currentSize);
}
private drawActiveShape() {
mp5.fill(mp5.color(colors.redLight));
mp5.rect(this.x, this.y, this.currentSize, this.currentSize);
}
} }

View file

@ -1,5 +1,5 @@
import { mp5 } from '../../main'; import { mp5 } from '../../main';
import { revealedArea$ } from '../area'; import { playerHeadPosition$, revealedArea$ } from '../area';
import { colors } from '../constants/colors'; import { colors } from '../constants/colors';
export class Player { export class Player {
@ -34,6 +34,8 @@ export class Player {
this.drawPlayerTrail(); this.drawPlayerTrail();
this.drawPlayerShape(this.x, this.y); this.drawPlayerShape(this.x, this.y);
playerHeadPosition$.next({ x: this.x, y: this.y });
} }
public move() { public move() {