Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/src/js/jgame.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 31 by tsugehara, Tue Feb 26 14:41:21 2013 UTC revision 32 by tsugehara, Tue Feb 26 17:25:31 2013 UTC
# Line 811  var Trigger = (function () { Line 811  var Trigger = (function () {
811      };      };
812      return Trigger;      return Trigger;
813  })();  })();
814    var SimpleSound = (function () {
815        function SimpleSound() { }
816        SimpleSound.getAudioContext = function getAudioContext() {
817            if(SimpleSound.context) {
818                return SimpleSound.context;
819            }
820            SimpleSound.context = SimpleSound._getAudioContext();
821            return SimpleSound.context;
822        };
823        SimpleSound._getAudioContext = function _getAudioContext() {
824            if(window["AudioContext"]) {
825                return new window["AudioContext"]();
826            } else if(window["webkitAudioContext"]) {
827                return new window["webkitAudioContext"]();
828            } else {
829                return null;
830            }
831        };
832        SimpleSound.play = function play(sound, loop, when) {
833            var context = SimpleSound.getAudioContext();
834            var bufferSource = context.createBufferSource();
835            if(loop) {
836                bufferSource.loop = true;
837            }
838            bufferSource.buffer = sound;
839            bufferSource.connect(context.destination);
840            bufferSource.start(when === undefined ? 0 : when);
841            return bufferSource;
842        };
843        SimpleSound.hasBgm = function hasBgm() {
844            return SimpleSound.bgmSource !== undefined;
845        };
846        SimpleSound.playBgm = function playBgm(sound, loop, when) {
847            if(SimpleSound.bgmSource) {
848                SimpleSound.stopBgm();
849            }
850            SimpleSound.bgmSource = SimpleSound.play(sound, loop, when);
851            return SimpleSound.bgmSource;
852        };
853        SimpleSound.stop = function stop(source, when) {
854            if(!source) {
855                return;
856            }
857            source.stop(when === undefined ? 0 : when);
858        };
859        SimpleSound.stopBgm = function stopBgm(when) {
860            SimpleSound.stop(SimpleSound.bgmSource, when);
861            delete SimpleSound.bgmSource;
862        };
863        SimpleSound.tone = function tone(hertz, seconds) {
864            hertz = hertz !== undefined ? hertz : 200;
865            seconds = seconds !== undefined ? seconds : 1;
866            var nChannels = 1;
867            var sampleRate = 44100;
868            var amplitude = 2;
869            var context = SimpleSound.getAudioContext();
870            var buffer = context.createBuffer(nChannels, seconds * sampleRate, sampleRate);
871            var fArray = buffer.getChannelData(0);
872            for(var i = 0; i < fArray.length; i++) {
873                var time = i / buffer.sampleRate;
874                var angle = hertz * time * Math.PI;
875                fArray[i] = Math.sin(angle) * amplitude;
876            }
877            return buffer;
878        };
879        return SimpleSound;
880    })();
881  var ResourceLoader = (function () {  var ResourceLoader = (function () {
882      function ResourceLoader(resource) {      function ResourceLoader(resource) {
883          this.resource = resource;          this.resource = resource;
# Line 854  var ScriptResourceLoader = (function (_s Line 921  var ScriptResourceLoader = (function (_s
921    
922      }      }
923      ScriptResourceLoader.prototype.load = function (url, identifier) {      ScriptResourceLoader.prototype.load = function (url, identifier) {
924            var _this = this;
925          var script = document.createElement("script");          var script = document.createElement("script");
926          var heads = document.getElementsByTagName("head");          var heads = document.getElementsByTagName("head");
927          if(heads.length == 0) {          if(heads.length == 0) {
928              throw "can not find head tag";              throw "can not find head tag";
929          }          }
930          script.src = url + "?" + (new Date()).getTime();          script.src = url + "?" + (new Date()).getTime();
         var caller = this;  
931          var callback = this.completed;          var callback = this.completed;
932          script.onload = function () {          script.onload = function () {
933              callback.call(caller, identifier, script, true);              callback.call(_this, identifier, script, true);
934          };          };
935          script.onerror = function () {          script.onerror = function () {
936              callback.call(caller, identifier, script, false);              callback.call(_this, identifier, script, false);
937          };          };
938          heads[0].appendChild(script);          heads[0].appendChild(script);
939      };      };
# Line 880  var ScriptResourceLoader = (function (_s Line 947  var ScriptResourceLoader = (function (_s
947      };      };
948      return ScriptResourceLoader;      return ScriptResourceLoader;
949  })(ResourceLoader);  })(ResourceLoader);
950    var SoundResourceLoader = (function (_super) {
951        __extends(SoundResourceLoader, _super);
952        function SoundResourceLoader() {
953            _super.apply(this, arguments);
954    
955        }
956        SoundResourceLoader.prototype.load = function (url, identifier) {
957            var _this = this;
958            var request = new XMLHttpRequest();
959            request.open("GET", "sound/" + url, true);
960            request.responseType = "arraybuffer";
961            var callback = this.completed;
962            request.onload = function () {
963                var context = SimpleSound.getAudioContext();
964                if(context) {
965                    context.decodeAudioData(request.response, function (decodedAudio) {
966                        callback.call(_this, identifier, decodedAudio, true);
967                    }, function () {
968                        callback.call(_this, identifier, null, false);
969                    });
970                } else {
971                    callback.call(_this, identifier, null, false);
972                }
973            };
974            request.onerror = function () {
975                callback.call(_this, identifier, null, false);
976            };
977            request.send();
978        };
979        SoundResourceLoader.prototype.completed = function (name, audio, is_success) {
980            if(!is_success) {
981                console.log("error: " + name);
982            } else {
983                this.resource.sounds[name] = audio;
984            }
985            this.resource.requestCompleted(name);
986        };
987        return SoundResourceLoader;
988    })(ResourceLoader);
989  var Resource = (function () {  var Resource = (function () {
990      function Resource() {      function Resource() {
991          this.requests = [];          this.requests = [];
# Line 888  var Resource = (function () { Line 994  var Resource = (function () {
994          };          };
995          this.scripts = {          this.scripts = {
996          };          };
997            this.sounds = {
998            };
999          this.loaders = {          this.loaders = {
1000          };          };
1001          this.loaders["js"] = new ScriptResourceLoader(this);          this.loaders["js"] = new ScriptResourceLoader(this);
1002          this.loaders["default"] = new ImageResourceLoader(this);          this.loaders["default"] = new ImageResourceLoader(this);
1003            this.loaders["mp3"] = new SoundResourceLoader(this);
1004            this.loaders["ogg"] = this.loaders["mp3"];
1005            this.loaders["wav"] = this.loaders["mp3"];
1006            this.loaders["mid"] = this.loaders["mp3"];
1007      }      }
1008      Resource.getInstance = function getInstance() {      Resource.getInstance = function getInstance() {
1009          return (function () {          return (function () {
# Line 904  var Resource = (function () { Line 1016  var Resource = (function () {
1016      Resource.prototype.get = function (name) {      Resource.prototype.get = function (name) {
1017          return this.images[name];          return this.images[name];
1018      };      };
1019        Resource.prototype.sound = function (name) {
1020            return this.sounds[name];
1021        };
1022      Resource.prototype.requestCompleted = function (name) {      Resource.prototype.requestCompleted = function (name) {
1023          for(var i = 0; i < this.requests.length; i++) {          for(var i = 0; i < this.requests.length; i++) {
1024              if(this.requests[i] == name) {              if(this.requests[i] == name) {
# Line 2158  var Game = (function () { Line 2273  var Game = (function () {
2273      Game.prototype.r = function (name) {      Game.prototype.r = function (name) {
2274          return this.resource.get(name);          return this.resource.get(name);
2275      };      };
2276        Game.prototype.s = function (name) {
2277            return this.resource.sound(name);
2278        };
2279      Game.prototype.preloadArray = function (ary, loadingScene) {      Game.prototype.preloadArray = function (ary, loadingScene) {
2280          var param = {          var param = {
2281          };          };

Legend:
Removed from v.31  
changed lines
  Added in v.32

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26