Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Filename: framework.js
- Version: 0.2.5
- Last Update: 22/08/14
- Description:
- Framework written by Julian Reid aka Epihaumut / Jools64.
- Use:
- You are free to use this code for any project that you like
- be it commercial or not.
- I have not yet made any documentation so this code would be
- best used as an example rather than as a library in an actual
- game.
- Todo:
- Add more options to particles such as animating sprites
- Text
- size, family, etc properties rather than just font text.
- automatic height measurement with multiple line support.
- move onScreen from Sprite to Graphic
- Masks
- Pixel Mask
- Line Mask
- Circle Mask
- Collision check that takes multiple types.
- Each entity can have multiple types
- MaskList
- Sound
- Stop sound
- Pitch shift
- 3D
- Input
- Add mouse and touch to key bindings
- Improve game.state implementation. Maybe helper methods and method to save to local storage.
- */
- window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
- window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
- Function.prototype.subclass = function()
- {
- SubClass.prototype = this.prototype;
- return new SubClass();
- }; function SubClass(){};
- window.startupFunctions = [];
- window.onload = function()
- {
- for(var f in window.startupFunctions)
- {
- window.startupFunctions[f]();
- }
- }
- function Sound()
- {
- this.globalVolume = 0.4;
- this.music = null;
- try
- {
- window.AudioContext = window.AudioContext||window.webkitAudioContext;
- this.context = new AudioContext();
- }
- catch(e) {
- alert('Your browser does not support the Web Audio API. Please update to the latest version of Chrome, Firefox, Opera or Safari. IE is currently not supported.');
- }
- }
- Sound.prototype.play = function(url, loop, volume, pan)
- {
- if(loop === undefined)
- loop = false;
- if(volume === undefined)
- volume = 1;
- if(pan === undefined)
- pan = 0;
- pan = Utils.clamp(pan, -1, 1);
- volume *= this.globalVolume;
- var buffer = Assets.getSound(url);
- var gain = this.context.createGain();
- var panner = this.context.createPanner();
- var source = this.context.createBufferSource();
- panner.connect(gain);
- panner.coneOuterGain = 1;
- panner.coneOuterAngle = 0;
- panner.coneInnerAngle = 0;
- var angle = (pan*90)+90;
- panner.setPosition(Math.cos(Utils.toRad(angle)), 0, Math.sin(Utils.toRad(angle)))
- gain.connect(this.context.destination);
- gain.gain.value = volume;
- source.buffer = buffer;
- source.connect(panner);
- source.loop = loop;
- source.start(0);
- source.isPlaying = true;
- source.onended = function(){
- this.isPlaying = false;
- }
- return source;
- }
- Sound.prototype.stop = function(sound)
- {
- sound.stop();
- }
- Sound.prototype.playMusic = function(url, loop, volume)
- {
- if(this.music != null)
- this.music.stop();
- if(loop === undefined)
- loop = false;
- if(volume === undefined)
- volume = 2;
- this.music = this.play(url, loop, volume);
- }
- Sound.prototype.isPlaying = function(sound)
- {
- return(sound != null && sound.isPlaying);
- }
- window.Sound = new Sound();
- function Utils()
- {
- }
- Utils.prototype.safeDivide = function(dividend, numerator)
- {
- if(numerator == 0)
- return 0;
- return dividend / numerator;
- }
- Utils.prototype.parseXML = function(string)
- {
- if(window.DOMParser)
- {
- var parser = new DOMParser();
- var xml = parser.parseFromString(string, "text/xml");
- }
- else
- {
- var xml = new ActiveXObject("Microsoft.XMLDOM");
- xml.async = false;
- xml.loadXML(string);
- }
- return xml;
- }
- Utils.prototype.clamp = function(number, min, max)
- {
- if(number > max)
- number = max;
- if(number < min)
- number = min;
- return number;
- }
- Utils.prototype.loop = function(number, min, max)
- {
- var d = (max-min);
- while(number > max)
- number -= d;
- while(number < min)
- number += d;
- return number;
- }
- Utils.prototype.toRad = function(degrees)
- {
- return (2*Math.PI)-((2*Math.PI)/360)*degrees;
- }
- Utils.prototype.distance = function(x1, y1, x2, y2)
- {
- var dx = x2 - x1;
- var dy = y2 - y1;
- return Math.abs(Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)));
- }
- Utils.prototype.sign = function(number)
- {
- return number > 0 ? 1 : number < 0 ? -1 : number;
- }
- Utils.prototype.signApply = function(number, neg, zer, pos)
- {
- var sign = this.sign(number);
- if(sign < 0) return neg;
- if(sign == 0) return zer;
- if(sign > 0) return pos;
- }
- Utils.prototype.direction = function(sx, sy, dx, dy)
- {
- var deltaX = dx - sx;
- var deltaY = dy - sy;
- var dir = (Math.atan2(deltaY, deltaX) * 180 / Math.PI);
- return 360 - (dir < 0 ? dir + 360 : dir);
- }
- Utils.prototype.padNumber = function(n, zeros)
- {
- var string = typeof n === "number" ? n.toString() : n;
- while(string.length <= zeros-1)
- {
- string = "0" + string;
- }
- return string;
- }
- window.Utils = new Utils();
- function Input()
- {
- this.canvas = null;
- }
- Input.prototype.init = function(canvas)
- {
- this.canvas = canvas;
- this.keys = [];
- for(var i = 0; i < 128; i++)
- this.keys[i] = false;
- this.keysLast = this.keys.slice(0);
- this.bindings = [];
- this.mouse = [];
- for(var i = 0; i < 3; i++)
- this.mouse[i] = false;
- this.mouseLast = this.mouse.slice(0);
- this.mouseX = 0;
- this.mouseY = 0;
- this.touches = null;
- var that = this;
- canvas.addEventListener("contextmenu", function(e){
- e.preventDefault();
- return false;
- }, false);
- canvas.addEventListener("mousedown", function(e){
- that.mouse[e.button] = true;
- }, false);
- canvas.addEventListener("mouseup", function(e){
- that.mouse[e.button] = false;
- }, false);
- canvas.addEventListener("mousemove", function(e){
- var rect = canvas.getBoundingClientRect();
- that.mouseX = e.clientX - rect.left;
- that.mouseY = e.clientY - rect.top;
- that.mouseX = (that.mouseX / rect.width)*game.width;
- that.mouseY = (that.mouseY / rect.height)*game.height;
- }, false);
- window.addEventListener("keydown", function(e){
- that.keys[e.keyCode] = true;
- if(e.keyCode == 70)
- {
- game.fullscreen();
- }
- if(e.keyCode == that.keyToCode("UP") ||
- e.keyCode == that.keyToCode("DOWN") ||
- e.keyCode == that.keyToCode("LEFT") ||
- e.keyCode == that.keyToCode("RIGHT") ||
- e.keyCode == that.keyToCode("ESCAPE") ||
- e.keyCode == that.keyToCode("SPACE") ||
- e.keyCode == that.keyToCode("ENTER"))
- {
- e.preventDefault();
- return false;
- }
- }, false);
- window.addEventListener("keyup", function(e){
- that.keys[e.keyCode] = false;
- e.preventDefault();
- return false;
- }, false);
- canvas.addEventListener('touchstart', function(e){
- that.setTouch(e);
- }, false);
- canvas.addEventListener('touchcancel', function(e){
- that.setTouch(e);
- }, false);
- canvas.addEventListener('touchend', function(e){
- that.setTouch(e);
- }, false);
- canvas.addEventListener('touchmove', function(e){
- that.setTouch(e);
- }, false);
- }
- Input.prototype.addBinding = function(name, binding)
- {
- this.bindings[name.toLowerCase()] = binding;
- }
- Input.prototype.getBinding = function(name)
- {
- return this.bindings[name.toLowerCase()];
- }
- Input.prototype.checkBinding = function(name)
- {
- var binding = this.bindings[name.toLowerCase()];
- if(binding != undefined)
- {
- for(k in binding.keys)
- if(Input.checkKey(binding.keys[k]))
- return true;
- }
- return false;
- }
- Input.prototype.checkBindingPressed = function(name)
- {
- var binding = this.bindings[name.toLowerCase()];
- if(binding != undefined)
- {
- for(k in binding.keys)
- if(Input.checkKeyPressed(binding.keys[k]))
- return true;
- }
- return false;
- }
- Input.prototype.checkBindingReleased = function(name)
- {
- var binding = this.bindings[name.toLowerCase()];
- if(binding != undefined)
- {
- for(k in binding.keys)
- if(Input.checkKeyReleased(binding.keys[k]))
- return true;
- }
- return false;
- }
- Input.prototype.setTouch = function(e)
- {
- e.preventDefault();
- this.touches = e.touches;
- }
- Input.prototype.isTouched = function(x, y, w, h)
- {
- if(this.touches != null)
- {
- for(var t in this.touches)
- {
- var touch = this.touches[t];
- if(typeof touch.clientX !== "undefined")
- {
- var rect = game.canvas.getBoundingClientRect();
- var tx = touch.clientX - rect.left;
- var ty = touch.clientY - rect.top;
- tx = (tx / rect.width) * game.width;
- ty = (ty / rect.height) * game.height;
- if(tx > x &&
- ty > y &&
- tx < x + w &&
- ty < y + h)
- {
- return new Point(tx, ty);
- }
- }
- }
- }
- return null;
- }
- Input.prototype.mouseButtonToCode = function(button)
- {
- var code;
- switch(button)
- {
- case "LEFT":
- code = 0;
- break;
- case "MIDDLE":
- code = 1;
- break;
- case "RIGHT":
- code = 2;
- break;
- }
- return code;
- }
- Input.prototype.checkMouse = function(button)
- {
- return(this.mouse[this.mouseButtonToCode(button)]);
- }
- Input.prototype.checkKeyPressed = function(key)
- {
- return(this.mouse[this.mouseButtonToCode(key)] && !this.mouseLast[this.mouseButtonToCode(key)]);
- }
- Input.prototype.checkKeyReleased = function(key)
- {
- return(!this.mouse[this.mouseButtonToCode(key)] && this.mouseLast[this.mouseButtonToCode(key)]);
- }
- Input.prototype.checkKey = function(key)
- {
- return(this.keys[this.keyToCode(key)]);
- }
- Input.prototype.checkKeyPressed = function(key)
- {
- return(this.keys[this.keyToCode(key)] && !this.keysLast[this.keyToCode(key)]);
- }
- Input.prototype.checkKeyReleased = function(key)
- {
- return(!this.keys[this.keyToCode(key)] && this.keysLast[this.keyToCode(key)]);
- }
- Input.prototype.keyToCode = function(key)
- {
- switch(key.toUpperCase())
- {
- case "SPACE":
- return 32;
- break;
- case "ENTER":
- return 13;
- break;
- case "ESCAPE":
- return 27;
- break;
- case "CONTROL":
- return 17;
- break;
- case "SHIFT":
- return 16;
- break;
- case "ALT":
- return 18;
- break;
- case "TAB":
- return 9;
- break;
- case "LEFT":
- return 37;
- break;
- case "UP":
- return 38;
- break;
- case "DOWN":
- return 40;
- break;
- case "RIGHT":
- return 39;
- break;
- default:
- return key.charCodeAt(0);
- break;
- }
- }
- Input.prototype.update = function()
- {
- if(this.keys == null)
- this.keys = [];
- this.keysLast = this.keys.slice(0);
- this.mouseLast = this.mouse.slice(0);
- }
- window.Input = new Input();
- function KeyBinding(key)
- {
- this.keys = [];
- this.addKey(key);
- }
- KeyBinding.prototype.addKey = function(key)
- {
- if(typeof key === "object")
- {
- for(var k = 0; k < key.length; k++) //One of these isn't coming out as a string.
- if(typeof key[k] === "string")
- this.keys.push(key[k]);
- }
- else
- this.keys.push(key);
- }
- function Assets()
- {
- this.images = [];
- this.imagePath = "images/";
- this.sounds = [];
- this.soundPath = "sounds/";
- this.data = [];
- this.dataPath = "data/";
- this.callback = null;
- }
- Assets.prototype.setCallback = function(callback)
- {
- this.callback = callback;
- }
- Assets.prototype.loadImage = function(paths)
- {
- for(var path in paths)
- {
- var image = new Image();
- var that = this;
- image.onload = function(){
- that.images[this.path] = this;
- that.isDone();
- }
- image.src = this.imagePath + paths[path];
- image.path = paths[path];
- this.images[paths[path]] = null;
- }
- }
- Assets.prototype.loadSound = function(paths)
- {
- for(var path in paths)
- {
- var sound = new XMLHttpRequest();
- sound.open('GET', this.soundPath + paths[path] + "?r="+Math.random()*99999, true); // random parameter to stop caching
- sound.responseType = 'arraybuffer';
- var that = this;
- sound.onload = function(e)
- {
- var snd = this;
- Sound.context.decodeAudioData(this.response, function(buffer) {
- that.sounds[snd.path] = buffer;
- that.isDone();
- }, function(){console.log('An error occured decoding audio.');});
- }
- sound.path = paths[path];
- this.sounds[paths[path]] = null;
- sound.send();
- }
- }
- Assets.prototype.loadData = function(paths)
- {
- for(var path in paths)
- {
- var data = new XMLHttpRequest();
- data.open('GET', this.dataPath + paths[path] + "?r="+Math.random()*99999, true); // random parameter to stop caching
- var that = this;
- data.onload = function(e)
- {
- that.data[this.path] = this.response;
- that.isDone();
- }
- data.path = paths[path];
- this.data[paths[path]] = null;
- data.send();
- }
- }
- Assets.prototype.isDone = function()
- {
- for(var image in this.images)
- {
- if(this.images[image] == null)
- return false;
- }
- for(var sound in this.sounds)
- {
- if(this.sounds[sound] == null)
- return false;
- }
- for(var data in this.data)
- {
- if(this.data[data] == null)
- return false;
- }
- this.callback();
- }
- Assets.prototype.getImage = function(path)
- {
- var image = this.images[path];
- if(image === undefined)
- console.log("Image not yet loaded: " + path);
- else
- return image;
- return null;
- }
- Assets.prototype.getSound = function(path)
- {
- var sound = this.sounds[path];
- if(sound === undefined)
- console.log("Sound not yet loaded: " + path);
- else
- return sound;
- return null;
- }
- Assets.prototype.getData = function(path)
- {
- var data = this.data[path];
- if(data === undefined)
- console.log("Data file has not yet loaded: " + path);
- else
- return data;
- return null;
- }
- Assets.prototype.scaleImage = function(path, scale)
- {
- var image = this.images[path];
- if(image === undefined)
- console.log("Image not yet loaded: " + path);
- else
- {
- var scaled = document.createElement("canvas");
- scaled.width = image.width*scale;
- scaled.height = image.height*scale;
- scaledCtx = scaled.getContext("2d");
- scaledCtx.imageSmoothingEnabled = false;
- scaledCtx.mozImageSmoothingEnabled = false;
- scaledCtx.webkitImageSmoothingEnabled = false;
- scaledCtx.drawImage(image, 0, 0, image.width, image.height,
- 0, 0, scaled.width, scaled.height);
- this.images[path] = scaled;
- }
- return null;
- }
- Assets.prototype.scaleAll = function(scale)
- {
- for(var i in this.images)
- {
- var image = this.images[i];
- this.scaleImage(image.path, scale);
- }
- }
- window.Assets = new Assets();
- function Game(canvas, width, height, scale, smoothing, fixedTimeStep)
- {
- this.canvas = canvas;
- this.width = width === undefined ? 640 : width;
- this.height = height === undefined ? 360 : height;
- this.scale = scale === undefined ? 2 : scale;
- this.clearColor = new Color(50, 50, 50);
- this.smoothing = smoothing === undefined ? false : smoothing;
- this.fixedTimeStep = fixedTimeStep === undefined ? false : fixedTimeStep;
- this.stepLength = 1/60;
- this.stepTimer = 0;
- this.state = [];
- this.images = [];
- this.sounds = [];
- this.data = [];
- this.ctx = canvas.getContext("2d");
- if(!smoothing)
- {
- this.ctx.imageSmoothingEnabled = false;
- this.ctx.mozImageSmoothingEnabled = false;
- this.ctx.webkitImageSmoothingEnabled = false;
- }
- this.lastTime = Date.now();
- this.maxDelta = 0.2;
- this.world = null;
- }
- Game.prototype.start = function()
- {
- //setup global reference
- window.game = this;
- this.canvas.width = this.width*this.scale;
- this.canvas.height = this.height*this.scale;
- this.loadingScreen();
- Assets.setCallback(function(){
- game.init();
- });
- Assets.loadImage(this.images);
- Assets.loadSound(this.sounds);
- Assets.loadData(this.data);
- }
- Game.prototype.loadingScreen = function()
- {
- this.ctx = this.canvas.getContext("2d");
- this.ctx.fillStyle = "#000";
- this.ctx.fillRect(0, 0, game.width, game.height);
- this.ctx.font = "16pt Arial";
- this.ctx.fillStyle = "#FFF";
- this.ctx.textBaseline = "center";
- this.ctx.textAlign = "center";
- this.ctx.fillText("Loading, please wait...", game.width/2, game.height/2);
- var splash = new Image();
- var that = this;
- splash.onload = function(){
- that.ctx.drawImage(this, 0, 0, that.canvas.width, that.canvas.height);
- that.ctx.font = "16pt Arial";
- that.ctx.fillStyle = "#FFF";
- that.ctx.textBaseline = "center";
- that.ctx.textAlign = "center";
- that.ctx.fillText("Loading, please wait...", game.width/2, game.height/2);
- }
- splash.src = "images/splashScreen.png";
- }
- Game.prototype.init = function()
- {
- Input.init(this.canvas);
- requestAnimationFrame(this.loop.bind(this));
- if(this.scale != 1)
- Assets.scaleAll(this.scale);
- }
- Game.prototype.loop = function()
- {
- requestAnimationFrame(this.loop.bind(this));
- var currentTime = Date.now();
- var delta = (currentTime - this.lastTime)/1000;
- if(delta > this.maxDelta)
- delta = this.maxDelta;
- if(delta < 0)
- delta = 0;
- var that = this;
- if(this.fixedTimeStep)
- {
- this.stepTimer += delta;
- while(this.stepTimer >= this.stepLength)
- {
- this.stepTimer -= this.stepLength;
- this.update(this.stepLength);
- }
- }
- else
- this.update(delta);
- this.draw();
- this.lastTime = currentTime;
- }
- Game.prototype.update = function(delta)
- {
- if(this.world != null)
- this.world.update(delta);
- Input.update();
- }
- Game.prototype.draw = function()
- {
- if(this.clearColor != null)
- {
- this.ctx.fillStyle = this.clearColor.toHex();
- this.ctx.beginPath();
- this.ctx.rect(0, 0, this.width*this.scale, this.height*this.scale);
- this.ctx.fill();
- }
- if(this.world != null)
- this.world.draw();
- }
- Game.prototype.setWorld = function(world)
- {
- world.init();
- this.world = world;
- }
- Game.prototype.fullscreen = function()
- {
- console.log("fullscreen");
- if(this.canvas.webkitRequestFullScreen)
- this.canvas.webkitRequestFullScreen();
- else if(this.canvas.mozRequestFullScreen)
- this.canvas.mozRequestFullScreen();
- else if(this.canvas.requestFullScreen)
- this.canvas.requestFullScreen()
- }
- Game.prototype.showCursor = function(show)
- {
- if(show)
- this.canvas.style.cursor = "";
- else
- this.canvas.style.cursor = "none";
- }
- function World()
- {
- this.entities = [];
- this.removeList = [];
- this.addList = [];
- this.camera = null;
- this.nextUpdateSort = true;
- }
- World.prototype.init = function()
- {
- this.camera = new Camera();
- }
- World.prototype.update = function(delta)
- {
- this.camera.update(delta);
- for(var entity in this.entities)
- this.entities[entity].update(delta);
- if(this.addList.length > 0)
- {
- for(var entity in this.addList)
- this.entities.push(this.addList[entity]);
- this.addList.length = 0;
- this.depthSort();
- }
- if(this.removeList.length > 0)
- {
- for(var entity in this.removeList)
- this.entities.splice(this.entities.indexOf(this.removeList[entity]), 1);
- this.removeList.length = 0;
- }
- if(this.nextUpdateSort)
- {
- this.entities.sort(this.depthSortCompare);
- this.nextUpdateSort = false;
- }
- }
- World.prototype.draw = function()
- {
- for(var entity in this.entities)
- this.entities[entity].draw();
- }
- World.prototype.add = function(entity)
- {
- entity.world = this;
- entity.init();
- this.addList.push(entity);
- }
- World.prototype.remove = function(entity)
- {
- this.removeList.push(entity);
- }
- World.prototype.depthSortCompare = function(a, b)
- {
- if(a.depth > b.depth)
- return -1;
- if(a.depth < b.depth)
- return 1;
- return 0;
- }
- World.prototype.depthSort = function()
- {
- this.nextUpdateSort = true;
- }
- World.prototype.collide = function(entity, type, offsetX, offsetY)
- {
- var entity1 = entity;
- offsetX = offsetX === undefined ? 0 : offsetX;
- offsetY = offsetY === undefined ? 0 : offsetY;
- entity1.x += offsetX;
- entity1.y += offsetY;
- for(var e in this.entities)
- {
- var entity2 = this.entities[e];
- if(entity2.type == type)
- {
- if(entity1.mask != null && entity2.mask != null && entity1.mask.collide(entity2.mask))
- {
- entity1.x -= offsetX;
- entity1.y -= offsetY;
- return entity2;
- }
- }
- }
- entity1.x -= offsetX;
- entity1.y -= offsetY;
- return null;
- }
- World.prototype.typeCount = function(type)
- {
- var count = 0;
- for(var e in this.entities)
- if(this.entities[e].type == type)
- ++count;
- return count;
- }
- World.prototype.getFirstOfType = function(type)
- {
- for(var e in this.entities)
- {
- if(this.entities[e].type == type)
- return this.entities[e];
- }
- return null;
- }
- World.prototype.getType = function(type)
- {
- var typeList = [];
- for(var e in this.entities)
- {
- if(this.entities[e].type == type)
- typeList.push(this.entities[e]);
- }
- return typeList;
- }
- function Camera()
- {
- this.x = 0;
- this.y = 0;
- this.offsetX = game.width/2;
- this.offsetY = game.height/2;
- this.targetX = this.x + this.offsetX;
- this.targetY = this.y + this.offsetY;
- this.target = null;
- this.easeSpeed = 4;
- this.bounds = false;
- this.width = 0;
- this.height = 0;
- }
- Camera.prototype.setBounds = function(width, height)
- {
- if(width === undefined || height === undefined)
- this.bounds = false;
- else
- {
- this.width = width;
- this.height = height;
- this.bounds = true;
- }
- }
- Camera.prototype.setPosition = function(x, y)
- {
- this.x = x;
- this.y = y;
- }
- Camera.prototype.setTarget = function()
- {
- if(arguments.length == 1)
- {
- //Target entity
- this.target = arguments[0];
- }
- else if(arguments.length == 2)
- {
- //Target position
- this.targetX = arguments[0];
- this.targetY = arguments[1];
- this.target = null;
- }
- }
- Camera.prototype.update = function(delta)
- {
- if(this.target)
- {
- this.targetX = this.target.x;
- this.targetY = this.target.y;
- }
- this.x += ((this.targetX - this.offsetX) - this.x) * delta * this.easeSpeed;
- this.y += ((this.targetY - this.offsetY) - this.y) * delta * this.easeSpeed;
- if(this.bounds)
- {
- this.x = Utils.clamp(this.x, 0, this.width - game.width);
- this.y = Utils.clamp(this.y, 0, this.height - game.height);
- }
- }
- function Entity()
- {
- this.graphic = null;
- this.depth = 0;
- this.mask = null;
- this.type = "";
- this.x = 0;
- this.y = 0;
- this.deltaCounter = 0;
- this.stepInterval = 1/60;
- this.fixedTimeStep = false;
- this.maxSteps = 8;
- }
- Entity.prototype.init = function()
- {
- }
- Entity.prototype.update = function(delta)
- {
- if(this.graphic != null)
- this.graphic.update(delta);
- if(this.fixedTimeStep)
- {
- this.deltaCounter += delta;
- var steps = 0;
- while(this.deltaCounter >= this.stepInterval)
- {
- this.step();
- this.deltaCounter -= this.stepInterval;
- if(++steps >= this.maxSteps)
- break;
- }
- }
- }
- Entity.prototype.step = function()
- {
- }
- Entity.prototype.draw = function()
- {
- if(this.graphic != null && this.graphic.visible)
- this.graphic.draw();
- }
- Entity.prototype.setGraphic = function(graphic)
- {
- this.graphic = graphic;
- this.graphic.setOwner(this);
- this.graphic.init();
- }
- Entity.prototype.setMask = function(mask)
- {
- this.mask = mask;
- this.mask.owner = this;
- }
- Entity.prototype.setDepth = function(depth)
- {
- if(this.depth != depth)
- {
- this.depth = depth;
- this.world.depthSort();
- }
- }
- Entity.prototype.collide = function(type, xOffset, yOffset)
- {
- return this.world.collide(this, type, xOffset, yOffset);
- }
- function Graphic()
- {
- this.owner = null;
- this.xOffset = 0;
- this.yOffset = 0;
- this.xOrigin = 0;
- this.xOrigin = 0;
- this.angle = 0;
- this.xScale = 1;
- this.yScale = 1;
- this.alpha = 1;
- this.width = 0;
- this.height = 0;
- this.image = null;
- this.visible = true;
- this.effectsApplied = false;
- }
- Graphic.prototype.setOwner = function(owner)
- {
- this.owner = owner;
- }
- Graphic.prototype.init = function()
- {
- }
- Graphic.prototype.update = function(delta)
- {
- }
- Graphic.prototype.applyEffects = function()
- {
- if(this.alpha != 1 || this.angle != 0 || this.xScale != 1 || this.yScale != 1)
- {
- this.effectsApplied = true;
- var transformX = (this.getX() + this.xOrigin)*game.scale;
- var transformY = (this.getY() + this.yOrigin)*game.scale;
- game.ctx.save();
- game.ctx.translate(transformX, transformY);
- game.ctx.rotate(Utils.toRad(this.angle));
- game.ctx.scale(this.xScale, this.yScale);
- game.ctx.globalAlpha = Utils.clamp(this.alpha, 0, 1);
- game.ctx.translate(-transformX, -transformY);
- }
- }
- Graphic.prototype.resetEffects = function()
- {
- if(this.effectsApplied)
- {
- this.effectsApplied = false;
- game.ctx.restore();
- }
- }
- Graphic.prototype.draw = function()
- {
- }
- Graphic.prototype.getX = function()
- {
- return ~~(this.owner.x - this.xOffset - ~~this.owner.world.camera.x);
- }
- Graphic.prototype.getY = function()
- {
- return ~~(this.owner.y - this.yOffset - ~~this.owner.world.camera.y);
- }
- Graphic.prototype.centerOffset = function()
- {
- this.xOffset = this.width/2;
- this.yOffset = this.height/2;
- }
- Graphic.prototype.centerOrigin = function()
- {
- this.xOrigin = this.width/2;
- this.yOrigin = this.height/2;
- }
- Graphic.prototype.setOffset = function(x, y)
- {
- this.xOffset = x;
- this.yOffset = y;
- }
- Graphic.prototype.setOrigin = function(x, y)
- {
- this.xOrigin = x;
- this.yOrigin = y;
- }
- Graphic.prototype.drawTile = function(x, y, width, height, id)
- {
- var sx = (id*width)%(this.image.width/game.scale);
- var sy = (((id*width) - sx)/(this.image.width/game.scale))*height;
- try
- {
- game.ctx.drawImage(this.image, sx*game.scale, sy*game.scale,
- width*game.scale, height*game.scale,
- ~~x*game.scale, ~~y*game.scale,
- width*game.scale, height*game.scale);
- }
- catch(IndexSizeError){
- console.log("Error: Tile id goes outside sheet: " + id);
- }
- }
- function GraphicList(graphics)
- {
- Graphic.call(this);
- this.graphics = graphics;
- }
- GraphicList.prototype = Graphic.subclass();
- GraphicList.prototype.setOwner = function(owner)
- {
- for(var g in this.graphics)
- this.graphics[g].setOwner(owner);
- }
- GraphicList.prototype.init = function()
- {
- for(var g in this.graphics)
- this.graphics[g].init();
- }
- GraphicList.prototype.draw = function()
- {
- for(var g in this.graphics)
- if(this.graphics[g].visible)
- this.graphics[g].draw();
- }
- GraphicList.prototype.update = function(delta)
- {
- for(var g in this.graphics)
- this.graphics[g].update(delta);
- }
- function BitmapText(string, font, buffered)
- {
- Graphic.call(this);
- this.string = string;
- this.lastString = "";
- this.font = font;
- this.buffered = (typeof buffered === "undefined") ? true : this.buffered;
- this.redrawBuffer = true;
- if(this.buffered)
- {
- this.buffer = document.createElement('canvas');
- this.buffer.width = this.stringWidth(string)*game.scale;
- this.buffer.height = this.stringHeight(string)*game.scale;
- this.bctx = this.buffer.getContext('2d');
- }
- }
- BitmapText.prototype = Graphic.subclass();
- BitmapText.prototype.draw = function()
- {
- Graphic.prototype.draw.call(this);
- this.applyEffects();
- if(this.buffered)
- {
- if(this.redrawBuffer)
- {
- if(this.lastString != this.string.substring(0, this.lastString.length))
- this.bctx.clearRect(0, 0, this.buffer.width, this.buffer.height);
- this.drawText(this.bctx, 0, 0);
- this.redrawBuffer = false;
- this.lastString = this.string;
- }
- game.ctx.drawImage(this.buffer, this.getX()*game.scale, this.getY()*game.scale);
- }
- else
- this.drawText(game.ctx, this.getX(), this.getY());
- this.resetEffects();
- }
- BitmapText.prototype.setSize = function(width, height)
- {
- this.buffer.width = width;
- this.buffer.height = height;
- this.bctx = this.buffer.getContext('2d');
- }
- BitmapText.prototype.setString = function(string)
- {
- this.string = string;
- this.redrawBuffer = true;
- }
- BitmapText.prototype.drawText = function(context, x, y)
- {
- var lines = this.string.split("\n");
- var xOffset = 0;
- var yOffset = 0;
- for(var line in lines)
- {
- for(i = 0; i < lines[line].length; i++)
- {
- this.drawLetter(context, lines[line].charAt(i), x + xOffset, y + yOffset);
- xOffset += this.font.charWidth[lines[line].charAt(i)] + this.font.hSpacing;
- }
- yOffset += this.font.charHeight + this.font.vSpacing;
- xOffset = 0;
- }
- }
- BitmapText.prototype.drawLetter = function(context, letter, x, y)
- {
- // This is a horrible mess due to copying and pasting from an older framework. Much sadness to be found.
- var img = Assets.getImage(this.font.image);
- var s = this.font.charMap[letter]*(this.font.defaultCharWidth*game.scale);
- var sx = (s % img.width);
- var sy = ((s - sx)/(img.width)*(this.font.charHeight*game.scale));
- var w = this.font.charWidth[letter]*game.scale;
- context.drawImage(img, sx, sy,
- w, this.font.charHeight*game.scale, x*game.scale, y*game.scale,
- w, this.font.charHeight*game.scale);
- }
- BitmapText.prototype.stringWidth = function(string)
- {
- var width = 0;
- var lines = string.split("\n");
- for(var line in lines)
- {
- for(i = 0; i < lines[line].length; i++)
- {
- var lw = this.font.charWidth[lines[line].charAt(i)];
- width += (typeof lw === "undefined" ? this.font.defaultCharWidth : lw) + this.font.hSpacing;
- }
- }
- return width;
- }
- BitmapText.prototype.stringHeight = function(string)
- {
- var height = 0;
- lines = string.split("\n");
- height = lines.length * (this.font.charHeight + this.font.vSpacing);
- return height;
- }
- //BITMAP FONT CLASS
- function BitmapFont(image, layout, charWidth, charHeight, hSpacing, vSpacing)
- {
- this.image = image;
- this.layout = layout;
- this.defaultCharWidth = charWidth;
- this.charHeight = charHeight;
- this.hSpacing = (typeof this.hSpacing === "undefined") ? 0 : this.hSpacing;
- this.vSpacing = (typeof this.vSpacing === "undefined") ? 0 : this.vSpacing;
- this.hSpacing = hSpacing;
- this.vSpacing = vSpacing;
- this.charMap = [];
- this.charWidth = [];
- for(var i = 0; i < layout.length; i++)
- {
- this.charMap[layout.charAt(i)] = i;
- this.charWidth[layout.charAt(i)] = charWidth;
- }
- this.charWidth[" "] = charWidth;
- }
- BitmapFont.prototype.setCharWidth = function(character, width)
- {
- if(character instanceof Array)
- {
- for(var c in character)
- this.charWidth[character[c]] = width;
- }
- else
- this.charWidth[character] = width;
- }
- function Particle(x, y)
- {
- Graphic.call(this);
- this.x = x;
- this.y = y;
- this.speed = 3;
- this.direction = 0;
- this.life = 1;
- this.timer = 0;
- this.alpha = 1;
- }
- Particle.prototype = Graphic.subclass();
- function ParticleEmitter(imagePath)
- {
- Graphic.call(this);
- this.image = Assets.getImage(imagePath);
- this.width = this.image.width/game.scale;
- this.height = this.image.height/game.scale;
- this.centerOffset();
- this.emissionSpeed = 60;
- this.emissionTimer = 0;
- this.blendMode = "source-over";//"lighter"
- this.direction = 0;
- this.directionVar = 360;
- this.directionDelta = -2;
- this.speed = 0;
- this.speedVar = 0.5;
- this.speedDelta = 0.2;
- this.life = 0.1;
- this.lifeVar = 0.4;
- this.fadeOut = true;
- this.particles = [];
- }
- ParticleEmitter.prototype = Graphic.subclass();
- ParticleEmitter.prototype.addParticle = function()
- {
- var p = new Particle(this.owner.x, this.owner.y);
- this.particles.push(p);
- return p;
- }
- ParticleEmitter.prototype.update = function(delta)
- {
- this.emissionTimer += delta;
- while(this.emissionTimer > 1/this.emissionSpeed)
- {
- this.emissionTimer -= 1/this.emissionSpeed;
- p = this.addParticle();
- p.direction = this.direction + Math.random()*this.directionVar;
- p.speed = this.speed + Math.random()*this.speedVar;
- p.life = this.life + Math.random()*this.lifeVar;
- }
- for(var p in this.particles)
- {
- var pt = this.particles[p];
- pt.timer += delta;
- pt.direction += this.directionDelta * 60 * delta;
- pt.speed += this.speedDelta * 60 * delta;
- if(this.fadeOut)
- {
- pt.alpha = Utils.clamp(4 - ((pt.timer/pt.life)*4), 0, 1);
- }
- pt.x += Math.cos(Utils.toRad(pt.direction))*pt.speed * 60 * delta;
- pt.y += Math.sin(Utils.toRad(pt.direction))*pt.speed * 60 * delta;
- if(pt.timer > pt.life)
- this.particles.splice(p, 1);
- }
- }
- ParticleEmitter.prototype.draw = function()
- {
- for(var p in this.particles)
- {
- var pt = this.particles[p];
- game.ctx.globalCompositeOperation = this.blendMode;
- game.ctx.globalAlpha = pt.alpha;
- game.ctx.globalAlpha = pt.alpha;
- game.ctx.globalAlpha = pt.alpha;
- this.drawTile(pt.x - this.owner.world.camera.x - this.xOffset, pt.y - this.owner.world.camera.y - this.yOffset, 16, 16, 0);
- game.ctx.globalAlpha = 1;
- game.ctx.globalCompositeOperation = "source-over";
- }
- }
- function Sprite(imagePath, width, height)
- {
- Graphic.call(this);
- this.image = Assets.getImage(imagePath);
- this.width = width === undefined ? this.image.width/game.scale : width;
- this.height = height === undefined ? this.image.height/game.scale : height;
- this.centerOrigin();
- this.frame = 0;
- this.frames = [0];
- this.frameRate = 0;
- this.animations = [];
- this.animationDone = false;
- this.loop = true;
- this.onScreen = true;
- }
- Sprite.prototype = Graphic.subclass();
- Sprite.prototype.draw = function()
- {
- var size = Math.max(this.width, this.height);
- var x = this.getX();
- var y = this.getY();
- if(x > -size &&
- y > -size &&
- x < game.width + size &&
- y < game.height + size)
- {
- this.onScreen = true;
- this.applyEffects();
- var frame;
- if(this.frames.length > 0)
- frame = this.frames[~~(this.frame)];
- else
- frame = 0;
- this.drawTile(this.getX(), this.getY(),
- this.width, this.height, frame);
- this.resetEffects();
- }
- else
- this.onScreen = false;
- }
- Sprite.prototype.update = function(delta)
- {
- this.frame += this.frameRate*delta;
- if(this.frame < 0)
- this.frame = this.frames.length-1;
- if(this.frame >= this.frames.length)
- {
- if(this.loop)
- this.frame = 0;
- else this.frame = this.frames.length-1;
- this.animationDone = true;
- }
- }
- Sprite.prototype.add = function(name, frames, frameRate)
- {
- this.animations[name] = new Animation(frames, frameRate);
- }
- Sprite.prototype.play = function(name, frameRate, reset, loop)
- {
- this.frames = this.animations[name].frames;
- if(frameRate && frameRate >= 0)
- this.frameRate = frameRate;
- else
- this.frameRate = this.animations[name].frameRate;
- if(reset !== undefined && reset)
- this.frame = 0;
- if(loop !== undefined)
- this.loop = loop;
- else this.loop = true;
- this.animationDone = false;
- if(this.frame < 0)
- this.frame = this.frames.length-1;
- if(this.frame >= this.frames.length)
- {
- if(this.loop)
- this.frame = 0;
- else this.frame = this.frames.length-1;
- }
- }
- function Text(string, font)
- {
- Graphic.call(this);
- this.string = string;
- this.stringLast = "";
- this.canvas = document.createElement("canvas");
- this.ctx = null;
- this.font = font === undefined ? "16pt Arial" : font;
- }
- Text.prototype = Graphic.subclass();
- Text.prototype.draw = function()
- {
- if(this.string != this.stringLast || this.canvas == null)
- {
- this.stringLast = this.string;
- game.ctx.font = this.font;
- this.canvas.width = Math.ceil(game.ctx.measureText(this.string).width);
- this.canvas.height = 64;
- this.width = this.canvas.width;
- this.height = this.canvas.height;
- this.ctx = this.canvas.getContext("2d");
- this.ctx.font = this.font;
- this.ctx.fillStyle = "#FFF";
- this.ctx.textBaseline = 'top';
- this.ctx.fillText(this.string, 0, 0);
- }
- this.applyEffects();
- if(this.canvas != null)
- game.ctx.drawImage(this.canvas, this.getX()*game.scale, this.getY()*game.scale);
- this.resetEffects();
- }
- function TileMap(imagePath, tileSize, width, height)
- {
- Graphic.call(this);
- this.image = Assets.getImage(imagePath);
- this.map = [];
- for(var i = 0; i < width; i++)
- {
- this.map[i] = [];
- for(var t = 0; t < height; t++)
- this.map[i][t] = -1;
- }
- this.tileSize = tileSize;
- this.width = width;
- this.height = height;
- }
- TileMap.prototype = Graphic.subclass();
- TileMap.prototype.draw = function()
- {
- this.applyEffects();
- var fx, fy, tx, ty;
- fx = ~~(this.owner.world.camera.x/this.tileSize);
- fy = ~~(this.owner.world.camera.y/this.tileSize);
- tx = Math.ceil((this.owner.world.camera.x + game.width)/this.tileSize);
- ty = Math.ceil((this.owner.world.camera.y + game.height)/this.tileSize);
- fx = Utils.clamp(fx, 0, this.width);
- tx = Utils.clamp(tx, 0, this.width);
- fy = Utils.clamp(fy, 0, this.height);
- ty = Utils.clamp(ty, 0, this.height);
- for(var i = fx; i < tx; i++)
- for(var t = fy; t < ty; t++)
- if(this.map[i][t] != -1)
- {
- this.drawTile(this.getX() + (i*this.tileSize),
- this.getY() + (t*this.tileSize),
- this.tileSize,
- this.tileSize,
- this.map[i][t]);
- }
- this.resetEffects();
- }
- TileMap.prototype.fill = function(id)
- {
- for(var i = 0; i < this.width; i++)
- for(var t = 0; t < this.height; t++)
- this.map[i][t] = id;
- }
- TileMap.prototype.set = function(x, y, id)
- {
- this.map[x][y] = id;
- }
- function Background(imagePath, xRepeat, yRepeat)
- {
- Graphic.call(this);
- this.image = Assets.getImage(imagePath);
- this.xRepeat = xRepeat === undefined ? true : xRepeat;
- this.yRepeat = yRepeat === undefined ? true : yRepeat;
- }
- Background.prototype = Graphic.subclass();
- Background.prototype.draw = function()
- {
- var scrollX, scrollY;
- var iw = this.image.width/game.scale;
- var ih = this.image.height/game.scale;
- if(this.xRepeat)
- {
- scrollX = (this.getX()%iw);
- if(scrollX > 0)
- scrollX -= iw;
- }
- else
- scrollX = this.getX();
- if(this.yRepeat)
- {
- var scrollY = (this.getY()%ih);
- if(scrollY > 0)
- scrollY -= ih;
- }
- else
- scrollY = this.getY();
- var toX = this.xRepeat ? (game.width + iw) : 1;
- var toY = this.yRepeat ? (game.height + ih) : 1;
- for(var x = 0; x < toX; x += iw)
- for(var y = 0; y < toY; y += ih)
- {
- this.drawTile((this.width + x + scrollX), (this.height + y + scrollY),
- iw, ih, 0);
- }
- }
- function Animation(frames, frameRate)
- {
- this.frames = frames;
- this.frameRate = frameRate === undefined ? 0 : frameRate;
- }
- function Mask()
- {
- this.type = "";
- this.owner = null;
- this.x = 0;
- this.y = 0;
- this.BOX = 0;
- this.GRID = 1;
- }
- Mask.prototype.collide = function(other)
- {
- }
- Mask.prototype.getX = function()
- {
- return this.x + this.owner.x;
- }
- Mask.prototype.getY = function()
- {
- return this.y + this.owner.y;
- }
- function Grid(gridSize, width, height)
- {
- Mask.call(this);
- this.type = this.GRID;
- this.gridSize = gridSize;
- this.width = width;
- this.height = height;
- this.map = [];
- this.clear();
- }
- Grid.prototype = Mask.subclass();
- Grid.prototype.collide = function(other)
- {
- switch(other.type)
- {
- case this.BOX:
- var fx = ~~(other.getX()/this.gridSize);
- var fy = ~~(other.getY()/this.gridSize);
- var tx = Math.ceil((other.getX() + other.width)/this.gridSize);
- var ty = Math.ceil((other.getY() + other.width)/this.gridSize);
- fx = Utils.clamp(fx, 0, this.width);
- tx = Utils.clamp(tx, 0, this.width);
- fy = Utils.clamp(fy, 0, this.height);
- ty = Utils.clamp(ty, 0, this.height);
- for(var x = 0; x < this.width; x++)
- for(var y = 0; y < this.height; y++)
- {
- var bx = (x*this.gridSize) + this.getX();
- var by = (y*this.gridSize) + this.getY();
- if(this.map[x][y])
- {
- if(!(other.getX() > bx + this.gridSize ||
- other.getX() + other.width < bx ||
- other.getY() > by + this.gridSize ||
- other.getY() + other.height < by))
- return true;
- }
- }
- break;
- default:
- console.log("Error: Collision not implemented: " + this.type + " with " + other.type);
- return false;
- break;
- }
- return false;
- }
- Grid.prototype.clear = function()
- {
- for(var x = 0; x < this.width; x++)
- {
- this.map[x] = [];
- for(var y = 0; y < this.height; y++)
- {
- this.map[x][y] = false;
- }
- }
- }
- Grid.prototype.set = function(x, y, value)
- {
- this.map[x][y] = value;
- }
- function Box(width, height, x, y)
- {
- Mask.call(this);
- this.type = this.BOX;
- this.x = x === undefined ? 0 : x;
- this.y = y === undefined ? 0 : y;
- this.width = width;
- this.height = height;
- }
- Box.prototype = Mask.subclass();
- Box.prototype.collide = function(other)
- {
- switch(other.type)
- {
- case this.BOX:
- return(!(other.getX() > this.getX() + this.width ||
- other.getX() + other.width < this.getX() ||
- other.getY() > this.getY() + this.height ||
- other.getY() + other.height < this.getY()));
- break;
- case this.GRID:
- return(other.collide(this));
- break;
- default:
- console.log("Error: Collision not implemented: " + this.type + " with " + other.type);
- return false;
- break;
- }
- }
- function OgmoLoader()
- {
- }
- OgmoLoader.prototype.getMapAttribute = function(xml, attributeName)
- {
- return xml.getElementsByTagName("level")[0].getAttribute(attributeName);
- }
- OgmoLoader.prototype.loadTileMap = function(xml, elementName, image, tileSize)
- {
- var gridSize = xml.getElementsByTagName("level")[0].getAttribute("gridSize");
- var width = xml.getElementsByTagName("level")[0].getAttribute("width");
- var height = xml.getElementsByTagName("level")[0].getAttribute("height");
- var tileMap = new TileMap(image, tileSize, width/tileSize, height/tileSize);
- var tiles = xml.getElementsByTagName(elementName)[0].getElementsByTagName("tile");
- for(var i = 0; i < tiles.length; i++)
- {
- var id = tiles[i].getAttribute("id");
- var x = tiles[i].getAttribute("x");
- var y = tiles[i].getAttribute("y");
- tileMap.set(x, y, id);
- }
- return tileMap;
- }
- OgmoLoader.prototype.loadGrid = function(xml, elementName, tileSize)
- {
- var gridSize = xml.getElementsByTagName("level")[0].getAttribute("gridSize");
- var width = xml.getElementsByTagName("level")[0].getAttribute("width");
- var height = xml.getElementsByTagName("level")[0].getAttribute("height");
- var grid = new Grid(tileSize, width/tileSize, height/tileSize);
- var string = xml.getElementsByTagName(elementName)[0].innerHTML;
- var mapWidth = ~~(width/tileSize)+1;
- for(var i = 0; i < string.length; i++)
- {
- if(string.charAt(i) == '1')
- {
- grid.set(~~(i % mapWidth), ~~(i/mapWidth), true);
- }
- }
- return grid;
- }
- OgmoLoader.prototype.loadEntities = function(xml, entitiesElement, world)
- {
- var entities = xml.getElementsByTagName(entitiesElement)[0].childNodes;
- for(var i = 0; i < entities.length; i++)
- {
- if(entities[i].nodeType == 1)
- {
- var x = entities[i].getAttribute("x");
- var y = entities[i].getAttribute("y");
- var jsString = entities[i].nodeName + "(" + x + ", " + y;
- for(var t = 0; t < 10; t++)
- {
- var arg = entities[i].getAttribute("arg"+t);
- if(arg)
- jsString += ", \"" + arg + "\"";
- }
- jsString += ")";
- var entity = null;
- try
- {
- eval("entity = new " + jsString + ";");
- if(entity != null)
- world.add(entity);
- }
- catch(e)
- {
- console.log("Error loading entity from map.");
- console.log(e);
- }
- }
- }
- }
- window.OgmoLoader = new OgmoLoader();
- function Point(x, y)
- {
- this.x = x;
- this.y = y;
- }
- function Color(r, g, b)
- {
- this.r = r;
- this.g = g;
- this.b = b;
- }
- Color.prototype.toHex = function()
- {
- return "#" + Utils.padNumber(this.r.toString(16), 2) + Utils.padNumber(this.g.toString(16), 2) + Utils.padNumber(this.b.toString(16), 2);
- }
- // ENTITY: SPLASH
- function ESplash(firstWorld)
- {
- Entity.call(this);
- this.x = 0;
- this.y = 0;
- this.timer = 0;
- this.fadeTime = 0.5;
- this.sustainTime = 2;
- this.firstWorld = firstWorld;
- }
- ESplash.prototype = Entity.subclass();
- ESplash.prototype.init = function()
- {
- Entity.prototype.init.call(this);
- this.setGraphic(new Sprite("splashScreen.png"));
- this.graphic.alpha = 0;
- this.graphic.setOrigin(0, 0);
- }
- ESplash.prototype.update = function(delta)
- {
- Entity.prototype.update.call(this, delta);
- this.timer += delta;
- if(this.timer < this.fadeTime)
- this.graphic.alpha = (this.timer / this.fadeTime);
- else if(this.timer < this.fadeTime + this.sustainTime)
- this.graphic.alpha = 1;
- else if(this.timer < (this.fadeTime*2) + this.sustainTime)
- this.graphic.alpha = 1 - ((this.timer - (this.fadeTime + this.sustainTime)) / this.fadeTime);
- else
- this.done();
- //console.log(game);
- this.graphic.xScale = game.width / this.graphic.width;
- this.graphic.yScale = game.height / this.graphic.height;
- if(Input.checkKeyPressed("ENTER") ||
- Input.checkKeyPressed("ESCAPE") ||
- Input.checkKeyPressed("SPACE"))
- this.done();
- }
- ESplash.prototype.done = function()
- {
- this.world.remove(this);
- game.setWorld(this.firstWorld);
- }
- // WORLD: SPLASH
- function WSplash(firstWorld)
- {
- World.call(this);
- this.firstWorld = firstWorld;
- }
- WSplash.prototype = World.subclass();
- WSplash.prototype.init = function()
- {
- World.prototype.init.call(this);
- this.add(new ESplash(this.firstWorld));
- }
- function Vector3(x, y, z)
- {
- this.set(x, y, z);
- }
- Vector3.prototype.zero = function()
- {
- this.set(0, 0, 0);
- }
- Vector3.prototype.set = function(x, y, z)
- {
- if(typeof x == "object")
- {
- this.x = x.x;
- this.y = x.y;
- this.z = x.z;
- }
- else
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
- Vector3.prototype.clone = function()
- {
- clone = new Vector3(this.x, this.y, this.z);
- return clone;
- }
- Vector3.prototype.distance = function(second)
- {
- if(typeof second === "undefined")
- return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
- var delta = second.clone();
- delta.subtract(this);
- return Math.sqrt((delta.x * delta.x) + (delta.y * delta.y) + (delta.z * delta.z));
- }
- Vector3.prototype.normalize = function()
- {
- var dist = this.distance();
- this.divide(dist);
- }
- Vector3.prototype.add = function(value)
- {
- if(typeof value == "object")
- {
- this.x += value.x;
- this.y += value.y;
- this.z += value.z;
- }
- else
- {
- this.x += value;
- this.y += value;
- this.z += value;
- }
- }
- Vector3.prototype.subtract = function(value)
- {
- if(typeof value === "object")
- {
- this.x -= value.x;
- this.y -= value.y;
- this.z -= value.z;
- }
- else
- {
- this.x -= value;
- this.y -= value;
- this.z -= value;
- }
- }
- Vector3.prototype.multiply = function(value)
- {
- if(typeof value === "object")
- {
- this.x *= value.x;
- this.y *= value.y;
- this.z *= value.z;
- }
- else
- {
- this.x *= value;
- this.y *= value;
- this.z *= value;
- }
- }
- Vector3.prototype.divide = function(value)
- {
- if(typeof value === "object")
- {
- this.x = Utils.safeDivide(this.x, value.x);
- this.y = Utils.safeDivide(this.y, value.y);
- this.z = Utils.safeDivide(this.z, value.z);
- }
- else
- {
- this.x = Utils.safeDivide(this.x, value);
- this.y = Utils.safeDivide(this.y, value);
- this.z = Utils.safeDivide(this.z, value);
- }
- }
- Vector3.prototype.lerp = function(target, time)
- {
- var delta = target.clone();
- delta.subtract(this);
- delta.multiply(time);
- this.add(delta);
- }
Advertisement
Add Comment
Please, Sign In to add comment