Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Filename: framework.js
- Version: 0.1
- 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:
- Particles
- Emitter (Can be attached to entity)
- Bitmap Font Graphic
- Pixel Mask
- Line Mask
- MaskList
- OgmoLoader
- Sound
- Stop sound
- Pitch shift
- 3D
- Input
- Key bindings
- */
- 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.3;
- 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);
- 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 = 1;
- this.music = this.play(url, loop, volume);
- }
- window.Sound = new Sound();
- function Utils()
- {
- }
- 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 = n.toString();
- 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.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("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.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 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], true);
- 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], true);
- 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;
- }
- window.Assets = new Assets();
- function Game(canvas, width, height, smoothing)
- {
- this.canvas = canvas;
- this.width = width === undefined ? 640 : width;
- this.height = height === undefined ? 360 : height;
- this.clearColor = new Color(50, 50, 50);
- this.smoothing = smoothing === undefined ? false : smoothing;
- 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.canvas.height = this.height;
- Assets.setCallback(function(){
- game.init();
- });
- Assets.loadImage(this.images);
- Assets.loadSound(this.sounds);
- Assets.loadData(this.data);
- }
- Game.prototype.init = function()
- {
- Input.init(this.canvas);
- requestAnimationFrame(this.loop.bind(this));
- }
- 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;
- 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()
- {
- this.ctx.fillStyle = this.clearColor.toHex();
- this.ctx.beginPath();
- this.ctx.rect(0, 0, this.width, this.height);
- 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;
- }
- 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;
- }
- 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;
- }
- 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)
- {
- return this.world.collide(this, type);
- }
- 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.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.angle != 0 || this.xScale != 1 || this.yScale != 1)
- {
- this.effectsApplied = true;
- var transformX = this.getX() + this.xOrigin;
- var transformY = this.getY() + this.yOrigin;
- game.ctx.save();
- game.ctx.translate(transformX, transformY);
- game.ctx.rotate(Utils.toRad(this.angle));
- game.ctx.scale(this.xScale, this.yScale);
- 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 Math.floor(this.owner.x - this.xOffset - this.owner.world.camera.x);
- }
- Graphic.prototype.getY = function()
- {
- return Math.floor(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.drawTile = function(x, y, width, height, id)
- {
- var sx = (id*width)%this.image.width;
- var sy = (((id*width) - sx)/this.image.width)*height;
- try
- {
- game.ctx.drawImage(this.image, sx, sy,
- width, height,
- x, y,
- width, height);
- }
- 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 Sprite(imagePath, width, height)
- {
- Graphic.call(this);
- this.image = Assets.getImage(imagePath);
- this.width = width === undefined ? this.image.width : width;
- this.height = height === undefined ? this.image.height : 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 x = this.getX();
- var y = this.getY();
- if(x > -this.width &&
- y > -this.height &&
- x < game.width + this.width &&
- y < game.height + this.height)
- {
- this.onScreen = true;
- this.applyEffects();
- var frame;
- if(this.frames.length > 0)
- frame = this.frames[Math.floor(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 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 = Math.floor(this.owner.world.camera.x/this.tileSize);
- fy = Math.floor(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;
- if(this.xRepeat)
- {
- scrollX = (this.getX()%this.image.width);
- if(scrollX > 0)
- scrollX -= this.image.width;
- }
- else
- scrollX = this.getX();
- if(this.yRepeat)
- {
- var scrollY = (this.getY()%this.image.height);
- if(scrollY > 0)
- scrollY -= this.image.height;
- }
- else
- scrollY = this.getY();
- var toX = this.xRepeat ? (game.width + this.image.width) : 1;
- var toY = this.yRepeat ? (game.height + this.image.height) : 1;
- for(var x = 0; x < toX; x += this.image.width)
- for(var y = 0; y < toY; y += this.image.height)
- {
- game.ctx.drawImage(this.image,
- this.width + x + scrollX,
- this.height + y + scrollY);
- }
- }
- 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 = Math.floor(other.getX()/this.gridSize);
- var fy = Math.floor(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 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 "#" + this.r.toString(16) + this.g.toString(16) + this.b.toString(16);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement