2018-05-17 12:42:33 +02:00
|
|
|
if (data.entity === this.parentEntity) {
|
|
|
|
console.log('AR Football 2 Entity Loaded');
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Code Components
|
|
|
|
*/
|
|
|
|
this.mouseMove = R3.EntityManager.Instance.findComponentById('dwxvtxzrun');
|
2018-05-17 15:37:56 +02:00
|
|
|
this.touchMove = R3.EntityManager.Instance.findComponentById('p49pad0i7l');
|
2018-05-18 15:31:22 +02:00
|
|
|
this.beforeRender = R3.EntityManager.Instance.findComponentById('7l8ar325qf');
|
2018-05-17 12:42:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Meshes
|
|
|
|
*/
|
|
|
|
this.footballPlayer = R3.EntityManager.Instance.findComponentById('t537n02x0s');
|
2018-05-18 15:31:22 +02:00
|
|
|
this.football = R3.EntityManager.Instance.findComponentById('umgbzb0ur2');
|
2018-05-20 22:50:22 +02:00
|
|
|
this.goalL = R3.EntityManager.Instance.findComponentById('ackykfwyd4');
|
|
|
|
this.goalR = R3.EntityManager.Instance.findComponentById('040yhx0atm');
|
|
|
|
this.goalWithHoles = R3.EntityManager.Instance.findComponentById('p3oxe63wjm');
|
2018-05-17 12:42:33 +02:00
|
|
|
|
2018-05-18 15:31:22 +02:00
|
|
|
/**
|
|
|
|
* Geometry
|
|
|
|
*/
|
|
|
|
this.ballGeometry = R3.EntityManager.Instance.findComponentById('dt6jl8kocw');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Materials
|
|
|
|
*/
|
|
|
|
this.materialWhite = R3.EntityManager.Instance.findComponentById('iualutcl1y');
|
|
|
|
this.materialBlack = R3.EntityManager.Instance.findComponentById('2g36po454g');
|
2018-05-17 15:37:56 +02:00
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
/**
|
|
|
|
* Scenes
|
|
|
|
*/
|
|
|
|
this.arScene = R3.EntityManager.Instance.findComponentById('grb0df22dd');
|
|
|
|
|
2018-05-17 12:42:33 +02:00
|
|
|
this.playerRotation = {
|
|
|
|
x : 0,
|
|
|
|
y : 0
|
|
|
|
};
|
|
|
|
|
2018-05-18 15:31:22 +02:00
|
|
|
this.balls = [];
|
|
|
|
|
|
|
|
this.activeBall = this.football.instance;
|
2018-05-17 15:37:56 +02:00
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
this.canKick = false;
|
|
|
|
|
|
|
|
this.kick = false;
|
|
|
|
|
|
|
|
this.startTime = null;
|
|
|
|
|
|
|
|
this.kickDuration = 0;
|
|
|
|
|
|
|
|
this.kickDistance = 0;
|
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
this.raycaster = new THREE.Raycaster();
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-23 08:22:37 +02:00
|
|
|
this.debug = true;
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
this.gravity = -9.8;
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-21 16:14:37 +02:00
|
|
|
this.initialSpeed = 5;
|
|
|
|
|
|
|
|
this.startRayZ = -2;
|
|
|
|
|
|
|
|
this.endRayZ = -2.5;
|
|
|
|
|
2018-05-23 08:25:55 +02:00
|
|
|
this.playerY = 0.375;
|
2018-05-21 16:14:37 +02:00
|
|
|
|
2018-05-23 08:22:37 +02:00
|
|
|
this.ballY = this.football.instance.geometry.boundingSphere.radius / 2;
|
2018-05-21 14:40:51 +02:00
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-18 15:31:22 +02:00
|
|
|
R3.CustomCode.prototype.render = function(delta) {
|
2018-05-17 15:37:56 +02:00
|
|
|
|
2018-05-18 15:31:22 +02:00
|
|
|
this.balls.map(
|
|
|
|
|
2018-05-20 13:36:43 +02:00
|
|
|
function(ball) {
|
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
if (ball.stopped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ball.speed <= 0) {
|
|
|
|
ball.stopped = true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 16:14:37 +02:00
|
|
|
ball.speed -= delta / 2;
|
2018-05-20 22:50:22 +02:00
|
|
|
|
2018-05-21 16:14:37 +02:00
|
|
|
ball.t += delta / 2;
|
2018-05-20 13:36:43 +02:00
|
|
|
|
|
|
|
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;
|
2018-05-20 22:50:22 +02:00
|
|
|
ball.mesh.position.y = ball.s0.y + u.y * ball.t + 0.5 * (this.gravity) * ball.t * ball.t;
|
2018-05-20 13:36:43 +02:00
|
|
|
ball.mesh.position.z = ball.s0.z + u.z * ball.t;
|
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
/**
|
|
|
|
* Bounce
|
|
|
|
*/
|
2018-05-21 16:14:37 +02:00
|
|
|
if (ball.mesh.position.y <= this.ballY) {
|
2018-05-20 13:36:43 +02:00
|
|
|
ball.s0.x = ball.mesh.position.x;
|
2018-05-21 16:14:37 +02:00
|
|
|
ball.s0.y = this.ballY;
|
2018-05-20 13:36:43 +02:00
|
|
|
ball.s0.z = ball.mesh.position.z;
|
|
|
|
ball.upAngle *= ball.bounciness;
|
|
|
|
ball.t = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
/**
|
|
|
|
* 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';
|
2018-05-21 00:06:04 +02:00
|
|
|
R3.Event.Emit(
|
|
|
|
R3.Event.PLAY_AUDIO,
|
|
|
|
{
|
|
|
|
name : 'Audio - Crowd Cheer'
|
|
|
|
}
|
|
|
|
);
|
2018-05-20 22:50:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
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';
|
2018-05-21 00:06:04 +02:00
|
|
|
R3.Event.Emit(
|
|
|
|
R3.Event.PLAY_AUDIO,
|
|
|
|
{
|
|
|
|
name : 'Audio - Crowd Cheer'
|
|
|
|
}
|
|
|
|
);
|
2018-05-20 22:50:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
/**
|
|
|
|
* 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 &&
|
2018-05-21 16:14:37 +02:00
|
|
|
ball.mesh.position.z < this.startRayZ &&
|
|
|
|
ball.mesh.position.z > this.endRayZ
|
2018-05-20 22:50:22 +02:00
|
|
|
) {
|
|
|
|
|
|
|
|
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);
|
2018-05-20 13:36:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
}.bind(this)
|
2018-05-18 15:31:22 +02:00
|
|
|
)
|
2018-05-17 15:37:56 +02:00
|
|
|
};
|
|
|
|
|
2018-05-17 12:42:33 +02:00
|
|
|
R3.CustomCode.prototype.move = function(data) {
|
|
|
|
|
2018-05-21 15:02:20 +02:00
|
|
|
var x = data.x || 0;
|
|
|
|
var y = data.y || 0;
|
2018-05-21 14:53:17 +02:00
|
|
|
x *= 0.0025;
|
|
|
|
y *= 0.0075;
|
2018-05-17 12:42:33 +02:00
|
|
|
|
|
|
|
this.playerRotation.x += x;
|
|
|
|
this.playerRotation.y += y;
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
/**
|
|
|
|
* Clamp player rotation to values between 0 and Math.PI * 2
|
|
|
|
*/
|
|
|
|
if (this.playerRotation.y > Math.PI) {
|
|
|
|
this.playerRotation.y = Math.PI;
|
|
|
|
}
|
2018-05-17 15:37:56 +02:00
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
if (this.playerRotation.y < -Math.PI) {
|
|
|
|
this.playerRotation.y = -Math.PI;
|
|
|
|
}
|
2018-05-17 15:37:56 +02:00
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
if (this.playerRotation.x > Math.PI / 2) {
|
|
|
|
this.playerRotation.x = Math.PI / 2;
|
2018-05-17 15:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
if (this.playerRotation.x < -Math.PI / 2) {
|
|
|
|
this.playerRotation.x = -Math.PI / 2;
|
2018-05-17 15:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
if (this.playerRotation.y <= 0) {
|
|
|
|
this.canKick = true;
|
2018-05-17 15:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
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;
|
2018-05-18 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
|
|
|
|
if (this.canKick && this.activeBall === null) {
|
|
|
|
|
|
|
|
this.activeBall = new THREE.Mesh(
|
|
|
|
this.ballGeometry.instance,
|
|
|
|
[
|
|
|
|
this.materialWhite.instance,
|
|
|
|
this.materialBlack.instance
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
this.activeBall.scale.copy(this.football.instance.scale);
|
2018-05-21 16:14:37 +02:00
|
|
|
this.activeBall.position.y = this.ballY;
|
2018-05-20 16:20:34 +02:00
|
|
|
|
|
|
|
this.football.parentScene.instance.add(this.activeBall);
|
2018-05-18 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 12:42:33 +02:00
|
|
|
this.footballPlayer.instance.matrix = new THREE.Matrix4();
|
|
|
|
this.footballPlayer.instance.matrix.decompose(
|
|
|
|
this.footballPlayer.instance.position,
|
|
|
|
this.footballPlayer.instance.quaternion,
|
|
|
|
this.footballPlayer.instance.scale
|
|
|
|
);
|
|
|
|
|
2018-05-21 16:14:37 +02:00
|
|
|
this.footballPlayer.instance.translateY(this.playerY);
|
2018-05-17 12:42:33 +02:00
|
|
|
|
|
|
|
this.footballPlayer.instance.rotateY(this.playerRotation.x);
|
|
|
|
|
2018-05-21 16:14:37 +02:00
|
|
|
this.footballPlayer.instance.translateZ(this.ballY);
|
2018-05-17 12:42:33 +02:00
|
|
|
|
|
|
|
this.footballPlayer.instance.rotateX(this.playerRotation.y);
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
if (this.kick) {
|
2018-05-17 12:42:33 +02:00
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
this.kick = false;
|
|
|
|
|
|
|
|
if (this.startTime === null) {
|
2018-05-18 15:31:22 +02:00
|
|
|
console.warn('no kick start time');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-21 00:06:04 +02:00
|
|
|
R3.Event.Emit(
|
|
|
|
R3.Event.PLAY_AUDIO,
|
|
|
|
{
|
|
|
|
name : 'Audio - Kick'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
var kickDuration = Date.now() - this.startTime;
|
2018-05-18 15:31:22 +02:00
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
var speed = this.kickDistance / kickDuration * 20;
|
2018-05-18 15:31:22 +02:00
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
// if (speed > 1.5) {
|
|
|
|
// speed = 1.5;
|
|
|
|
// }
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
// console.log(speed);
|
2018-05-20 13:36:43 +02:00
|
|
|
|
2018-05-18 15:31:22 +02:00
|
|
|
var v = new THREE.Vector3(0, 0, -1);
|
|
|
|
|
2018-05-20 16:20:34 +02:00
|
|
|
v.multiplyScalar(speed);
|
2018-05-18 15:31:22 +02:00
|
|
|
|
|
|
|
v.applyAxisAngle(
|
|
|
|
new THREE.Vector3(0,1,0),
|
2018-05-20 16:20:34 +02:00
|
|
|
this.playerRotation.x
|
2018-05-18 15:31:22 +02:00
|
|
|
);
|
|
|
|
|
2018-05-20 22:50:22 +02:00
|
|
|
var vRotationAxis = new THREE.Vector3(
|
|
|
|
-v.z,
|
|
|
|
0,
|
|
|
|
v.x
|
|
|
|
).normalize();
|
|
|
|
|
2018-05-18 15:31:22 +02:00
|
|
|
this.balls.push(
|
|
|
|
{
|
2018-05-20 13:36:43 +02:00
|
|
|
mesh : this.activeBall,
|
2018-05-21 14:40:51 +02:00
|
|
|
speed : this.initialSpeed,
|
2018-05-20 13:36:43 +02:00
|
|
|
mouseYTravelDistance : this.kick.distance,
|
2018-05-20 16:20:34 +02:00
|
|
|
directionAngle : this.playerRotation.x + Math.PI,
|
|
|
|
kickDuration : this.kickDuration,
|
|
|
|
kickDistance : this.kickDistance,
|
|
|
|
kickSpeed : speed,
|
|
|
|
upAngle : Math.PI * speed,
|
2018-05-20 13:36:43 +02:00
|
|
|
v : v,
|
2018-05-20 22:50:22 +02:00
|
|
|
vRotationAxis : vRotationAxis,
|
2018-05-20 13:36:43 +02:00
|
|
|
t : 0,
|
2018-05-21 16:14:37 +02:00
|
|
|
s0 : new THREE.Vector3(0,this.ballY,0),
|
|
|
|
s1 : new THREE.Vector3(0,this.ballY,0),
|
2018-05-20 22:50:22 +02:00
|
|
|
bounciness : 0.5,
|
|
|
|
scored : false,
|
|
|
|
scoredThroughGoal : null,
|
|
|
|
stopped :false
|
2018-05-18 15:31:22 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.activeBall = null;
|
|
|
|
}
|
|
|
|
|
2018-05-17 12:42:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
2018-05-21 16:14:37 +02:00
|
|
|
this.footballPlayer.instance.translateY(this.playerY);
|
|
|
|
this.football.instance.position.y = this.ballY;
|
2018-05-20 22:50:22 +02:00
|
|
|
|
2018-05-24 14:16:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
R3.Event.Subscribe(
|
|
|
|
R3.Event.GAME_START,
|
|
|
|
function() {
|
|
|
|
|
|
|
|
R3.Event.Emit(
|
|
|
|
R3.Event.PLAY_AUDIO,
|
|
|
|
{
|
|
|
|
name : 'Audio - Crowd'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.mouseMove.entityLoaded = this;
|
|
|
|
this.touchMove.entityLoaded = this;
|
|
|
|
this.beforeRender.entityLoaded = this;
|
2018-05-21 00:06:04 +02:00
|
|
|
}
|
|
|
|
);
|
2018-05-20 22:50:22 +02:00
|
|
|
|
2018-05-24 14:16:53 +02:00
|
|
|
R3.Event.Emit(R3.Event.GAME_LOADED);
|
|
|
|
|
2018-05-21 15:04:07 +02:00
|
|
|
//@ sourceURL=entityLoaded.js
|