Advertisement
Guest User

Game Editor

a guest
Mar 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script src="http://spelprogrammering.nu/simple.js">
  2.  
  3. g_debug = true;
  4. g_debugDetachCamera = false;
  5.  
  6. g_debugTile = 1;
  7. g_debugTileX = 0;
  8. g_debugTileY = 0;
  9.  
  10. g_spawnPointX = 0;
  11. g_spawnPointY = 0;
  12.  
  13.  
  14. g_drawTile = [
  15.     null,
  16.     // Grassblock
  17.     function grass(x, y) {
  18.         //rectangle(x, y, g_tileSize, g_tileSize, "#000");
  19.         //rectangle(x+1, y+1, g_tileSize-2, 12, "#03c600");
  20.         //rectangle(x+1, y+13, g_tileSize-2, 50, "#723f00");
  21.         rectangle(x, y, g_tileSize, 14, "#03c600");
  22.         rectangle(x, y+14, g_tileSize, 50, "#723f00");
  23.     },
  24.     // dirtblock
  25.     function dirt(x, y) {
  26.         //rectangle(x, y, g_tileSize, g_tileSize, "#000"); 
  27.         //rectangle(x+1, y+1, g_tileSize-2, g_tileSize-2, "#723f00");
  28.         rectangle(x, y, g_tileSize, g_tileSize, "#723f00");
  29.     },
  30.     // Stoneblock
  31.     function stone(x, y) {
  32.         //rectangle(x, y, g_tileSize, g_tileSize, "#000");
  33.         //rectangle(x+1, y+1, g_tileSize-2, g_tileSize-2, "#1f252d");
  34.         rectangle(x, y, g_tileSize, g_tileSize, "#1f252d");
  35.     },
  36. ];
  37.  
  38. g_tileSize = 64;
  39.  
  40. let g_camera = {
  41.     x: 0, y: 0,
  42.     w: window.innerWidth, h: window.innerHeight,
  43. };
  44.  
  45. let g_player = {
  46.     x: 50, y: 1400,
  47.     dy: 0,
  48.     w: 73, h: 96,
  49.    
  50.     movementSpeed: 290,
  51.     jumpingSpeed: 300,
  52.     inAir: false,
  53.     inJump: false, // dy < 0, aka an acceleration upwards
  54.    
  55.     numJumps: 0,
  56.     maxJumps: 2,
  57. };
  58.  
  59. let g_world = {
  60.     w: 0, h: 0,
  61.    
  62.     gravity: 450,
  63.    
  64.     tiles: [],
  65.     cols: 0, rows: 0,
  66. };
  67.  
  68. let g_chunks = [];
  69.  
  70. function save_level() {
  71.    
  72. }
  73.  
  74. function start() {
  75.     // updating camera when the window size
  76.     window.onresize = function(event) {
  77.         g_camera.w = window.innerWidth;
  78.         g_camera.h = window.innerHeight;
  79.        
  80.         document.documentElement.style.overflow = "hidden";
  81.     };
  82.    
  83.     //hideMouse();
  84.    
  85.     g_world.cols = 40;
  86.     g_world.rows = 20;
  87.     g_world.w = g_world.cols * g_tileSize;
  88.     g_world.h = g_world.rows * g_tileSize;
  89.    
  90.     // Generate the world
  91.     for(let i = 0; i < g_world.cols; i++) {
  92.         g_world.tiles[i] = [];
  93.         for(let j = 0; j < g_world.rows; j++) {
  94.             g_world.tiles[i][j] = 0;
  95.         }
  96.     }
  97.    
  98.     for(let i = 0; i < g_world.cols; i++) {
  99.         g_world.tiles[i][g_world.rows-1] = 3;
  100.         g_world.tiles[i][g_world.rows-2] = 2;
  101.         g_world.tiles[i][g_world.rows-3] = 2;
  102.         g_world.tiles[i][g_world.rows-4] = 1;
  103.     }
  104.    
  105.     g_player.x = g_spawnPointX;
  106.     g_player.y = g_spawnPointY;
  107. }
  108.  
  109. function updateInput(dt) {
  110.     if(keyboard.enter) {
  111.         if(g_debug)
  112.             hideMouse();
  113.         else
  114.             showMouse();
  115.        
  116.         g_debug = !g_debug;
  117.         keyboard.enter = false;
  118.        
  119.         g_player.x = g_spawnPointX;
  120.         g_player.y = g_spawnPointY;
  121.     }
  122.    
  123.     if(g_debug) {
  124.         if(keyboard.space) {
  125.             g_debugDetachCamera = !g_debugDetachCamera;
  126.            
  127.             keyboard.space = false;
  128.         }
  129.        
  130.         if(keyboard.one) {
  131.             g_debugTile = 1;
  132.         }
  133.         if(keyboard.two) {
  134.             g_debugTile = 2;
  135.         }
  136.         if(keyboard.three) {
  137.             g_debugTile = 3;
  138.         }
  139.        
  140.         if(mouse.x >= 0 && mouse.x <= window.innerWidth && mouse.y >= 0 && mouse.y <= window.innerHeight) {
  141.             let x = floor(((mouse.x + g_camera.x) / g_world.w) * g_world.cols);
  142.             let y = floor(((mouse.y + g_camera.y) / g_world.h) * g_world.rows);
  143.            
  144.             if(x >= 0 && x < g_world.cols && y >= 0 && y < g_world.rows) {
  145.                 g_debugTileX = x;
  146.                 g_debugTileY = y;
  147.                
  148.                 if(mouse.left) {
  149.                     g_world.tiles[x][y] = g_debugTile;
  150.                 }
  151.                
  152.                 if(mouse.right) {
  153.                     g_world.tiles[x][y] = 0;
  154.                 }
  155.             }
  156.         }
  157.     }
  158.    
  159.     if(!g_debugDetachCamera || !g_debug) {
  160.         if(keyboard.a && g_player.x > 0) {
  161.             g_player.x -= g_player.movementSpeed * dt;
  162.         }
  163.        
  164.         if(keyboard.d && g_player.x + g_player.w < g_world.w) {
  165.             g_player.x += g_player.movementSpeed * dt;
  166.         }
  167.        
  168.         if(keyboard.w && g_player.numJumps < g_player.maxJumps) {
  169.             g_player.inJump = true;
  170.             g_player.numJumps++;
  171.            
  172.             g_player.dy = -g_player.jumpingSpeed;
  173.            
  174.             keyboard.w = false;
  175.         }
  176.     } else {
  177.         if(keyboard.w) {
  178.             g_camera.y -= 600 * dt;
  179.         }
  180.         if(keyboard.s) {
  181.             g_camera.y += 600 * dt;
  182.         }
  183.         if(keyboard.a) {
  184.             g_camera.x -= 600 * dt;
  185.         }
  186.         if(keyboard.d) {
  187.             g_camera.x += 600 * dt;
  188.         }
  189.     }
  190. }
  191.  
  192. function updateCamera(dt) {
  193.     g_camera.x = g_player.x + g_player.w / 2 - g_camera.w / 2;
  194.     g_camera.y = g_player.y + g_player.h / 2 - g_camera.h / 2;
  195.    
  196.     if(g_camera.x < 0) {
  197.         g_camera.x = 0;
  198.     }
  199.    
  200.     if(g_camera.x + g_camera.w > g_world.w) {
  201.         g_camera.x = g_world.w - g_camera.w;
  202.     }
  203.    
  204.     if(g_camera.y < 0) {
  205.         g_camera.y = 0;
  206.     }
  207.    
  208.     if(g_camera.y + g_camera.h > g_world.h) {
  209.         g_camera.y = g_world.h - g_camera.h;
  210.     }
  211. }
  212.  
  213. let dt, now, last = Date.now();
  214. function update() {
  215.     if((now = Date.now()) > last) {
  216.         dt = (now - last) * 0.001;
  217.         last = now;
  218.     }
  219.    
  220.     //doesn't fall through ground
  221.     g_player.inAir = true;
  222.     if(g_player.y + g_player.h >= g_world.h && !g_player.inJump) {
  223.         g_player.inAir = false;
  224.         g_player.inJump = false;
  225.         g_player.numJumps = 0;
  226.        
  227.         g_player.dy = 0;
  228.         g_player.y = g_world.h - g_player.h ;
  229.     }
  230.    
  231.    
  232.    
  233.     updateInput(dt);
  234.    
  235.     if(g_player.inAir && g_player.dy < g_world.gravity) {
  236.         g_player.dy += g_world.gravity * dt;
  237.     }
  238.    
  239.     if(g_player.dy > 0) {
  240.         g_player.inJump = false;
  241.     }
  242.    
  243.     g_player.y += g_player.dy * dt;
  244.    
  245.     if(!g_debugDetachCamera || !g_debug)
  246.         updateCamera(dt);
  247.    
  248.     draw();
  249. }
  250.  
  251. function draw() {
  252.     clearScreen();
  253.    
  254.     fill("#b5d1ff");
  255.    
  256.     // tiles
  257.     for(let i = 0; i < g_world.cols; i++) {
  258.         for(let j = 0; j < g_world.rows; j++) {
  259.             let x = i * g_tileSize - g_camera.x;
  260.             let y = j * g_tileSize - g_camera.y;
  261.            
  262.             if(x + g_tileSize > 0 && x < g_camera.w && y + g_tileSize > 0 && y < g_camera.h) {
  263.                 if(g_world.tiles[i][j] != 0) {
  264.                     g_drawTile[g_world.tiles[i][j]](
  265.                         x,
  266.                         y
  267.                     );
  268.                 }
  269.             }
  270.         }
  271.     }
  272.    
  273.     // debugging info
  274.     if(g_debug) {
  275.         // rutnät
  276.         for(let i = 0; i <= g_world.rows; i++) {
  277.             let y = i * g_tileSize - g_camera.y;
  278.             line(0 - g_camera.x, y, g_world.w - g_camera.x, y, 1, "#000");
  279.         }
  280.        
  281.         for(let i = 0; i <= g_world.cols; i++) {
  282.             let x = i * g_tileSize - g_camera.x;
  283.             line(x, 0 - g_camera.y, x, g_world.h - g_camera.y, 1, "#000");
  284.         }
  285.        
  286.         // choosen tile
  287.         let x = g_tileSize * g_debugTileX - g_camera.x;
  288.         let y = g_tileSize * g_debugTileY - g_camera.y;
  289.        
  290.         line(x-1, y-1, x-1, y+g_tileSize+1, 1, "#000");
  291.         line(x-1, y-1, x+g_tileSize+1, y-1, 1, "#000");
  292.         line(x-1, y+g_tileSize+1, x+g_tileSize+1, y+g_tileSize+1, 1, "#000");
  293.         line(x+g_tileSize+1, y+g_tileSize+1, x+g_tileSize+1, y-1, 1, "#000");
  294.        
  295.         text(
  296.             253,
  297.             10,
  298.             10,
  299.             "tile (" + g_debugTileX + ", " + g_debugTileY + "): " + g_world.tiles[g_debugTileX][g_debugTileY],
  300.             "#fff"
  301.         );
  302.         text(253, 20, 10, "setTile: " + g_debugTile, "#fff");
  303.        
  304.         text(3, 10, 10, "x: " + floor(g_player.x), "#fff");
  305.         text(3, 20, 10, "y: " + floor(g_player.y) + " (" + floor(g_world.h - g_player.y - g_player.h) + ")", "#fff");
  306.        
  307.         text(113, 10, 10, "inJump: " + g_player.inJump, "#fff");
  308.         text(113, 20, 10, "dy: " + floor(g_player.dy), "#fff");
  309.         text(113, 40, 10, "numJumps: " + g_player.numJumps, "#fff");
  310.         text(113, 50, 10, "inAir: " + g_player.inAir, "#fff");
  311.     }
  312.    
  313.     // player
  314.     if(g_player.inJump != true){
  315.     rectangle(
  316.         g_player.x - g_camera.x,
  317.         g_player.y - g_camera.y,
  318.         g_player.w,
  319.         g_player.h,
  320.         "white"
  321.     );
  322.     }
  323.     else if(g_player.inJump == true){
  324.     rectangle(
  325.         g_player.x - g_camera.x,
  326.         g_player.y - g_camera.y,
  327.         g_player.w,
  328.         g_player.h,
  329.         "white"
  330.     //  "Bob_Jumping_1.png"
  331.     );
  332.     }
  333.     //picture(g_player.x - g_camera.x, g_player.y - g_camera.y, "bosse.png");
  334. }
  335.  
  336. function playerTileCollision(x, y) {
  337.    
  338. }
  339.  
  340. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement