Advertisement
myname0

tmp

Jul 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //function draw(){
  2. //    var canvas = document.getElementById("canvas");
  3. //    var context = canvas.getContext("2d");
  4. //    context.fillRect(0, 500, 150, 100);
  5. //    
  6. //    draw = function(){
  7. //        context.fillRect(100, 500, 150, 100);
  8. //    }
  9. //}
  10. (function(){
  11. //    var canvas = document.getElementById("canvas"),
  12. //        context = canvas.getContext("2d"),
  13. //        requestAnimationFrame=(function(){
  14. //            return window.requestAnimationFrame || window.webkitRequestAnimationFrame ||
  15. //                window.mozRequestAnimationFrame || window.oRequestAnimationFrame ||
  16. //                window.msRequestAnimationFrame ||
  17. //                function(callback){
  18. //                window.setTimeout(callback, 1000 / 60);  
  19. //            };
  20. //        }),
  21. //        start = Date.now(),
  22. //        gameTime = 0;
  23. //    
  24. //    canvas.onclick = function(){
  25. //      animate(function(timePassed){
  26. //          context.fillRect(timePassed / 5, 500, 150, 100);
  27. //      }, 2000);  
  28. //    };
  29. //    
  30. //    function animate(draw, duration){
  31. //        var start = performance.now();
  32. //        
  33. //        requestAnimationFrame(function animate(time){
  34. //            var timePassed = time - start;
  35. //            
  36. //            console.log(time, start);
  37. //            
  38. //            if(timePassed > duration) timePassed = duration;
  39. //            
  40. //            draw(timePassed);
  41. //            
  42. //            if(timePassed < duration){
  43. //                requestAnimationFrame(animate);
  44. //            }
  45. //        });
  46. //    };
  47. //    
  48. //    function main(){
  49. //        var now = Date.now(),
  50. //            timePassed = (now - start) / 1000.0;
  51. //        
  52. //        update(timePassed);
  53. //        render();
  54. //        
  55. //        start = now;
  56. //        
  57. //        requestAnimationFrame(main);
  58. //    };
  59. //    
  60. //    finction update(timePassed){
  61. //        gameTime += timePassed;
  62. //        
  63. //        
  64. //    };
  65.    
  66.    
  67.     var APP = APP || {};
  68.    
  69.     APP.namespace = function (string) {
  70.         var parts = string.split('.'),
  71.             parent = APP,
  72.             i;
  73.  
  74.         if(parts[0] == "APP") {
  75.             parts = parts.slice(1);
  76.         }
  77.  
  78.         for(i = 0; i < parts.length; i += 1) {
  79.             if(typeof parent[parts[i]] === "undefined") {
  80.                 parent[parts[i]] = {};
  81.             }
  82.  
  83.             parent = parent[parts[i]];
  84.         }
  85.  
  86.         return parent;
  87.     };
  88.    
  89.    
  90.     APP.namespace("APP.controller.keyHandler");
  91.    
  92.     APP.controller.keyHandler = (function () {
  93.         var keys = [];
  94.        
  95.         document.body.addEventListener('keydown', function(e){
  96.             switch(e.keyCode){
  97.                 case 37:
  98.                     keys["left"] = true;
  99.                     break;
  100.                 case 38:
  101.                     keys["up"] = true;
  102.                     break;
  103.                 case 39:
  104.                     keys["right"] = true;
  105.                     break;
  106.                 case 40:
  107.                     keys["down"] = true;
  108.                     break;
  109.             }
  110.                            
  111.             //keys[e.keyCode] = true;
  112.         });
  113.  
  114.         document.body.addEventListener('keyup', function(e){
  115.             //keys[e.keyCode] = false;
  116.             switch(e.keyCode){
  117.                 case 37:
  118.                     keys["left"] = false;
  119.                     break;
  120.                 case 38:
  121.                     keys["up"] = false;
  122.                     break;
  123.                 case 39:
  124.                     keys["right"] = false;
  125.                     break;
  126.                 case 40:
  127.                     keys["down"] = false;
  128.                     break;
  129.             }
  130.         });
  131.        
  132.         return {
  133.             getKeys : function () {
  134.                 return keys;
  135.             }
  136.         };
  137.        
  138.     }());
  139.    
  140.    
  141.     APP.namespace("APP.model.player");
  142.    
  143.     APP.model.player = (function () {
  144.        
  145.         var keyHandler = APP.controller.keyHandler;
  146.             Player;
  147.             keys = keyHandler.getKeys();
  148.        
  149.         Player = function Player(wdth, hght, x, y){
  150.             this.width = wdth;
  151.             this.height = hght;
  152.             this.x = x;
  153.             this.y = y;
  154.             this.speed = 2;
  155.             this.friction = 0.7;
  156.             this.vel_x = this.vel_y = 0;
  157.         };
  158.        
  159.         Player.prototype = {
  160.             getX: function () {
  161.                 return this.x;
  162.             },
  163.            
  164.             getY: function () {
  165.                 return this.y;
  166.             },
  167.            
  168.             getWidth: function () {
  169.                 return this.width;
  170.             },
  171.            
  172.             getHeight: function () {
  173.                 return this.height;
  174.             },
  175.            
  176.             run: function () {
  177.                  switch(true){
  178.                     case keys["left"]:
  179.                         this.vel_x -= this.speed;
  180.                         break;
  181.  
  182.                     case keys["right"]:
  183.                         this.vel_x += this.speed;
  184.                         break;
  185.  
  186.                     case keys["up"]:
  187.                         this.vel_y -= this.speed;
  188.                         break;
  189.  
  190.                     case keys["down"]:
  191.                         this.vel_y += this.speed;
  192.                         break;    
  193.                 }
  194.  
  195.                 this.vel_x *= this.friction;
  196.                 this.vel_y *= this.friction;
  197.  
  198.                 this.x += this.vel_x;
  199.                 this.y += this.vel_y;
  200.  
  201.                 this.x = this.x <= 0 ? 0 : this.x;
  202.                 this.y = this.y <= 0 ? 0 : this.y;
  203.  
  204.                 this.x = this.x >= canvas.width - this.width ? canvas.width - this.width : this.x;
  205.                 this.y = this.y >= canvas.height - this.height ? canvas.height - this.height : this.y;
  206.             }
  207.         };
  208.        
  209.         return Player;
  210.        
  211.     }());
  212.    
  213.     APP.namespace("APP.view.canvasView");
  214.    
  215.     APP.view.canvasView = (function () {
  216.         (function(){
  217.             var requestAnimationFrame = window.requestAnimationFrame ||
  218.                                         window.mozRequestAnimationFrame ||
  219.                                         window.webkitRequestAnimationFrame ||
  220.                                         window.msRequestAnimationFrame;
  221.             window.requestAnimationFrame = requestAnimationFrame;
  222.         })();
  223.        
  224.          var canvas = document.getElementById('canvas'),
  225.              context = canvas.getContext('2d'),
  226.              player = new APP.model.player(60, 50, 0, canvas.height - 50);
  227.        
  228.         function render() {
  229.             context.clearRect(0,0, canvas.width, canvas.height);
  230.             context.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight())
  231.             player.run();
  232.         }
  233.  
  234.         (function gameLoop(){
  235.             render();
  236.             requestAnimationFrame(gameLoop);
  237.         })();
  238.        
  239.     }());
  240.    
  241.    
  242.    
  243.    
  244.  
  245.  
  246. //    var canvas = document.getElementById('canvas'),
  247. //        ctx = canvas.getContext('2d'),
  248. //        //keys = [];
  249.  
  250.    
  251.  
  252. //    function Player(wdth, hght){
  253. //        this.width = wdth;
  254. //        this.height = hght;
  255. //        this.x = 0;
  256. //        this.y = canvas.height - this.height;
  257. //        this.speed = 2;
  258. //        this.friction = 0.7;
  259. //        this.vel_x = this.vel_y = 0;
  260. //    }
  261.  
  262. //    Player.prototype.load = function() {
  263. //        ctx.fillRect(this.x, this.y, this.width, this.height);
  264. //    };
  265.  
  266. //    Player.prototype.run = function() {
  267. //        switch(true){
  268. //            case keys[37]:
  269. //                this.vel_x -= this.speed;
  270. //                break;
  271. //
  272. //            case keys[39]:
  273. //                this.vel_x += this.speed;
  274. //                break;
  275. //
  276. //            case keys[38]:
  277. //                this.vel_y -= this.speed;
  278. //                break;
  279. //
  280. //            case keys[40]:
  281. //                this.vel_y += this.speed;
  282. //                break;    
  283. //        }
  284. //
  285. //        this.vel_x *= this.friction;
  286. //        this.vel_y *= this.friction;
  287. //
  288. //        this.x += this.vel_x;
  289. //        this.y += this.vel_y;
  290. //
  291. //        this.x = this.x <= 0 ? 0 : this.x;
  292. //        this.y = this.y <= 0 ? 0 : this.y;
  293. //
  294. //        this.x = this.x >= canvas.width - this.width ? canvas.width - this.width : this.x;
  295. //        this.y = this.y >= canvas.height - this.height ? canvas.height - this.height : this.y;
  296. //
  297. //    };
  298.  
  299.  
  300.  
  301. //    var player1 = new Player(50, 80);
  302.  
  303.    
  304.  
  305. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement