New OversimplifiedJS version to fix adding objects to room and a new way to add audio.

This commit is contained in:
Robbie Antenesse 2016-04-20 16:19:22 -06:00
parent 1b025e4fb8
commit d62da3341f
2 changed files with 111 additions and 155 deletions

View File

@ -560,25 +560,27 @@ Oversimplified.Room.prototype.Draw = function () {
} }
// Add a GameObject or PremadeObject to the room. // Add a GameObject or PremadeObject to the room.
Oversimplified.Room.prototype.AddObject = function (newObjectName, newObjectOptions) { Oversimplified.Room.prototype.AddObject = function (objectOrNewName, objectOptions) {
newObjectOptions = (typeof newObjectOptions !== 'undefined') ? newObjectOptions : {}; objectOptions = (typeof objectOptions !== 'undefined') ? objectOptions : {};
var self = this; var self = this;
if (newObjectName.type == "GameObject") { //Create from prefabricated object if (objectOrNewName.type == "GameObject") { //Create from prefabricated object
var newID = Oversimplified.nextID++; // Overwrite manual id or name, if entered.
var newName = newObjectName.name + newID.toString(); objectOptions.id = Oversimplified.nextID++;
self.objects[newName] = Oversimplified.CopyObject(newObjectName, newID, newName, newObjectOptions); objectOptions.name = objectOrNewName.name + objectOptions.id.toString();
// console.log(objectOptions.name);
self.objects[objectOptions.name] = Oversimplified.CopyObject(objectOrNewName, objectOptions);
return self.objects[newName]; return self.objects[objectOptions.name];
} }
else { else {
if (self.objects[newObjectName]) { if (self.objects[objectOrNewName]) {
if (Oversimplified.DEBUG.showMessages) console.log("Object with name \"" + newObjectName + "\" already exists in current room!"); if (Oversimplified.DEBUG.showMessages) console.log("Object with name \"" + objectOrNewName + "\" already exists in current room!");
return false; return false;
} }
self.objects[newObjectName] = new Oversimplified.GameObject(newObjectName, newObjectOptions); self.objects[objectOrNewName] = new Oversimplified.GameObject(objectOrNewName, objectOptions);
return self.objects[newObjectName]; return self.objects[objectOrNewName];
} }
} }
@ -1070,7 +1072,7 @@ Oversimplified.GUIs.Add = function (guiName, guiOptions) {
Oversimplified.GUIs[guiName] = new Oversimplified.Animation(guiName, guiOptions); Oversimplified.GUIs[guiName] = new Oversimplified.Animation(guiName, guiOptions);
return Oversimplified.GUIs[guiName]; return Oversimplified.GUIs[guiName];
} else { } else {
if (Oversimplified.DEBUG.showMessages) console.log("An GUI with the name \"" + guiName + "\" already exists!"); if (Oversimplified.DEBUG.showMessages) console.log("A GUI with the name \"" + guiName + "\" already exists!");
return false; return false;
} }
} }
@ -1116,10 +1118,39 @@ Oversimplified.GUI.prototype.AddElement = function (options) {
/* Effects namespace /* Effects namespace
*/ */
Oversimplified.Effects = { Oversimplified.Effects = {
Sounds: [], Sounds: {},
Tunes: [], Tunes: {}
} }
// Aliases for Sounds and Tunes
Oversimplified.Effects.S = Oversimplified.Effects.Sounds;
Oversimplified.Effects.T = Oversimplified.Effects.Music = Oversimplified.Effects.M = Oversimplified.Effects.Tunes;
// Alias for Effects
Oversimplified.E = Oversimplified.Effects;
Oversimplified.Effects.AddSound = function (soundName, soundSources) {
if (typeof Oversimplified.Effects.Sounds[soundName] === 'undefined') {
Oversimplified.Effects.Sounds[soundName] = new Oversimplified.Sound(soundName, soundSources);
return Oversimplified.Effects.Sounds[soundName];
} else {
if (Oversimplified.DEBUG.showMessages) console.log("A Sound with the name \"" + soundName + "\" already exists!");
return false;
}
}
Oversimplified.Effects.NewSound = Oversimplified.Effects.AddSound;
Oversimplified.Effects.AddTune = function (tuneName, tuneSources) {
if (typeof Oversimplified.Effects.Tunes[tuneName] === 'undefined') {
Oversimplified.Effects.Tunes[tuneName] = new Oversimplified.Sound(tuneName, tuneSources);
return Oversimplified.Effects.Tunes[tuneName];
} else {
if (Oversimplified.DEBUG.showMessages) console.log("A Tune with the name \"" + tuneName + "\" already exists!");
return false;
}
}
Oversimplified.Effects.AddMusic = Oversimplified.Effects.NewTune = Oversimplified.Effects.NewMusic = Oversimplified.Effects.AddTune;
Oversimplified.Effects.Tunes.CheckLoops = function () { Oversimplified.Effects.Tunes.CheckLoops = function () {
for (var tune in Oversimplified.Effects.Tunes) { for (var tune in Oversimplified.Effects.Tunes) {
if (Oversimplified.Effects.Tunes[tune].type == "Tune" && Oversimplified.Effects.Tunes[tune].IsPlaying()) { if (Oversimplified.Effects.Tunes[tune].type == "Tune" && Oversimplified.Effects.Tunes[tune].IsPlaying()) {
@ -1133,30 +1164,33 @@ Oversimplified.Effects.Tunes.CheckLoops = function () {
Plays a sound effect once. Plays a sound effect once.
Preferably source should be a .wav file and secondarySource should be a .mp3 file. Preferably source should be a .wav file and secondarySource should be a .mp3 file.
*/ */
Oversimplified.Sound = function (name, source, secondarySource) { Oversimplified.Sound = function (name, sourcesObject) {
this.id = Oversimplified.nextID++; this.id = Oversimplified.nextID++;
secondarySource = typeof secondarySource !== 'undefined' ? secondarySource : false; sourcesObject = typeof sourcesObject !== 'undefined' ? sourcesObject : {};
this.name = name; this.name = name;
this.source = source; this.source = {
this.secondarySource = secondarySource; mp3: (typeof sourcesObject.mp3 !== 'undefined' && sourcesObject.mp3.length > 0) ? sourcesObject.mp3 : false,
wav: (typeof sourcesObject.wav !== 'undefined' && sourcesObject.wav.length > 0) ? sourcesObject.wav : false,
ogg: (typeof sourcesObject.ogg !== 'undefined' && sourcesObject.ogg.length > 0) ? sourcesObject.ogg : false
};
this.audioElement = document.createElement("audio"); this.audioElement = document.createElement("audio");
this.audioElement.id = this.name + this.id.toString(); this.audioElement.id = this.name + this.id.toString();
// Alias for this.audioElement
this.element = this.audioElement;
for (var type in this.source) {
if (type !== false) {
var audioSource = document.createElement("source"); var audioSource = document.createElement("source");
audioSource.src = this.source; audioSource.src = this.source[type];
this.audioElement.appendChild(audioSource);
if (this.secondarySource != false) {
audioSource.src = this.secondarySource;
this.audioElement.appendChild(audioSource); this.audioElement.appendChild(audioSource);
} }
}
document.getElementById("audio").appendChild(this.audioElement); document.getElementById("audio").appendChild(this.audioElement);
this.audioElement.load(); this.audioElement.load();
this.element = this.audioElement;
} }
Oversimplified.Sound.prototype.type = "Sound"; Oversimplified.Sound.prototype.type = "Sound";
@ -1178,32 +1212,34 @@ Oversimplified.Sound.prototype.IsPlaying = function () {
Preferably source should be a .mp3 file and secondarySource should be a .ogg file. Preferably source should be a .mp3 file and secondarySource should be a .ogg file.
If duration is specified, loop when duration is reached. If duration is specified, loop when duration is reached.
*/ */
Oversimplified.Tune = function (name, source, secondarySource, duration) { Oversimplified.Tune = function (name, tuneOptions) {
this.id = Oversimplified.nextID++; this.id = Oversimplified.nextID++;
secondarySource = typeof secondarySource !== 'undefined' ? secondarySource : false; tuneOptions = (typeof tuneOptions !== 'undefined') ? tuneOptions : {};
duration = typeof duration !== 'undefined' ? duration : false;
this.name = name; this.name = name;
this.source = source; this.source = {
this.secondarySource = secondarySource; mp3: (typeof tuneOptions.mp3 !== 'undefined' && tuneOptions.mp3.length > 0) ? tuneOptions.mp3 : false,
this.duration = duration; wav: (typeof tuneOptions.wav !== 'undefined' && tuneOptions.wav.length > 0) ? tuneOptions.wav : false,
ogg: (typeof tuneOptions.ogg !== 'undefined' && tuneOptions.ogg.length > 0) ? tuneOptions.ogg : false
};
this.duration = (typeof tuneOptions.duration !== 'undefined') ? tuneOptions.duration : false;
this.audioElement = document.createElement("audio"); this.audioElement = document.createElement("audio");
this.audioElement.id = this.name + this.id.toString(); this.audioElement.id = this.name + this.id.toString();
// Alias for this.audioElement
this.element = this.audioElement;
for (var type in this.source) {
if (type !== false) {
var audioSource = document.createElement("source"); var audioSource = document.createElement("source");
audioSource.src = this.source; audioSource.src = this.source[type];
this.audioElement.appendChild(audioSource);
if (this.secondarySource != false) {
audioSource.src = this.secondarySource;
this.audioElement.appendChild(audioSource); this.audioElement.appendChild(audioSource);
} }
}
document.getElementById("audio").appendChild(this.audioElement); document.getElementById("audio").appendChild(this.audioElement);
this.audioElement.load(); this.audioElement.load();
this.element = this.audioElement;
} }
Oversimplified.Tune.prototype.type = "Tune"; Oversimplified.Tune.prototype.type = "Tune";
@ -1228,56 +1264,11 @@ Oversimplified.Tune.prototype.IsPlaying = function () {
return !this.element.paused && !this.element.ended && 0 < this.element.currentTime; return !this.element.paused && !this.element.ended && 0 < this.element.currentTime;
} }
// Aliases for Sounds and Tunes
Oversimplified.Effects.S = Oversimplified.Effects.Sounds;
Oversimplified.Effects.T = Oversimplified.Effects.Tunes;
// Alias for "Tunes" in case it's too hard to remember.
Oversimplified.Effects.Music = Oversimplified.Effects.Tunes;
Oversimplified.Effects.M = Oversimplified.Effects.Tunes;
// Alias for Effects
Oversimplified.E = Oversimplified.Effects;
// Create a new GameObject inside the current Room and return it.
Oversimplified.CreateObject = function (newObjectName, x, y, imageSrc, maskImageSrc, animationsArray) {
if (newObjectName.type == "GameObject") { //Create from prefabricated object
var newID = Oversimplified.nextID++;
var newName = newObjectName.name + newID.toString();
Oversimplified.O[newName] = Oversimplified.CopyObject(newObjectName, newID, newName);
Oversimplified.O[newName].x = x;
Oversimplified.O[newName].y = y;
return Oversimplified.O[newName];
}
else {
if (Oversimplified.O[newObjectName]) {
if (Oversimplified.DEBUG.showMessages) console.log("Object with name \"" + newObjectName + "\" already exists in current room!");
return false;
}
Oversimplified.O[newObjectName] = new Oversimplified.GameObject(newObjectName, x, y, imageSrc, maskImageSrc, animationsArray);
}
Oversimplified.O[newObjectName].Start();
return Oversimplified.O[newObjectName];
}
/* Copy a GameObject /* Copy a GameObject
newID and newName are optional. If excluded, they are auto-populated with the next id value and the original object's name.
Use "identical" to copy name and id of original object.
*/ */
Oversimplified.CopyObject = function (object, newID, newName, objectOptions) { Oversimplified.CopyObject = function (object, objectOptions) {
var resultingCopy = {}; var resultingCopy = {};
if (newID != "identical") {
resultingCopy.id = typeof newID !== 'undefined' ? newID : Oversimplified.nextID++;
resultingCopy.name = typeof newName !== 'undefined' ? newName : object.name + resultingCopy.id.toString();
} else { //If second argument is "identical" with quotes, then copy id and name, too.
resultingCopy.id = object.id;
resultingCopy.name = object.name;
}
//Copy Oversimplified.GameObject-unique properties //Copy Oversimplified.GameObject-unique properties
if (object.type == 'GameObject') { if (object.type == 'GameObject') {
resultingCopy.self = resultingCopy; resultingCopy.self = resultingCopy;
@ -1311,52 +1302,17 @@ Oversimplified.CopyObject = function (object, newID, newName, objectOptions) {
} }
} }
for (var option in objectOptions) { for (var option in objectOptions) {
//Overwrite any properties. //Overwrite any extra properties specified in objectOptions.
if (object[option].slice) { // If it's an array, copy its values. if (objectOptions[option].slice) { // If it's an array, copy its values.
resultingCopy[option] = object[option].slice(); resultingCopy[option] = objectOptions[option].slice();
} else { } else {
resultingCopy[option] = objectOptions[option]; resultingCopy[option] = objectOptions[option];
} }
} }
return resultingCopy; // If id and name were not specified in the objectOptions and are therefore not set, set them!
} if (typeof resultingCopy.id === 'undefined') resultingCopy.id = Oversimplified.nextID++;
if (typeof resultingCopy.name === 'undefined') resultingCopy.name = object.name + resultingCopy.id.toString();
/* Copy any class (needs expanding)
newID and newName are optional. If excluded, they are auto-populated with the next id value and the original object's name.
Use "identical" to copy the id and name of the original object.
*/
Oversimplified.Copy = function (object, newID, newName) {
var resultingCopy = {};
if (newID != "identical") {
resultingCopy.id = typeof newID !== 'undefined' ? newID : Oversimplified.nextID++;
resultingCopy.name = typeof newName !== 'undefined' ? newName : object.name + resultingCopy.id.toString();
} else { //If second argument is "identical" with quotes, then copy id and name, too.
resultingCopy.id = object.id;
resultingCopy.name = object.name;
}
//Copy Oversimplified.GameObject-unique properties
if (object.type == 'GameObject') {
resultingCopy = Oversimplified.CopyObject(object, newID, newName);
}
if (object.type == 'Room') {
/* resultingCopy.background = new Image();
resultingCopy.background.loaded = false;
resultingCopy.background.src = object.background.src;
resultingCopy.background.onload = function () {
resultingCopy.loaded = true;
} */
resultingCopy.objects = {};
for (var subObject in object.objects) {
resultingCopy.objects[subObject] = Oversimplified.Copy(object.objects[subObject]);
}
}
for (var property in object) {
if (typeof resultingCopy[property] === 'undefined') {
resultingCopy[property] = object[property];
}
}
return resultingCopy; return resultingCopy;
} }

View File

@ -1,17 +1,17 @@
var mus_title = new OS.Tune("Title", "audio/music/title.mp3", "audio/music/title.wav"); var mus_title = new OS.E.AddTune("Title", {mp3: "audio/music/title.mp3", wav: "audio/music/title.wav"});
var mus_sail = new OS.Tune("Sailing", "audio/music/ocean.mp3", "audio/music/ocean.wav"); var mus_sail = new OS.E.AddTune("Sailing", {mp3: "audio/music/ocean.mp3", wav: "audio/music/ocean.wav"});
var mus_trade = new OS.Tune("Trade", "audio/music/trade.mp3", "audio/music/trade.wav"); var mus_trade = new OS.E.AddTune("Trade", {mp3: "audio/music/trade.mp3", wav: "audio/music/trade.wav"});
var mus_credits = new OS.Tune("Credits", "audio/music/credits.mp3", "audio/music/credits.wav"); var mus_credits = new OS.E.AddTune("Credits", {mp3: "audio/music/credits.mp3", wav: "audio/music/credits.wav"});
var snd_buy = new OS.Sound("Buy", "audio/sounds/Buy.wav", "audio/sounds/Buy.mp3"); var snd_buy = new OS.E.AddSound("Buy", {wav: "audio/sounds/Buy.wav", mp3: "audio/sounds/Buy.mp3"});
var snd_cannotbuy = new OS.Sound("Cannot Buy", "audio/sounds/Cannot_Buy.wav", "audio/sounds/Cannot_Buy.mp3"); var snd_cannotbuy = new OS.E.AddSound("Cannot Buy", {wav: "audio/sounds/Cannot_Buy.wav", mp3: "audio/sounds/Cannot_Buy.mp3"});
var snd_heal = new OS.Sound("Heal", "audio/sounds/Heal.wav", "audio/sounds/Heal.mp3"); var snd_heal = new OS.E.AddSound("Heal", {wav: "audio/sounds/Heal.wav", mp3: "audio/sounds/Heal.mp3"});
var snd_illness = new OS.Sound("Illness", "audio/sounds/Illness.wav", "audio/sounds/Illness.mp3"); var snd_illness = new OS.E.AddSound("Illness", {wav: "audio/sounds/Illness.wav", mp3: "audio/sounds/Illness.mp3"});
var snd_cursordown = new OS.Sound("Move Cursor Down", "audio/sounds/Move_Cursor.wav", "audio/sounds/Move_Cursor.mp3"); var snd_cursordown = new OS.E.AddSound("Move Cursor Down", {wav: "audio/sounds/Move_Cursor.wav", mp3: "audio/sounds/Move_Cursor.mp3"});
var snd_cursorup = new OS.Sound("Move Cursor Up", "audio/sounds/Move_Cursor_Up.wav", "audio/sounds/Move_Cursor_Up.mp3"); var snd_cursorup = new OS.E.AddSound("Move Cursor Up", {wav: "audio/sounds/Move_Cursor_Up.wav", mp3: "audio/sounds/Move_Cursor_Up.mp3"});
var snd_sailing = new OS.Sound("Sailing", "audio/sounds/Sailing.wav", "audio/sounds/Sailing.mp3"); var snd_sailing = new OS.E.AddSound("Sailing", {wav: "audio/sounds/Sailing.wav", mp3: "audio/sounds/Sailing.mp3"});
var snd_select = new OS.Sound("Select", "audio/sounds/Select.wav", "audio/sounds/Select.mp3"); var snd_select = new OS.E.AddSound("Select", {wav: "audio/sounds/Select.wav", mp3: "audio/sounds/Select.mp3"});
var snd_sell = new OS.Sound("Sell", "audio/sounds/Sell.wav", "audio/sounds/Sell.mp3"); var snd_sell = new OS.E.AddSound("Sell", {wav: "audio/sounds/Sell.wav", mp3: "audio/sounds/Sell.mp3"});
var snd_wave = new OS.Sound("Wave Crash", "audio/sounds/Wave_Crash.wav", "audio/sounds/Wave_Crash.mp3"); var snd_wave = new OS.E.AddSound("Wave Crash", {wav: "audio/sounds/Wave_Crash.wav", mp3: "audio/sounds/Wave_Crash.mp3"});
function loadAudio() {} function loadAudio() {}