Initial Commit: CC - Entity Loaded - AR Football 3 (wtmstcyv66.js)

beta.r3js.org
Theunis Johannes Botha 2018-05-28 21:00:19 +02:00
parent d6f09afb8e
commit 55c5052229
1 changed files with 578 additions and 0 deletions

578
wtmstcyv66.js Normal file
View File

@ -0,0 +1,578 @@
if (data.entity === this.parentEntity) {
console.log('AR Football 2 Entity Loaded');
} else {
return;
}
/**
* Code Components
*/
this.mouseMove = R3.EntityManager.Instance.findComponentById('s2zsm78yxy');
this.touchMove = R3.EntityManager.Instance.findComponentById('s2ox7qg6em');
this.beforeRender = R3.EntityManager.Instance.findComponentById('7mz9kopp3p');
/**
* Meshes
*/
this.footballPlayer = R3.EntityManager.Instance.findComponentById('e19s2po3i5');
this.football = R3.EntityManager.Instance.findComponentById('6yq2mst2wf');
this.goalL = R3.EntityManager.Instance.findComponentById('95djvohlv1');
this.goalR = R3.EntityManager.Instance.findComponentById('xqj8qnv9gs');
this.goalWithHoles = R3.EntityManager.Instance.findComponentById('b0kddfdu3n');
/**
* Geometry
*/
this.ballGeometry = R3.EntityManager.Instance.findComponentById('dftmw6uhk');
/**
* Materials
*/
this.materialWhite = R3.EntityManager.Instance.findComponentById('wd0zkhwfwr');
this.materialBlack = R3.EntityManager.Instance.findComponentById('dfezir3h79');
/**
* Scenes
*/
this.arScene = R3.EntityManager.Instance.findComponentById('bgqz53dzcn');
this.playerRotation = {
x : 0,
y : 0
};
R3.CustomCode.TIME_LIMIT = 1.5 * 90;
this.balls = [];
this.canKick = true;
this.kick = false;
this.startTime = null;
this.kickDuration = 0;
this.kickDistance = 0;
this.raycaster = new THREE.Raycaster();
this.debug = false;
this.gravity = -9.8;
this.initialSpeed = 5;
this.startRayZ = -1.75;
this.endRayZ = -2;
this.playerY = 0.389;
this.totalTime = R3.CustomCode.TIME_LIMIT;
this.score = 0;
this.secondCounter = 0;
this.ballY = this.football.instance.geometry.boundingSphere.radius / 2;
this.activeBall = null;
if (this.debug) {
this.arrowHelper = new THREE.ArrowHelper(
new THREE.Vector3(0,0,-1),
new THREE.Vector3(0,0,0),
1
);
this.arScene.instance.add(this.arrowHelper);
}
R3.CustomCode.prototype.createActiveBall = function() {
this.activeBall = new THREE.Mesh(
this.ballGeometry.instance,
[
this.materialWhite.instance,
this.materialBlack.instance
]
);
this.activeBall.scale.copy(this.football.instance.scale);
this.activeBall.position.copy(this.football.instance.position);
this.football.parentScene.instance.add(this.activeBall);
};
R3.CustomCode.prototype.render = function(delta) {
this.totalTime -= delta;
this.secondCounter += delta;
if (this.secondCounter >= 1) {
this.secondCounter -= 1;
R3.Event.Emit(
R3.Event.GAME_DATA,
{
type : 'timer',
time : Math.round(this.totalTime)
}
)
}
if (this.totalTime <= 0) {
this.totalTime = 0;
R3.Event.Emit(
R3.Event.GAME_OVER,
{
timeLimit : R3.CustomCode.TIME_LIMIT,
timeUsed : R3.CustomCode.TIME_LIMIT - this.totalTime,
score : this.score,
goals : this.balls.reduce(
function(result, ball){
if (ball.scored) {
result.push(ball);
}
return result;
},
[]
)
}
)
}
if (this.totalTime)
this.balls.map(
function(ball) {
if (ball.stopped) {
return;
}
if (ball.speed <= 0) {
ball.stopped = true;
}
ball.speed -= delta / 2;
ball.t += delta / 2;
var u = new THREE.Vector3(
ball.speed * Math.sin(ball.directionAngle),
ball.speed * Math.sin(ball.upAngle),
ball.speed * Math.cos(ball.directionAngle)
);
ball.mesh.position.x = ball.s0.x + u.x * ball.t;
ball.mesh.position.y = ball.s0.y + u.y * ball.t + 0.5 * (this.gravity) * ball.t * ball.t;
ball.mesh.position.z = ball.s0.z + u.z * ball.t;
/**
* Bounce
*/
if (ball.mesh.position.y <= this.ballY) {
ball.s0.x = ball.mesh.position.x;
ball.s0.y = this.ballY;
ball.s0.z = ball.mesh.position.z;
ball.upAngle *= ball.bounciness;
ball.t = 0;
}
/**
* Check for goal collision
*/
var goalDistanceL = this.goalL.position.distanceTo(ball.mesh.position);
var goalDistanceR = this.goalR.position.distanceTo(ball.mesh.position);
if (goalDistanceL < (this.goalL.geometry.boundingSphere.radius - ball.mesh.geometry.boundingSphere.radius)) {
if (ball.scored === false) {
console.log('left goal!');
ball.scored = true;
ball.scoredThroughGoal = 'left';
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Crowd Cheer'
}
);
R3.Event.Emit(
R3.Event.GAME_DATA,
{
type : 'score',
goal : 'left'
}
);
}
}
if (goalDistanceR < (this.goalR.geometry.boundingSphere.radius - ball.mesh.geometry.boundingSphere.radius)) {
if (ball.scored === false) {
console.log('right goal!');
ball.scored = true;
ball.scoredThroughGoal = 'right';
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Crowd Cheer'
}
);
R3.Event.Emit(
R3.Event.GAME_DATA,
{
type : 'score',
goal : 'right'
}
);
}
}
/**
* Check for goal reflection
*/
var futureTime = ball.t + delta;
ball.s1.x = ball.s0.x + u.x * futureTime;
ball.s1.y = ball.s0.y + u.y * futureTime + 0.5 * (this.gravity) * futureTime * futureTime;
ball.s1.z = ball.s0.z + u.z * futureTime;
//var distance = ball.mesh.position.distanceTo(ball.s1);
var direction = ball.s1.sub(ball.mesh.position).normalize();
if (this.debug) {
this.arrowHelper.position.copy(ball.mesh.position);
this.arrowHelper.setDirection(direction);
this.arrowHelper.setLength(1);
}
if (
!ball.scored &&
ball.mesh.position.z < this.startRayZ &&
ball.mesh.position.z > this.endRayZ
) {
this.raycaster.set(ball.mesh.position, direction);
this.raycaster.intersectObjects([
this.goalWithHoles.instance
]).map(
function(intersect) {
if (intersect.distance < ball.mesh.geometry.boundingSphere.radius) {
var normal = new THREE.Vector3(
intersect.object.geometry.getAttribute('normal').array[0],
intersect.object.geometry.getAttribute('normal').array[1],
intersect.object.geometry.getAttribute('normal').array[2]
);
var reflect = direction.reflect(normal);
var angle = direction.angleTo(reflect);
ball.directionAngle = angle;
ball.s0.x = ball.mesh.position.x;
ball.s0.y = ball.mesh.position.y;
ball.s0.z = ball.mesh.position.z;
ball.upAngle *= ball.bounciness;
ball.speed *= ball.bounciness * 1.5;
ball.t = 0;
ball.v.copy(direction);
ball.vRotationAxis.x *= -1;
ball.vRotationAxis.z *= -1;
}
}
)
}
// if (ball.mesh.position.z <= -5 && !ball.scored && ball.lastIntersect) {
// console.log(ball.lastIntersect);
// }
/**
* Rotate the ball
*/
ball.mesh.rotateOnAxis(ball.vRotationAxis, -ball.speed * 0.04);
}.bind(this)
)
};
R3.CustomCode.prototype.move = function(data) {
var x = data.x || 0;
var y = data.y || 0;
x *= 0.0025;
y *= 0.0075;
this.playerRotation.x += x;
this.playerRotation.y += y;
/**
* Clamp player rotation to values between 0 and Math.PI * 2
*/
if (this.playerRotation.y > Math.PI) {
this.playerRotation.y = Math.PI;
}
if (this.playerRotation.y < -Math.PI) {
this.playerRotation.y = -Math.PI;
}
if (this.playerRotation.x > Math.PI / 2) {
this.playerRotation.x = Math.PI / 2;
}
if (this.playerRotation.x < -Math.PI / 2) {
this.playerRotation.x = -Math.PI / 2;
}
if (this.playerRotation.y <= 0) {
this.canKick = true;
}
if (this.canKick && this.playerRotation.y > 0) {
this.canKick = false;
this.kick = true;
}
if (y > 0 && this.canKick) {
/**
* Moving down, initialize start time and record the distance travelled
*/
if (this.startTime === null) {
this.startTime = Date.now();
}
this.kickDistance += Math.abs(y);
}
if (y < 0) {
/**
* We are no longer moving down, also reset our Y distance counter
* @type {boolean}
*/
this.kickDistance = 0;
this.startTime = null;
}
if (this.canKick && this.activeBall === null) {
this.createActiveBall();
}
this.footballPlayer.instance.matrix = new THREE.Matrix4();
this.footballPlayer.instance.matrix.decompose(
this.footballPlayer.instance.position,
this.footballPlayer.instance.quaternion,
this.footballPlayer.instance.scale
);
this.footballPlayer.instance.translateY(this.playerY);
this.footballPlayer.instance.rotateY(this.playerRotation.x);
this.footballPlayer.instance.translateZ(this.football.instance.geometry.boundingSphere.radius);
this.footballPlayer.instance.rotateX(this.playerRotation.y);
if (this.kick) {
this.kick = false;
if (this.startTime === null) {
console.warn('no kick start time');
return;
}
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Kick'
}
);
var kickDuration = Date.now() - this.startTime;
var speed = this.kickDistance / kickDuration * 20;
// if (speed > 1.5) {
// speed = 1.5;
// }
// console.log(speed);
var v = new THREE.Vector3(0, 0, -1);
v.multiplyScalar(speed);
v.applyAxisAngle(
new THREE.Vector3(0,1,0),
this.playerRotation.x
);
var vRotationAxis = new THREE.Vector3(
-v.z,
0,
v.x
).normalize();
this.balls.push(
{
mesh : this.activeBall,
speed : this.initialSpeed,
mouseYTravelDistance : this.kick.distance,
directionAngle : this.playerRotation.x + Math.PI,
kickDuration : this.kickDuration,
kickDistance : this.kickDistance,
kickSpeed : speed,
upAngle : Math.PI * speed,
v : v,
vRotationAxis : vRotationAxis,
t : 0,
s0 : new THREE.Vector3(0,this.ballY,0),
s1 : new THREE.Vector3(0,this.ballY,0),
bounciness : 0.5,
scored : false,
scoredThroughGoal : null,
stopped :false
}
);
this.activeBall = null;
}
}.bind(this)
R3.Event.Subscribe(
R3.Event.GAME_OVER,
function(data) {
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Crowd Cheer'
}
);
R3.Event.Emit(
R3.Event.STOP_AUDIO,
{
name : 'Audio - Crowd'
}
);
this.mouseMove.entityLoaded = null;
this.touchMove.entityLoaded = null;
this.beforeRender.entityLoaded = null;
}.bind(this)
);
R3.Event.Subscribe(
R3.Event.GAME_START,
function() {
this.canKick = true;
this.kick = false;
this.startTime = null;
this.kickDuration = 0;
this.kickDistance = 0;
/**
* Reset football player
* @type {number}
*/
this.footballPlayer.instance.rotation.x = 0;
this.footballPlayer.instance.rotation.y = 0;
this.footballPlayer.instance.rotation.z = 0;
this.footballPlayer.instance.position.x = 0;
this.footballPlayer.instance.position.y = this.playerY;
this.footballPlayer.instance.position.z = this.football.instance.geometry.boundingSphere.radius;
/**
* Reset football
* @type {number}
*/
this.football.instance.rotation.x = 0;
this.football.instance.rotation.y = 0;
this.football.instance.rotation.z = 0;
this.football.instance.position.x = 0;
this.football.instance.position.y = this.ballY;
this.football.instance.position.z = 0;
this.score = 0;
this.secondCounter = 0;
this.totalTime = R3.CustomCode.TIME_LIMIT;
R3.Event.Emit(
R3.Event.GAME_DATA,
{
type : 'timer',
time : this.totalTime
}
);
this.balls.map(
function(ball) {
this.football.parentScene.instance.remove(ball.mesh);
ball.mesh.material = null;
ball.mesh.geometry = null;
delete ball.mesh;
ball.mesh = null;
}.bind(this)
);
this.balls = [];
if (this.activeBall) {
this.football.parentScene.instance.remove(this.activeBall);
this.activeBall.material = null;
this.activeBall.geometry = null;
}
R3.Event.Emit(
R3.Event.PLAY_AUDIO,
{
name : 'Audio - Crowd'
}
);
this.createActiveBall();
this.mouseMove.entityLoaded = this;
this.touchMove.entityLoaded = this;
this.beforeRender.entityLoaded = this;
}.bind(this)
);
this.footballPlayer.instance.position.y = this.playerY;
this.footballPlayer.instance.position.z = this.football.instance.geometry.boundingSphere.radius;
this.football.instance.position.y = this.ballY;
this.football.instance.visible = false;
R3.Event.Emit(R3.Event.GAME_LOADED);
//@ sourceURL=entityLoaded.js