Skip to content

Commit

Permalink
#railroad: Switch elf texture when they get a present (#154)
Browse files Browse the repository at this point in the history
* #railroad: added blender plugin and re-exported scene

* #railroad: Switch elf textures when they get a present

* #railroad: addressed comments on PR

* #railroad: small adjustments to PR fixes
  • Loading branch information
dandov authored Nov 27, 2023
1 parent 65ff836 commit 06e3685
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 6 deletions.
Binary file added static/scenes/railroad/img/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/scenes/railroad/img/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/scenes/railroad/img/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/scenes/railroad/img/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/scenes/railroad/img/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions static/scenes/railroad/js/level.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
goog.provide('app.Level');

goog.require('app.Scene');
goog.require('app.getElfImage');
goog.require('app.systems.CameraSystem');
goog.require('app.systems.RaycasterSystem');
goog.require('app.systems.PresentSystem');
Expand Down Expand Up @@ -36,11 +37,24 @@ class Level {
this.presentSystem.update(deltaSeconds);
}

handleClick(clickEvent) {
async handleClick(clickEvent) {
const intersections = this.raycasterSystem.cast(clickEvent);

if (intersections.length > 0) {
this.presentSystem.shoot(intersections[0].point);
const presentLanded = this.presentSystem.shoot(intersections[0].point);
const targetObject = intersections[0].object;
if (targetObject instanceof THREE.Sprite &&
targetObject.userData.clickable &&
targetObject.userData.clickable.type === 'elf' &&
targetObject.userData.assetUrl !== undefined) {
// Wait for the present to hit its target and then update the elf's sprite.
await presentLanded;
const textureWithPresent =
getElfImage(targetObject.userData.assetUrl.replace('@', '_Holding@'));
if (textureWithPresent) {
targetObject.material.map = textureWithPresent;
}
}
}
}
}
Expand Down
22 changes: 20 additions & 2 deletions static/scenes/railroad/js/present.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Present {
this.scene = scene;
this.model = loadedObj.clone();
this.model.scale.setScalar(0.0015);
this.model.rotation.set(0, Math.PI / 2, 0);
for (let i = 0; i < this.model.children.length; i++) {
if (i !== 4) {
this.model.children[i].material = material0;
Expand All @@ -61,9 +62,17 @@ class Present {
}

parent.add(this.model);

this.shootPromise = undefined;
this.shootResolveFunction = undefined;
}

shoot(targetPosition) {
async shoot(targetPosition) {
if (this.shootPromise !== undefined) {
await this.shootPromise;
return;
}

// Move to world space to handle the throw
this.scene.scene.attach(this.model);

Expand All @@ -78,7 +87,7 @@ class Present {

// calculate variables we need to calculate physics stuff
var throwDistance = this.startPosition.distanceTo(this.targetPosition);
this.durationOfThrow = throwDistance/linearThrowSpeed;
this.durationOfThrow = throwDistance / linearThrowSpeed;

// if we know gravity, the height delta, and how long the throw takes,
// we can choose a start y velocity that will last through the throw
Expand All @@ -101,13 +110,22 @@ class Present {
// update the state
this.currentFlightTime = 0;
this.inFlight = true;

// Create a promise that will be resolved when the gift lands.
this.shootPromise = new Promise(r => {
this.shootResolveFunction = r;
});
await this.shootPromise;
}

update(deltaSeconds) {
if (this.inFlight && !this.landed) {
if (this.currentFlightTime > this.durationOfThrow) {
this.landed = true;
this.model.position.copy(this.targetPosition);
if (this.shootResolveFunction) {
this.shootResolveFunction();
}
} else {
var t = this.currentFlightTime/this.durationOfThrow;

Expand Down
12 changes: 12 additions & 0 deletions static/scenes/railroad/js/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const ELF_IMAGE_NAMES = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];
/** @type {Map<string, THREE.Texture>} */
Expand Down Expand Up @@ -157,8 +162,15 @@ function replaceElvesWithSprites(scene) {

sprite.material.rotation = (node.rotation.y);
sprite.userData.isElf = true;
sprite.userData.clickable = {type: 'elf'};
sprite.userData.assetUrl = assetUrl;
node.add(sprite);
});
}

function getElfImage(assetUrl) {
return elfImages.get(assetUrl);
}

app.Scene = Scene;
app.getElfImage = getElfImage;
7 changes: 5 additions & 2 deletions static/scenes/railroad/js/systems/present-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ class PresentSystem {
this.currentPresent = present;
}

shoot(targetPosition) {
this.currentPresent.shoot(targetPosition);
async shoot(targetPosition) {
const landPromise = this.currentPresent.shoot(targetPosition);
this.addNewPresent();
// Add new present first and then await the landing promise so that
// the player can keep throwing gifts.
await landPromise;
}

update(deltaSeconds) {
Expand Down

0 comments on commit 06e3685

Please sign in to comment.