mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 18:26:26 +02:00
Update shortbow to use spawner entities to specify position/rotation at runtime
This commit is contained in:
parent
c8ec5cbf5d
commit
4c310e1048
2 changed files with 79 additions and 23 deletions
|
@ -41,6 +41,7 @@ var ESCAPE_SOUND = SoundCache.getSound(Script.resolvePath("sounds/escape.wav"));
|
||||||
const STARTING_NUMBER_OF_LIVES = 6;
|
const STARTING_NUMBER_OF_LIVES = 6;
|
||||||
const ENEMIES_PER_WAVE_MULTIPLIER = 2;
|
const ENEMIES_PER_WAVE_MULTIPLIER = 2;
|
||||||
const POINTS_PER_KILL = 100;
|
const POINTS_PER_KILL = 100;
|
||||||
|
const ENEMY_SPEED = 3.0;
|
||||||
|
|
||||||
// Encode a set of key-value pairs into a param string. Does NOT do any URL escaping.
|
// Encode a set of key-value pairs into a param string. Does NOT do any URL escaping.
|
||||||
function encodeURLParams(params) {
|
function encodeURLParams(params) {
|
||||||
|
@ -78,6 +79,28 @@ function sendAndUpdateHighScore(entityID, score, wave, numPlayers, onResposeRece
|
||||||
request.send();
|
request.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findChildrenWithName(parentID, name) {
|
||||||
|
var childrenIDs = Entities.getChildrenIDs(parentID);
|
||||||
|
var matchingIDs = [];
|
||||||
|
for (var i = 0; i < childrenIDs.length; ++i) {
|
||||||
|
var id = childrenIDs[i];
|
||||||
|
var childName = Entities.getEntityProperties(id, 'name').name;
|
||||||
|
if (childName === name) {
|
||||||
|
matchingIDs.push(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matchingIDs;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPropertiesForEntities(entityIDs, properties) {
|
||||||
|
var properties = [];
|
||||||
|
for (var i = 0; i < entityIDs.length; ++i) {
|
||||||
|
properties.push(Entities.getEntityProperties(entityIDs[i], properties));
|
||||||
|
}
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var baseEnemyProperties = {
|
var baseEnemyProperties = {
|
||||||
"name": "SB.Enemy",
|
"name": "SB.Enemy",
|
||||||
"damping": 0,
|
"damping": 0,
|
||||||
|
@ -116,19 +139,8 @@ var baseEnemyProperties = {
|
||||||
"y": -0.54996079206466675,
|
"y": -0.54996079206466675,
|
||||||
"z": -0.54996079206466675
|
"z": -0.54996079206466675
|
||||||
},
|
},
|
||||||
"rotation": {
|
|
||||||
"w": 0.52459806203842163,
|
|
||||||
"x": 0.3808099627494812,
|
|
||||||
"y": -0.16060420870780945,
|
|
||||||
"z": 0.74430292844772339
|
|
||||||
},
|
|
||||||
"shapeType": "sphere",
|
"shapeType": "sphere",
|
||||||
"type": "Model",
|
"type": "Model",
|
||||||
"velocity": {
|
|
||||||
"x": 0,
|
|
||||||
"y": 0,
|
|
||||||
"z": -3
|
|
||||||
},
|
|
||||||
"script": Script.resolvePath('enemyClientEntity.js'),
|
"script": Script.resolvePath('enemyClientEntity.js'),
|
||||||
"serverScripts": Script.resolvePath('enemyServerEntity.js')
|
"serverScripts": Script.resolvePath('enemyServerEntity.js')
|
||||||
};
|
};
|
||||||
|
@ -269,11 +281,14 @@ ShortbowGameManager.prototype = {
|
||||||
Entities.editEntity(this.startButtonID, { visible: false });
|
Entities.editEntity(this.startButtonID, { visible: false });
|
||||||
|
|
||||||
// Spawn bows
|
// Spawn bows
|
||||||
for (var i = 0; i < this.bowPositions.length; ++i) {
|
var bowSpawnEntityIDs = findChildrenWithName(this.rootEntityID, 'SB.BowSpawn');
|
||||||
const bowPosition = Vec3.sum(this.rootPosition, this.bowPositions[i]);
|
var bowSpawnProperties = getPropertiesForEntities(bowSpawnEntityIDs, ['position', 'rotation']);
|
||||||
Vec3.print("Creating bow: " + i, this.bowPositions[i]);
|
for (var i = 0; i < bowSpawnProperties.length; ++i) {
|
||||||
|
const props = bowSpawnProperties[i];
|
||||||
|
Vec3.print("Creating bow: " + i, props.position);
|
||||||
this.bowIDs.push(Entities.addEntity({
|
this.bowIDs.push(Entities.addEntity({
|
||||||
"position": bowPosition,
|
"position": props.position,
|
||||||
|
"rotation": props.rotation,
|
||||||
"collisionsWillMove": 1,
|
"collisionsWillMove": 1,
|
||||||
"compoundShapeURL": Script.resolvePath("bow/bow_collision_hull.obj"),
|
"compoundShapeURL": Script.resolvePath("bow/bow_collision_hull.obj"),
|
||||||
"created": "2016-09-01T23:57:55Z",
|
"created": "2016-09-01T23:57:55Z",
|
||||||
|
@ -290,12 +305,6 @@ ShortbowGameManager.prototype = {
|
||||||
},
|
},
|
||||||
"modelURL": Script.resolvePath("bow/bow-deadly.fbx"),
|
"modelURL": Script.resolvePath("bow/bow-deadly.fbx"),
|
||||||
"name": "WG.Hifi-Bow",
|
"name": "WG.Hifi-Bow",
|
||||||
"rotation": {
|
|
||||||
"w": 0.9718012809753418,
|
|
||||||
"x": 0.15440607070922852,
|
|
||||||
"y": -0.10469216108322144,
|
|
||||||
"z": -0.14418250322341919
|
|
||||||
},
|
|
||||||
"script": Script.resolvePath("bow/bow.js"),
|
"script": Script.resolvePath("bow/bow.js"),
|
||||||
"shapeType": "compound",
|
"shapeType": "compound",
|
||||||
"type": "Model",
|
"type": "Model",
|
||||||
|
@ -349,10 +358,20 @@ ShortbowGameManager.prototype = {
|
||||||
print("Number of enemies:", numberOfEnemiesLeftToSpawn);
|
print("Number of enemies:", numberOfEnemiesLeftToSpawn);
|
||||||
this.checkEnemiesTimer = Script.setInterval(this.checkEnemies.bind(this), 100);
|
this.checkEnemiesTimer = Script.setInterval(this.checkEnemies.bind(this), 100);
|
||||||
|
|
||||||
|
var enemySpawnEntityIDs = findChildrenWithName(this.rootEntityID, 'SB.EnemySpawn');
|
||||||
|
var enemySpawnProperties = getPropertiesForEntities(enemySpawnEntityIDs, ['position', 'rotation']);
|
||||||
|
|
||||||
for (var i = 0; i < numberOfEnemiesLeftToSpawn; ++i) {
|
for (var i = 0; i < numberOfEnemiesLeftToSpawn; ++i) {
|
||||||
print("Adding enemy");
|
print("Adding enemy");
|
||||||
var idx = Math.floor(Math.random() * this.spawnPositions.length);
|
var idx = Math.floor(Math.random() * enemySpawnProperties.length);
|
||||||
this.spawnQueue.push({ spawnAt: currentDelay, position: Vec3.sum(this.rootPosition, this.spawnPositions[idx]) });
|
var props = enemySpawnProperties[idx];
|
||||||
|
this.spawnQueue.push({
|
||||||
|
spawnAt: currentDelay,
|
||||||
|
position: props.position,
|
||||||
|
rotation: props.rotation,
|
||||||
|
velocity: Vec3.multiply(ENEMY_SPEED, Quat.getFront(props.rotation))
|
||||||
|
|
||||||
|
});
|
||||||
currentDelay += delayBetweenSpawns;
|
currentDelay += delayBetweenSpawns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,6 +423,8 @@ ShortbowGameManager.prototype = {
|
||||||
var waveElapsedTime = Date.now() - this.spawnStartTime;
|
var waveElapsedTime = Date.now() - this.spawnStartTime;
|
||||||
while (this.spawnQueue.length > 0 && waveElapsedTime > this.spawnQueue[0].spawnAt) {
|
while (this.spawnQueue.length > 0 && waveElapsedTime > this.spawnQueue[0].spawnAt) {
|
||||||
baseEnemyProperties.position = this.spawnQueue[0].position;
|
baseEnemyProperties.position = this.spawnQueue[0].position;
|
||||||
|
baseEnemyProperties.rotation = this.spawnQueue[0].rotation;
|
||||||
|
baseEnemyProperties.velocity= this.spawnQueue[0].velocity;
|
||||||
|
|
||||||
baseEnemyProperties.userData = JSON.stringify({
|
baseEnemyProperties.userData = JSON.stringify({
|
||||||
gameChannel: this.commChannelName,
|
gameChannel: this.commChannelName,
|
||||||
|
|
|
@ -50,6 +50,22 @@ function spawnTemplate(templateName, overrides) {
|
||||||
return Entities.addEntity(properties);
|
return Entities.addEntity(properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function spawnTemplates(templateName, overrides) {
|
||||||
|
var templates = getTemplates(templateName);
|
||||||
|
if (template.length === 0) {
|
||||||
|
print("ERROR, unknown template name:", templateName);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
var spawnedEntities = [];
|
||||||
|
for (var i = 0; i < templates.length; ++i) {
|
||||||
|
print("Spawning: ", templateName);
|
||||||
|
var properties = mergeObjects(overrides, templates[i]);
|
||||||
|
spawnedEntities.push(Entities.addEntity(properties));
|
||||||
|
}
|
||||||
|
return spawnedEntities;
|
||||||
|
}
|
||||||
|
|
||||||
// TEMPLATES contains a dictionary of different named entity templates. An entity
|
// TEMPLATES contains a dictionary of different named entity templates. An entity
|
||||||
// template is just a list of properties.
|
// template is just a list of properties.
|
||||||
//
|
//
|
||||||
|
@ -63,6 +79,15 @@ function getTemplate(name) {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
function getTemplates(name) {
|
||||||
|
var templates = [];
|
||||||
|
for (var i = 0; i < TEMPLATES.length; ++i) {
|
||||||
|
if (TEMPLATES[i].name === name) {
|
||||||
|
templates.push(TEMPLATES[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return templates;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Cleanup Shortbow template data
|
// Cleanup Shortbow template data
|
||||||
|
@ -157,10 +182,20 @@ function createLocalGame() {
|
||||||
serverScripts: Script.resolvePath('shortbowServerEntity.js')
|
serverScripts: Script.resolvePath('shortbowServerEntity.js')
|
||||||
});
|
});
|
||||||
|
|
||||||
|
spawnTemplates("SB.BowSpawn", {
|
||||||
|
parentID: scoreboardID,
|
||||||
|
visible: false
|
||||||
|
});
|
||||||
|
spawnTemplates("SB.EnemySpawn", {
|
||||||
|
parentID: scoreboardID,
|
||||||
|
visible: false
|
||||||
|
});
|
||||||
|
|
||||||
var bowPositions = [];
|
var bowPositions = [];
|
||||||
var spawnPositions = [];
|
var spawnPositions = [];
|
||||||
for (var i = 0; i < TEMPLATES.length; ++i) {
|
for (var i = 0; i < TEMPLATES.length; ++i) {
|
||||||
var template = TEMPLATES[i];
|
var template = TEMPLATES[i];
|
||||||
|
|
||||||
if (template.name === "SB.BowSpawn") {
|
if (template.name === "SB.BowSpawn") {
|
||||||
bowPositions.push(Vec3.sum(rootPosition, template.localPosition));
|
bowPositions.push(Vec3.sum(rootPosition, template.localPosition));
|
||||||
Vec3.print("Pushing bow position", Vec3.sum(rootPosition, template.localPosition));
|
Vec3.print("Pushing bow position", Vec3.sum(rootPosition, template.localPosition));
|
||||||
|
|
Loading…
Reference in a new issue