Guest User

Untitled

a guest
Apr 12th, 2013
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Common.js
  2. $(window).load(function() {
  3.     game.init();
  4. });
  5.  
  6. window.requestAnimFrame = (function(){  
  7.    return window.requestAnimationFrame  ||    
  8.      window.webkitRequestAnimationFrame ||    
  9.      window.mozRequestAnimationFrame ||    
  10.      window.oRequestAnimationFrame  ||    
  11.      window.msRequestAnimationFrame  ||    
  12.      function(/* function */ callback, /* DOMElement */ element){  
  13.       window.setTimeout(callback, 1000 / 60);  
  14.      };  
  15.    })();  
  16.        
  17. var game = {
  18.     gameRunning: false,
  19.     init: function() {
  20.         inputHandler.init();
  21.        
  22.         this.currentMap = resourceLoader.loadMap("SampleMap");
  23.         this.entities = resourceLoader.loadEntities();
  24.        
  25.         game.canvas = $('#gameCanvas')[0];
  26.         game.context = game.canvas.getContext('2d');
  27.        
  28.        
  29.         //Here starts the game
  30.         this.gameRunning = true;
  31.         $('#gameCanvas').show();
  32.         window.requestAnimFrame(game.drawLoop);
  33.     },
  34.     drawLoop: function() { 
  35.         if(this.gameRunning)
  36.             requestAnimFrame(game.drawLoop);
  37.            
  38.         //1.Clear the canvas
  39.         game.context.clearRect(0, 0, game.canvas.width, game.canvas.height);
  40.        
  41.         //2.Iterate through all items
  42.         for(var i=0;i<game.entities.length; i++) {
  43.             game.entities[i].update();
  44.         }
  45.        
  46.         /*3.Draw them
  47.         * 3.1 DrawBaseGround(Layer#1), 3.2 DrawItems(Layer#2), 3.3 DrawCharacters with actions...
  48.         */
  49.         game.context.drawImage(game.currentMap.imageLayerOne, 0,0);
  50.         game.context.drawImage(game.currentMap.imageLayerTwo, 0,0);
  51.    
  52.         for(var j=0;j<game.entities.length; j++) {
  53.             game.entities[j].draw();
  54.         }
  55.     }
  56. }
  57.  
  58. var entities = [
  59.     {
  60.         name: "Player",
  61.         type: "human", //or "cpu"
  62.         posx: 85,
  63.         posy: 85,
  64.         speed: 10,
  65.         imagePath: 'images/chars/defaultChar.png',
  66.         charImage: new Image(),
  67.         draw: function() {
  68.             game.context.drawImage(this.charImage, this.posx, this.posy);
  69.         },
  70.         update: function() {
  71.             if(inputHandler.keyForward) {
  72.                 this.posy -= speed;
  73.             }
  74.             if(inputHandler.keyBackward) {
  75.                 this.posy += speed;
  76.             }
  77.             if(inputHandler.keyLeft) {
  78.                 this.posx -= speed;
  79.             }
  80.             if(inputHandler.keyRight) {
  81.                 this.posx += speed;
  82.             }
  83.            
  84.         }
  85.     }
  86. ]
  87.  
  88. var maps =
  89.     [
  90.         {
  91.             name: "SampleMap",
  92.             layerOneImagePath: "images/maps/sampleMap/layerOne.png",
  93.             layerTwoImagePath: "",
  94.             imageLayerOne: new Image(),
  95.             imageLayerTwo: new Image()
  96.         }
  97.     ]
  98.  
  99.  
  100. var resourceLoader = {
  101.     totalItemsLoaded: 0,
  102.     loadMap: function(mapName) {       
  103.         for(var i=0;i<maps.length; i++) {
  104.             if(maps[i].name == mapName) {
  105.                 if(maps[i].layerOneImagePath != '') {
  106.                     maps[i].imageLayerOne = this.loadImage(maps[i].layerOneImagePath);
  107.                 }
  108.                 if(maps[i].layerTwoImagePath != '') {
  109.                     maps[i].imageLayerTwo = this.loadImage(maps[i].layerTwoImagePath);
  110.                 }
  111.                
  112.                 return maps[i];
  113.             }
  114.         }
  115.     },
  116.     loadEntities: function() {
  117.         for(var i=0;i<entities.length; i++){
  118.             entities[i].charImage = this.loadImage(entities[i].imagePath);
  119.         }
  120.        
  121.         return entities;
  122.     },
  123.     loadImage: function(imagePath) {
  124.         this.totalItemsLoaded++;
  125.         var image = new Image();
  126.         image.src = imagePath;
  127.         return image;
  128.     }
  129. }
  130.  
  131. var inputHandler = {
  132.     keyForward: false,
  133.     keyBackward: false,
  134.     keyLeft: false,
  135.     keyRight: false,
  136.     init: function() {
  137.         $(window).click(inputHandler.mouseClickHandler);
  138.         $(window).keydown(inputHandler.keyDownHandler);
  139.         $(window).keyup(inputHandler.keyUpHandler);
  140.     }, 
  141.     keyDownHandler: function(ev) {
  142.         if(ev.keyCode == '87') { //fw
  143.             this.keyBackward = false;
  144.             this.keyForward = true;
  145.         }
  146.         if(ev.keyCode == '83') { //bw
  147.             this.keyForward = false;
  148.             this.keyBackward = true;
  149.         }
  150.         if(ev.keyCode == '65') { //left
  151.             this.keyRight = false;
  152.             this.keyLeft = true;
  153.         }
  154.         if(ev.keyCode == '68') { // right
  155.             this.keyRight = true;
  156.             this.keyLeft = false;
  157.         }
  158.     },
  159.     keyUpHandler: function(ev) {
  160.         if(ev.keyCode == '87') { //fw
  161.             this.keyForward = false;
  162.         }
  163.         if(ev.keyCode == '83') { //bw
  164.             this.keyBackward = false;
  165.         }
  166.         if(ev.keyCode == '65') { //left
  167.             this.keyLeft = false;
  168.         }
  169.         if(ev.keyCode == '68') { // right
  170.             this.keyRight = false;
  171.         }
  172.     },
  173.     mouseClickHandler: function(ev) {
  174.        
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment