Skip to content

Commit

Permalink
Implemented present thowing (#143)
Browse files Browse the repository at this point in the history
* I have basic linear present throwing working

* I now have a better throwing mechanic
  • Loading branch information
patm1987 authored Nov 20, 2023
1 parent 0989258 commit 6a5e42b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
8 changes: 5 additions & 3 deletions static/scenes/railroad/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ class Game {

handleClick(clickEvent) {
const nowSeconds = Date.now() / 1000;
this.raycasterSystem.cast(clickEvent);
const intersections = this.raycasterSystem.cast(clickEvent);

// Test present shot
const aim = this.placeholderScene.getCameraPosition(nowSeconds + 30);
this.presentSystem.shoot(aim);
// const aim = this.placeholderScene.getCameraPosition(nowSeconds + 30);
if (intersections.length > 0) {
this.presentSystem.shoot(intersections[0].point);
}
}
}

Expand Down
43 changes: 29 additions & 14 deletions static/scenes/railroad/js/present.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const loader = new THREE.OBJLoader();

let loadedObj;

// the distance to the target scaled by this number is how high we go
const midpointOffset = .25;

// a value from 0 to 1 that defines the peak height
const midpointT = .25;

class Present {

static async preload() {
Expand All @@ -19,9 +25,8 @@ class Present {
this.scene = scene;
this.inFlight = false;
this.landed = false;
this.totalFlightTime = 4;
this.totalFlightTime = .25;
this.currentFlightTime = 0;

this.model = loadedObj.clone();
this.model.scale.setScalar(0.001);
for (let i = 0; i < this.model.children.length; i++) {
Expand All @@ -31,35 +36,45 @@ class Present {
this.model.children[i].material = giftWrapMaterial;
}
}
this.scene.camera.add(obj);
obj.position.set(.125, -.125, -1);
}

shoot(targetPosition) {
this.targetPosition = targetPosition;
this.startPosition = this.model.position;
this.scene.scene.attach(this.model);
this.targetPosition = targetPosition.clone();
this.startPosition = new THREE.Vector3();
this.model.getWorldPosition(this.startPosition);

var midpoint = this.startPosition.clone()
.multiplyScalar(1-midpointT)
.addScaledVector(this.targetPosition, midpointT)
.add(new THREE.Vector3(0, this.startPosition.distanceTo(this.targetPosition) * midpointOffset));

this.curve = new THREE.QuadraticBezierCurve3(this.startPosition.clone(), midpoint.clone(), this.targetPosition.clone());

var midpoint = new THREE.Vector3().add(this.targetPosition, this.startPosition).multiplyScalar(0.5);
midpoint.setY(midpoint.y + 3);
this.curve = new THREE.QuadraticBezierCurve3(this.startPosition, midpoint, this.targetPosition);
this.inFlight = true;
}

addDebugGeo() {
const points = this.curve.getPoints(50);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({color: 0x00ff00});
const curveObject = new THREE.Line(geometry, material);
this.scene.scene.add(curveObject);
}

update(seconds, deltaSeconds) {
if (this.inFlight) {
if (this.currentFlightTime > this.totalFlightTime) {
this.landed = true;
this.model.position.copy(this.targetPosition);
// call some kind of function to update score if good hit
} else if (this.model) {
var t = this.currentFlightTime/this.totalFlightTime;
console.log(t);
console.log(this.currentFlightTime);
console.log(this.totalFlightTime);
this.model.position.copy(this.curve.getPoint(t));
console.log(this.curve.getPoint(t));
this.currentFlightTime = this.currentFlightTime + deltaSeconds;
}
} else if (!this.landed && this.model) {
// Maybe change this later but for now just float in front of the camera
this.model.position.copy(this.scene.getCameraPosition(seconds + 2));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions static/scenes/railroad/js/systems/raycaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RaycasterSystem {
cast(clickEvent) {
const intersections = this.getIntersections(clickEvent);
this.updateScore(intersections);
return intersections;
}

getIntersections(clickEvent) {
Expand Down

0 comments on commit 6a5e42b

Please sign in to comment.