r3-legacy/bak/r3-d3-game.js

432 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2017-01-10 17:04:30 +01:00
/**
* Game Runtime
2018-04-09 10:05:13 +02:00
* @param graphics R3.D3.Graphics
* @param apiGame R3.D3.API.Game
2017-01-10 17:04:30 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game = function (
2017-01-10 17:04:30 +01:00
graphics,
2017-02-21 18:55:18 +01:00
apiGame
2017-01-10 17:04:30 +01:00
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2017-01-12 17:40:17 +01:00
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiGame)) {
2017-01-12 17:40:17 +01:00
apiGame = {};
}
2018-04-09 10:05:13 +02:00
if (apiGame instanceof R3.D3.Game) {
return apiGame;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.Game.call(
2017-01-10 17:04:30 +01:00
this,
apiGame.id,
apiGame.name,
2017-01-20 13:40:27 +01:00
apiGame.baseUrl,
apiGame.path,
2017-02-21 18:55:18 +01:00
apiGame.imageFactory,
2017-01-10 17:04:30 +01:00
apiGame.gameType,
apiGame.cameras,
apiGame.composers,
2017-01-12 04:44:01 +01:00
apiGame.viewports,
2017-01-20 13:40:27 +01:00
apiGame.renderers,
apiGame.renderTargets,
apiGame.systems,
2017-01-10 17:04:30 +01:00
apiGame.entityManager,
2017-01-20 13:40:27 +01:00
apiGame.parentEntity
2017-01-10 17:04:30 +01:00
);
2018-04-09 10:05:13 +02:00
if (this.imageFactory instanceof R3.D3.API.ImageFactory) {
this.imageFactory = new R3.D3.ImageFactory(
2017-01-17 17:16:10 +01:00
this.graphics,
2017-02-21 18:55:18 +01:00
this.imageFactory
2017-01-17 17:16:10 +01:00
);
}
2017-01-20 13:40:27 +01:00
this.cameras = this.cameras.map(
function (apiCamera) {
2018-04-09 10:05:13 +02:00
if (apiCamera instanceof R3.D3.API.Camera) {
return new R3.D3.Camera(
2017-01-20 13:40:27 +01:00
this.graphics,
apiCamera
)
} else {
console.warn('apiCamera not of type API.Camera');
throw new Error('apiCamera not of type API.Camera');
}
2017-01-10 17:04:30 +01:00
}.bind(this)
);
2017-01-20 13:40:27 +01:00
this.composers = this.composers.map(
function (apiComposer) {
2018-04-09 10:05:13 +02:00
if (apiComposer instanceof R3.D3.API.Composer) {
return new R3.D3.Composer(
2017-01-20 13:40:27 +01:00
this.graphics,
apiComposer
)
} else {
console.warn('apiComposer not of type API.Composer');
throw new Error('apiComposer not of type API.Composer');
}
}.bind(this)
);
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
this.viewports = this.viewports.map(
function (apiViewport) {
2018-04-09 10:05:13 +02:00
if (apiViewport instanceof R3.D3.API.Viewport) {
return new R3.D3.Viewport(
2017-01-20 13:40:27 +01:00
this.graphics,
apiViewport
)
} else {
console.warn('apiViewport not of type API.Viewport');
throw new Error('apiViewport not of type API.Viewport');
}
}.bind(this)
);
2017-01-10 17:04:30 +01:00
2017-01-20 13:40:27 +01:00
this.renderers = this.renderers.map(
function (apiRenderer) {
2018-04-09 10:05:13 +02:00
if (apiRenderer instanceof R3.D3.API.Renderer) {
return new R3.D3.Renderer(
2017-01-20 13:40:27 +01:00
this.graphics,
apiRenderer
)
} else {
console.warn('apiRenderer not of type API.Renderer');
throw new Error('apiRenderer not of type API.Renderer');
}
}.bind(this)
);
2017-01-10 17:04:30 +01:00
2017-01-20 13:40:27 +01:00
this.renderTargets = this.renderTargets.map(
function (apiRenderTarget) {
2018-04-09 10:05:13 +02:00
if (apiRenderTarget instanceof R3.D3.API.RenderTarget) {
return new R3.D3.RenderTarget(
2017-01-20 13:40:27 +01:00
this.graphics,
apiRenderTarget
)
} else {
console.warn('apiRenderTarget not of type API.RenderTarget');
throw new Error('apiRenderTarget not of type API.RenderTarget');
}
}.bind(this)
);
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
this.systems = this.systems.map(
function (apiSystem) {
2018-04-09 10:05:13 +02:00
if (apiSystem instanceof R3.API.System) {
return new R3.System(
2017-01-20 13:40:27 +01:00
this.graphics,
apiSystem
)
} else {
console.warn('apiSystem not of type API.System');
throw new Error('apiSystem not of type API.System');
}
}.bind(this)
);
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.entityManager) {
2018-04-09 10:05:13 +02:00
if (this.entityManager instanceof R3.API.EntityManager) {
this.entityManager = new R3.EntityManager(
2017-01-20 13:40:27 +01:00
this.graphics,
this.entityManager
);
} else {
console.warn('entityManager not of type API.EntityManager');
throw new Error('entityManager not of type API.EntityManager');
}
2017-01-19 17:50:11 +01:00
}
2017-01-17 17:16:10 +01:00
this.buildIdToObject();
2017-01-19 17:50:11 +01:00
2017-02-22 16:06:27 +01:00
this.linkObjects(this.idToObject);
2017-01-10 17:04:30 +01:00
};
2018-04-09 10:05:13 +02:00
R3.D3.Game.prototype = Object.create(R3.D3.API.Game.prototype);
R3.D3.Game.prototype.constructor = R3.D3.Game;
2017-01-12 04:44:01 +01:00
2018-04-09 10:05:13 +02:00
R3.D3.Game.GAME_TYPE_VR_PONG = 0x1;
R3.D3.Game.GAME_TYPE_VR_RACER = 0x2;
2017-01-10 17:04:30 +01:00
2017-01-12 04:44:01 +01:00
/**
* Creates a camera instance of 'graphics' type (only THREE for now)
* @returns {THREE.Game}
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game.prototype.createInstance = function(update) {
2017-01-12 04:44:01 +01:00
var instance = null;
if (update) {
instance = this.instance;
}
return instance;
2017-01-10 17:04:30 +01:00
};
2018-04-09 10:05:13 +02:00
// R3.D3.Game.prototype.setSize = function(width, height) {
2017-01-13 16:19:51 +01:00
//
// // var w = 0;
// // var h = 0;
//
// this.viewports.map(
// function(viewport) {
// // w = viewport.width;
// // h = viewport.height;
// //
// // //TODO : calculate width and height decrease or increase ratio and adjust viewport x and y offset according
// // var wx = width / w;
// // var hx = height / h;
//
// viewport.width = width;
// viewport.height = height;
//
// viewport.updateInstance();
// }
// )
//
// };
2017-01-12 17:40:17 +01:00
2017-01-12 04:44:01 +01:00
/**
* Updates the instance with the current state
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game.prototype.updateInstance = function() {
2017-01-12 04:44:01 +01:00
this.instance = this.createInstance(true);
2017-01-10 17:04:30 +01:00
};
2017-01-12 04:44:01 +01:00
/**
2018-04-09 10:05:13 +02:00
* Converts a R3.D3.Game to a new R3.D3.API.Game
* @returns {R3.D3.API.Game}
2017-01-12 04:44:01 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game.prototype.toApiObject = function() {
2017-01-12 04:44:01 +01:00
2017-02-21 18:55:18 +01:00
var apiImageFactory = null;
2017-01-20 13:40:27 +01:00
var apiCameras = [];
var apiComposers = [];
var apiViewports = [];
var apiRenderers = [];
var apiRenderTargets = [];
var apiSystems = [];
var apiEntityManager = null;
2017-02-21 18:55:18 +01:00
2018-04-09 10:05:13 +02:00
if (this.imageFactory instanceof R3.D3.ImageFactory) {
2017-05-16 14:51:57 +02:00
apiImageFactory = this.imageFactory.toApiObject();
2017-02-21 18:55:18 +01:00
}
2017-01-20 13:40:27 +01:00
if (this.cameras) {
apiCameras = this.cameras.map(
function(camera) {
2018-04-09 10:05:13 +02:00
if (camera instanceof R3.D3.Camera) {
2017-05-16 14:51:57 +02:00
return camera.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('camera not an instance of Camera');
throw new Error('camera not an instance of Camera');
}
}
);
}
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.composers) {
apiComposers = this.composers.map(
function(composer) {
2018-04-09 10:05:13 +02:00
if (composer instanceof R3.D3.Composer) {
2017-05-16 14:51:57 +02:00
return composer.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('composer not an instance of Composer');
throw new Error('composer not an instance of Composer');
}
}
);
}
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.viewports) {
apiViewports = this.viewports.map(
function(viewport) {
2018-04-09 10:05:13 +02:00
if (viewport instanceof R3.D3.Viewport) {
2017-05-16 14:51:57 +02:00
return viewport.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('viewport not an instance of Viewport');
throw new Error('viewport not an instance of Viewport');
}
}
);
}
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.renderers) {
apiRenderers = this.renderers.map(
function(renderer) {
2018-04-09 10:05:13 +02:00
if (renderer instanceof R3.D3.Renderer) {
2017-05-16 14:51:57 +02:00
return renderer.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('renderer not an instance of Renderer');
throw new Error('renderer not an instance of Renderer');
}
}
);
}
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.renderTargets) {
apiRenderTargets = this.renderTargets.map(
function(renderTarget) {
2018-04-09 10:05:13 +02:00
if (renderTarget instanceof R3.D3.RenderTarget) {
2017-05-16 14:51:57 +02:00
return renderTarget.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('renderTarget not an instance of RenderTarget');
throw new Error('renderTarget not an instance of RenderTarget');
}
}
);
}
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.systems) {
apiSystems = this.systems.map(
function(system) {
2018-04-09 10:05:13 +02:00
if (system instanceof R3.System) {
2017-05-16 14:51:57 +02:00
return system.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('system not an instance of System');
throw new Error('system not an instance of System');
}
}
);
}
2017-01-12 04:44:01 +01:00
2017-01-20 13:40:27 +01:00
if (this.entityManager) {
2018-04-09 10:05:13 +02:00
if (this.entityManager instanceof R3.EntityManager) {
2017-05-16 14:51:57 +02:00
apiEntityManager = this.entityManager.toApiObject();
2017-01-20 13:40:27 +01:00
} else {
console.warn('entityManager not an instance of EntityManager');
throw new Error('entityManager not an instance of EntityManager');
}
}
2017-01-12 04:44:01 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.API.Game(
2017-01-12 04:44:01 +01:00
this.id,
this.name,
2017-01-17 17:16:10 +01:00
this.baseUrl,
this.path,
2017-02-21 18:55:18 +01:00
apiImageFactory,
2017-01-12 04:44:01 +01:00
this.gameType,
apiCameras,
apiComposers,
apiViewports,
2017-01-20 13:40:27 +01:00
apiRenderers,
apiRenderTargets,
apiSystems,
2017-01-12 04:44:01 +01:00
apiEntityManager,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.parentEntity)
2017-01-12 04:44:01 +01:00
);
2017-01-10 17:04:30 +01:00
};
2017-01-12 04:44:01 +01:00
/**
2018-04-09 10:05:13 +02:00
* Converts from an Object Game to a R3.D3.Game
* @param graphics R3.D3.Graphics
2017-01-12 04:44:01 +01:00
* @param objectGame Object
2018-04-09 10:05:13 +02:00
* @returns {R3.D3.Game}
2017-01-12 04:44:01 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game.FromObjectGame = function(graphics, objectGame) {
2017-01-12 04:44:01 +01:00
2018-04-09 10:05:13 +02:00
var apiGame = R3.D3.API.Game.FromObjectGame(objectGame);
2017-01-12 04:44:01 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.Game(
2017-01-12 04:44:01 +01:00
graphics,
apiGame
);
};
2017-01-18 16:03:44 +01:00
/**
* Loads a Game
* @param graphics
* @param objectGame
* @param onLoaded
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game.LoadGame = function(
2017-01-18 16:03:44 +01:00
graphics,
objectGame,
onLoaded
) {
2018-04-09 10:05:13 +02:00
var game = R3.D3.Game.FromObjectGame(
2017-01-18 16:03:44 +01:00
graphics,
objectGame
);
onLoaded(game);
};
/**
* Loads a Game from the API
2018-04-09 10:05:13 +02:00
* @param graphics R3.D3.Graphics
2017-01-18 16:03:44 +01:00
* @param partialGameObject Object
* @param onLoaded callback
* @param apiUrl
* @returns {*}
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Game.LoadGameFromApi = function(
2017-01-18 16:03:44 +01:00
graphics,
partialGameObject,
onLoaded,
apiUrl
) {
/**
* First check if this is a client or server side request
*/
if (typeof XMLHttpRequest == 'undefined') {
console.warn('Implement server side loading from API here');
return onLoaded(
null,
new Error('Not Implemented')
);
}
var xhr = new XMLHttpRequest();
xhr.open(
'GET',
apiUrl + '/game/load' + partialGameObject.path + '/' + partialGameObject.name
);
xhr.onreadystatechange = function(xhr) {
return function() {
if (xhr.readyState == 4) {
try {
var response = JSON.parse(xhr.responseText);
} catch (e) {
return onLoaded(null, new Error('Could not load game : ' + e.message));
}
if (!response.game || response.game.length == 0) {
return onLoaded(null, new Error('Could not load game'));
}
var objectGame = response.game[0];
2018-04-09 10:05:13 +02:00
R3.D3.Game.LoadGame(
2017-01-18 16:03:44 +01:00
graphics,
objectGame,
onLoaded
);
}
}
}(xhr);
xhr.send();
};
2017-05-09 15:44:29 +02:00
2018-04-09 10:05:13 +02:00
R3.D3.Game.prototype.setSize = function(width, height) {
2017-05-09 15:44:29 +02:00
this.renderers.map(
function(renderer) {
renderer.setSize(width, height);
}
);
};