Skip to content
Snippets Groups Projects
Commit 64de7979 authored by sermigu's avatar sermigu
Browse files

Subir nuevo archivo

parent 8c135fb6
No related branches found
No related tags found
No related merge requests found
barras.js 0 → 100644
var scene, renderer, camera;
var controls, boxHelper;
var animator = new Array();
var foto= new Array();
var clock = new THREE.Clock();
var aliados,enemigos,obstaculos;
function init() {
var oReq = new XMLHttpRequest();
oReq.open("GET", "https://firebasestorage.googleapis.com/v0/b/busbot-empc.appspot.com/o/capitulo5.txt?alt=media", false);
oReq.send();
var datos=oReq.responseText;
THREE.Object3D.DefaultUp.set(0, 1, 0);
// create a scene, that will hold all our elements such as objects, cameras and lights.
scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
camera = new THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 0.1, 2000);
// create a render and set the size
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0x000000));
renderer.setSize(window.innerWidth, window.innerHeight);
// show axes in the screen
var axes = new THREE.AxesHelper(20);
scene.add(axes);
datos=datos.split("\n");
var dimx=parseInt(datos[0]);
var dimy=parseInt(datos[1]);
enemigos=parseInt(datos[2]);
var puntero=3;
aliados=parseInt(datos[puntero+enemigos]);
obstaculos=parseInt(datos[puntero+enemigos+aliados+1]);
var i=0;
var ataques=new Array(dimx*dimy);
var casillasObstaculo=new Array(dimx*dimy);
var ataquesEnemigo=new Array(dimx*dimy);
for (i=0; i<ataques.length; i++){
ataques[i]=0;
casillasObstaculo[i]=0;
ataquesEnemigo[i]=0;
}
var texturet = new THREE.TextureLoader().load(
'https://firebasestorage.googleapis.com/v0/b/busbot-empc.appspot.com/o/skybox.jpg?alt=media',
() => {
var rt = new THREE.WebGLCubeRenderTarget(2048/*texturet.image.height*/);
rt.fromEquirectangularTexture(renderer, texturet);
scene.background = rt;
});
puntero=puntero+enemigos+aliados+2;
for (i=0; i<obstaculos; i++){
var obstaculo=datos[puntero];
obstaculo=obstaculo.split(" ");
var posx=parseInt(obstaculo[0]);
var posy=parseInt(obstaculo[1]);
casillasObstaculo[posx*dimy+posy]=1;
createCube(scene,posx+0.5,0.5*1,posy+0.5,1,1,1,0xffffff,0x000000);
puntero++;
}
puntero=3;
for (i=0; i<enemigos; i++){
var enemigo=datos[puntero];
enemigo=enemigo.split(" ");
var clase=enemigo[0];
var posx=parseInt(enemigo[1]);
var posy=parseInt(enemigo[2]);
var movimiento=parseInt(enemigo[3]);
var rango=parseInt(enemigo[4]);
var distancia=movimiento+rango;
calcularMovimiento(posx, posy, distancia, ataquesEnemigo, dimx, dimy,casillasObstaculo);
if(ataquesEnemigo[125]!=0) console.log(posx+" "+posy)
for (var j=0; j<ataques.length; j++){
ataques[j]=ataques[j]+ataquesEnemigo[j];
ataquesEnemigo[j]=0;
}
puntero++;
}
createFloor(dimx, dimy);
const gridHelper=new THREE.GridHelper(40, 40);
scene.add(gridHelper);
var texture, materialg, geometryg;
puntero=3+enemigos+1;
for (i=0; i<aliados; i++){
var aliado=datos[puntero];
aliado=aliado.split(" ");
var nombre=aliado[0];
var posx=parseInt(aliado[1]);
var posy=parseInt(aliado[2]);
var spriteDim=parseInt(aliado[3]);
if(ataques[posx*dimy+posy]!=0){
createCube2(scene,posx+0.5,0.5*ataques[posx*dimy+posy],posy+0.5,1,ataques[posx*dimy+posy],1,0x1176c5,0x000000,0xff0000);
var spriteDuration = spriteDim==6 ? 200 : 300;
texture=new THREE.TextureLoader().load('https://firebasestorage.googleapis.com/v0/b/busbot-empc.appspot.com/o/'+nombre+'.png?alt=media');
animator[i]=new TextureAnimator(texture, spriteDim, 1, spriteDim, spriteDuration); // texture, #horiz, #vert, #total, duration.
materialg=new THREE.SpriteMaterial({map: texture, transparent: true, side: THREE.DoubleSide});
foto[i]=new THREE.Sprite(materialg);
foto[i].position.set(posx+0.5,ataques[posx*dimy+posy]+0.5,posy+0.5);
foto[i].scale.set(1, 1, 1.0);
scene.add(foto[i]);
ataques[posx*dimy+posy]=0;
}
else{
createCube(scene,posx+0.5,0.5*1,posy+0.5,1,1,1,0x1176c5,0x000000);
var spriteDuration = spriteDim==6 ? 200 : 300;
texture=new THREE.TextureLoader().load('https://firebasestorage.googleapis.com/v0/b/busbot-empc.appspot.com/o/'+nombre+'.png?alt=media');
animator[i]=new TextureAnimator(texture, spriteDim, 1, spriteDim, spriteDuration); // texture, #horiz, #vert, #total, duration.
materialg=new THREE.SpriteMaterial({map: texture, transparent: true, side: THREE.DoubleSide});
foto[i]=new THREE.Sprite(materialg);
foto[i].position.set(posx+0.5,1.5,posy+0.5);
foto[i].scale.set(1, 1, 1.0);
scene.add(foto[i]);
}
puntero++;
}
for(i=0;i<dimx*dimy;i++){
if(ataques[i]==0){
continue;
}
createCube(scene,Math.floor(i/dimy)+0.5,0.5*ataques[i],i%dimy+0.5,1,ataques[i],1,0xff0000,0x000000);
}
puntero=3;
for (i=0; i<enemigos; i++){
var enemigo=datos[puntero];
enemigo=enemigo.split(" ");
var clase=enemigo[0];
var posx=parseInt(enemigo[1]);
var posy=parseInt(enemigo[2]);
puntero++;
texture=new THREE.TextureLoader().load('https://firebasestorage.googleapis.com/v0/b/busbot-empc.appspot.com/o/'+clase+'.png?alt=media');
animator[i+aliados]=new TextureAnimator(texture, 6, 1, 6, 200); // texture, #horiz, #vert, #total, duration.
materialg=new THREE.SpriteMaterial({map: texture, transparent: true, side: THREE.DoubleSide});
foto[i+aliados]=new THREE.Sprite(materialg);
foto[i+aliados].position.set(posx+0.5,ataques[posx*dimy+posy]+0.5,posy+0.5);
foto[i+aliados].scale.set(1, 1, 1.0);
scene.add(foto[i+aliados]);
}
createLight();
//position and point the camera to the center of the scene
camera.position.set(25, 20, 25);
camera.lookAt(scene.position);
//add the output of the renderer to the html element
document.getElementById("contenedor").appendChild(renderer.domElement);
//controls
createControls();
//render the scene
scene.translateX(-dimx/2);
scene.translateZ(-dimy/2);
renderer.render(scene, camera);
}
function calcularMovimiento(posx, posy, distancia, ataquesEnemigo, dimx, dimy, casillasObstaculo){
if(distancia==0){
return 0;
}
if(!(posx+1>=dimx)){
if(casillasObstaculo[(posx+1)*dimy+posy]!=1){
ataquesEnemigo[(posx+1)*dimy+posy]=1;
calcularMovimiento(posx+1, posy, distancia-1, ataquesEnemigo, dimx, dimy, casillasObstaculo);
}
}
if(!(posx-1<0)){
if(casillasObstaculo[(posx-1)*dimy+posy]!=1){
ataquesEnemigo[(posx-1)*dimy+posy]=1;
calcularMovimiento(posx-1, posy, distancia-1, ataquesEnemigo, dimx, dimy, casillasObstaculo);
}
}
if(!(posy+1>=dimy)){
if(casillasObstaculo[posx*dimy+posy+1]!=1){
ataquesEnemigo[posx*dimy+posy+1]=1;
calcularMovimiento(posx, posy+1, distancia-1, ataquesEnemigo, dimx, dimy, casillasObstaculo);
}
}
if(!(posy-1<0)){
if(casillasObstaculo[posx*dimy+posy-1]!=1){
ataquesEnemigo[posx*dimy+posy-1]=1;
calcularMovimiento(posx, posy-1, distancia-1, ataquesEnemigo, dimx, dimy, casillasObstaculo);
}
}
return ataquesEnemigo;
}
function createFloor(dimx,dimy) {
var planeGeometry = new THREE.PlaneGeometry(dimx, dimy);
var groundTexture = new THREE.TextureLoader().load('https://firebasestorage.googleapis.com/v0/b/busbot-empc.appspot.com/o/piedras.jpg?alt=media');
var planeMaterial = new THREE.MeshBasicMaterial({
map: groundTexture
});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
// rotate and position the plane
plane.rotation.x = -0.5 * Math.PI;
plane.position.set(dimx/2, 0, dimy/2);
// add the plane to the scene
scene.add(plane);
}
function createLight() {
var ambient = new THREE.AmbientLight(0x999999);
var spot = new THREE.SpotLight({
color: 0xffffff,
intensity: 0.1
});
spot.position.set(-50, 100, 100);
spot.castShadow = true;
spot.shadowDarkness = 0.2;
scene.add(ambient, spot);
}
function createCube(scene,x,y,z,dimx,dimy,dimz,color,colorhelper) {
var cubeGeometry = new THREE.BoxGeometry(dimx, dimy, dimz);
var cubeMaterial = new THREE.MeshLambertMaterial({
color: color
});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
// position the cube
cube.position.x = x;
cube.position.y = y;
cube.position.z = z;
scene.add(cube);
var helper = new THREE.BoxHelper(cube);
helper.material.color.set(colorhelper);
scene.add(helper);
}
function createCube2(scene,x,y,z,dimx,dimy,dimz,color,colorhelper, colorface) {
var cubeGeometry = new THREE.BoxGeometry(dimx, dimy, dimz);
cubeGeometry.faces[0].color.setHex(color);
cubeGeometry.faces[1].color.setHex(colorface);
cubeGeometry.faces[2].color.setHex(colorface);
cubeGeometry.faces[3].color.setHex(color);
cubeGeometry.faces[4].color.setHex(color);
cubeGeometry.faces[5].color.setHex(color);
cubeGeometry.faces[6].color.setHex(color);
cubeGeometry.faces[7].color.setHex(color);
cubeGeometry.faces[8].color.setHex(colorface);
cubeGeometry.faces[9].color.setHex(color);
cubeGeometry.faces[10].color.setHex(colorface);
cubeGeometry.faces[11].color.setHex(color);
var cubeMaterial = new THREE.MeshLambertMaterial({
color: 0xffffff,
vertexColors: true
});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.geometry.colorsNeedUpdate = true;
cube.castShadow = true;
// position the cube
cube.position.x = x;
cube.position.y = y;
cube.position.z = z;
scene.add(cube);
var helper = new THREE.BoxHelper(cube);
helper.material.color.set(colorhelper);
scene.add(helper);
}
function createControls() {
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.keys = [ 65, 83, 68 ];
}
function animate() {
requestAnimationFrame( animate );
render();
update();
}
function render() {
renderer.render( scene, camera );
}
function reqListener () {
console.log(this.responseText);
}
function update(){
var delta = clock.getDelta();
for(var i=0;i<aliados+enemigos;i++){
animator[i].update(1000 * delta);
}
controls.update();
}
function TextureAnimator(texture, tilesHoriz, tilesVert, numTiles, tileDispDuration){
// note: texture passed by reference, will be updated by the update function.
this.tilesHorizontal = tilesHoriz;
this.tilesVertical = tilesVert;
// how many images does this spritesheet contain?
// usually equals tilesHoriz * tilesVert, but not necessarily,
// if there at blank tiles at the bottom of the spritesheet.
this.numberOfTiles = numTiles;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 1 / this.tilesHorizontal, 1 / this.tilesVertical );
// how long should each image be displayed?
this.tileDisplayDuration = tileDispDuration;
// how long has the current image been displayed?
this.currentDisplayTime = 0;
// which image is currently being displayed?
this.currentTile = 0;
this.update = function( milliSec )
{
this.currentDisplayTime += milliSec;
while (this.currentDisplayTime > this.tileDisplayDuration)
{
this.currentDisplayTime -= this.tileDisplayDuration;
this.currentTile++;
if (this.currentTile == this.numberOfTiles)
this.currentTile = 0;
var currentColumn = this.currentTile % this.tilesHorizontal;
texture.offset.x = currentColumn / this.tilesHorizontal;
var currentRow = Math.floor( this.currentTile / this.tilesHorizontal );
texture.offset.y = currentRow / this.tilesVertical;
}
};
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment