Skip to content

Instantly share code, notes, and snippets.

@LoganBarnett
Last active August 29, 2015 14:20
Show Gist options
  • Save LoganBarnett/d2be89fcd974e6e45991 to your computer and use it in GitHub Desktop.
Save LoganBarnett/d2be89fcd974e6e45991 to your computer and use it in GitHub Desktop.
Logan's flailing re: a scene adapter using FP
const move = (obj) => {
if(obj.moving) {
const newLocation = new Vector3(
obj.transform.x + obj.speed.x
, obj.transform.y + obj.speed.y
, obj.transform.z + obj.speed.z
);
var newTransform = new Transform(obj.transform, {location: newLocation}) * Time.delta});
return new SceneObj(obj, {transform: newTransform});
}
else {
return obj;
}
};
const checkCollision = (obj) => {
// need sceneObjs
// can we just make a getSceneObjs()? Doesn't that mean the function doesn't get all inputs at start?
// Doesn't that violate the principles of FP? What if bigCollisionCheck() is decomposed with the sceneObjs in it already?
const check = /* big collision check */;
// maybe otherObj is null when not provided? Maybe we have a SceneObj.none we can use.
return new SceneObj(obj, {collidedThisFrame: true, collidedWith: otherObj});
};
const startFallingIfCollided = (obj) => {
if(obj.collidedThisFrame) {
return new SceneObj(obj, {speed: {x: 0, y: -1, z: 0}});
}
else {
return obj;
}
}
// try to move a ball across a scene, hit a wall, and then fall to the ground
const gimmehBall = () => {
// this is a very 'wind up' style where all of the ball's interactions are defined up-front as part of the ball's creation.
// Lots of new SceneObj. Should probably just be a createSceneObj so we're not stuffing behavior into the SceneObj itself.
const ball = new SceneObj(SceneObj.none, {moving: true});
// This looks maybe-ish. Is that supposed to be a smell? Maybe it should just be a maybe in the first place.
// That way we can handle errors when they inevitably come up.
const ballUpdate = (ball) => {
ball = move(ball);
ball = checkCollision(ball);
// I want to put the transition to falling here, but this feels like another function should be called instead.
ball = startFallingIfCollided(ball);
return ball;
}
// pretend for a moment that .push returns a new list
return sceneObjUpdates.push(ballUpdate);
}
gimmehBall();
// later in the scene manager
// the tires have to hit the pavement somewhere, right? Mutating the ref but only in this one dirty, secret place.
// ...dirty...
virtualSceneObjs = sceneObjs.map(sceneObjUpdates);
// do the delta somehow
...
// apply scene changes from the delta
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment