Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script src="http://spelprogrammering.nu/simple.js">
- g_debug = true;
- g_debugDetachCamera = false;
- g_debugTile = 1;
- g_debugTileX = 0;
- g_debugTileY = 0;
- g_spawnPointX = 0;
- g_spawnPointY = 0;
- g_drawTile = [
- null,
- // Grassblock
- function grass(x, y) {
- //rectangle(x, y, g_tileSize, g_tileSize, "#000");
- //rectangle(x+1, y+1, g_tileSize-2, 12, "#03c600");
- //rectangle(x+1, y+13, g_tileSize-2, 50, "#723f00");
- rectangle(x, y, g_tileSize, 14, "#03c600");
- rectangle(x, y+14, g_tileSize, 50, "#723f00");
- },
- // dirtblock
- function dirt(x, y) {
- //rectangle(x, y, g_tileSize, g_tileSize, "#000");
- //rectangle(x+1, y+1, g_tileSize-2, g_tileSize-2, "#723f00");
- rectangle(x, y, g_tileSize, g_tileSize, "#723f00");
- },
- // Stoneblock
- function stone(x, y) {
- //rectangle(x, y, g_tileSize, g_tileSize, "#000");
- //rectangle(x+1, y+1, g_tileSize-2, g_tileSize-2, "#1f252d");
- rectangle(x, y, g_tileSize, g_tileSize, "#1f252d");
- },
- ];
- g_tileSize = 64;
- let g_camera = {
- x: 0, y: 0,
- w: window.innerWidth, h: window.innerHeight,
- };
- let g_player = {
- x: 50, y: 1400,
- dy: 0,
- w: 73, h: 96,
- movementSpeed: 290,
- jumpingSpeed: 300,
- inAir: false,
- inJump: false, // dy < 0, aka an acceleration upwards
- numJumps: 0,
- maxJumps: 2,
- };
- let g_world = {
- w: 0, h: 0,
- gravity: 450,
- tiles: [],
- cols: 0, rows: 0,
- };
- let g_chunks = [];
- function save_level() {
- }
- function start() {
- // updating camera when the window size
- window.onresize = function(event) {
- g_camera.w = window.innerWidth;
- g_camera.h = window.innerHeight;
- document.documentElement.style.overflow = "hidden";
- };
- //hideMouse();
- g_world.cols = 40;
- g_world.rows = 20;
- g_world.w = g_world.cols * g_tileSize;
- g_world.h = g_world.rows * g_tileSize;
- // Generate the world
- for(let i = 0; i < g_world.cols; i++) {
- g_world.tiles[i] = [];
- for(let j = 0; j < g_world.rows; j++) {
- g_world.tiles[i][j] = 0;
- }
- }
- for(let i = 0; i < g_world.cols; i++) {
- g_world.tiles[i][g_world.rows-1] = 3;
- g_world.tiles[i][g_world.rows-2] = 2;
- g_world.tiles[i][g_world.rows-3] = 2;
- g_world.tiles[i][g_world.rows-4] = 1;
- }
- g_player.x = g_spawnPointX;
- g_player.y = g_spawnPointY;
- }
- function updateInput(dt) {
- if(keyboard.enter) {
- if(g_debug)
- hideMouse();
- else
- showMouse();
- g_debug = !g_debug;
- keyboard.enter = false;
- g_player.x = g_spawnPointX;
- g_player.y = g_spawnPointY;
- }
- if(g_debug) {
- if(keyboard.space) {
- g_debugDetachCamera = !g_debugDetachCamera;
- keyboard.space = false;
- }
- if(keyboard.one) {
- g_debugTile = 1;
- }
- if(keyboard.two) {
- g_debugTile = 2;
- }
- if(keyboard.three) {
- g_debugTile = 3;
- }
- if(mouse.x >= 0 && mouse.x <= window.innerWidth && mouse.y >= 0 && mouse.y <= window.innerHeight) {
- let x = floor(((mouse.x + g_camera.x) / g_world.w) * g_world.cols);
- let y = floor(((mouse.y + g_camera.y) / g_world.h) * g_world.rows);
- if(x >= 0 && x < g_world.cols && y >= 0 && y < g_world.rows) {
- g_debugTileX = x;
- g_debugTileY = y;
- if(mouse.left) {
- g_world.tiles[x][y] = g_debugTile;
- }
- if(mouse.right) {
- g_world.tiles[x][y] = 0;
- }
- }
- }
- }
- if(!g_debugDetachCamera || !g_debug) {
- if(keyboard.a && g_player.x > 0) {
- g_player.x -= g_player.movementSpeed * dt;
- }
- if(keyboard.d && g_player.x + g_player.w < g_world.w) {
- g_player.x += g_player.movementSpeed * dt;
- }
- if(keyboard.w && g_player.numJumps < g_player.maxJumps) {
- g_player.inJump = true;
- g_player.numJumps++;
- g_player.dy = -g_player.jumpingSpeed;
- keyboard.w = false;
- }
- } else {
- if(keyboard.w) {
- g_camera.y -= 600 * dt;
- }
- if(keyboard.s) {
- g_camera.y += 600 * dt;
- }
- if(keyboard.a) {
- g_camera.x -= 600 * dt;
- }
- if(keyboard.d) {
- g_camera.x += 600 * dt;
- }
- }
- }
- function updateCamera(dt) {
- g_camera.x = g_player.x + g_player.w / 2 - g_camera.w / 2;
- g_camera.y = g_player.y + g_player.h / 2 - g_camera.h / 2;
- if(g_camera.x < 0) {
- g_camera.x = 0;
- }
- if(g_camera.x + g_camera.w > g_world.w) {
- g_camera.x = g_world.w - g_camera.w;
- }
- if(g_camera.y < 0) {
- g_camera.y = 0;
- }
- if(g_camera.y + g_camera.h > g_world.h) {
- g_camera.y = g_world.h - g_camera.h;
- }
- }
- let dt, now, last = Date.now();
- function update() {
- if((now = Date.now()) > last) {
- dt = (now - last) * 0.001;
- last = now;
- }
- //doesn't fall through ground
- g_player.inAir = true;
- if(g_player.y + g_player.h >= g_world.h && !g_player.inJump) {
- g_player.inAir = false;
- g_player.inJump = false;
- g_player.numJumps = 0;
- g_player.dy = 0;
- g_player.y = g_world.h - g_player.h ;
- }
- updateInput(dt);
- if(g_player.inAir && g_player.dy < g_world.gravity) {
- g_player.dy += g_world.gravity * dt;
- }
- if(g_player.dy > 0) {
- g_player.inJump = false;
- }
- g_player.y += g_player.dy * dt;
- if(!g_debugDetachCamera || !g_debug)
- updateCamera(dt);
- draw();
- }
- function draw() {
- clearScreen();
- fill("#b5d1ff");
- // tiles
- for(let i = 0; i < g_world.cols; i++) {
- for(let j = 0; j < g_world.rows; j++) {
- let x = i * g_tileSize - g_camera.x;
- let y = j * g_tileSize - g_camera.y;
- if(x + g_tileSize > 0 && x < g_camera.w && y + g_tileSize > 0 && y < g_camera.h) {
- if(g_world.tiles[i][j] != 0) {
- g_drawTile[g_world.tiles[i][j]](
- x,
- y
- );
- }
- }
- }
- }
- // debugging info
- if(g_debug) {
- // rutnät
- for(let i = 0; i <= g_world.rows; i++) {
- let y = i * g_tileSize - g_camera.y;
- line(0 - g_camera.x, y, g_world.w - g_camera.x, y, 1, "#000");
- }
- for(let i = 0; i <= g_world.cols; i++) {
- let x = i * g_tileSize - g_camera.x;
- line(x, 0 - g_camera.y, x, g_world.h - g_camera.y, 1, "#000");
- }
- // choosen tile
- let x = g_tileSize * g_debugTileX - g_camera.x;
- let y = g_tileSize * g_debugTileY - g_camera.y;
- line(x-1, y-1, x-1, y+g_tileSize+1, 1, "#000");
- line(x-1, y-1, x+g_tileSize+1, y-1, 1, "#000");
- line(x-1, y+g_tileSize+1, x+g_tileSize+1, y+g_tileSize+1, 1, "#000");
- line(x+g_tileSize+1, y+g_tileSize+1, x+g_tileSize+1, y-1, 1, "#000");
- text(
- 253,
- 10,
- 10,
- "tile (" + g_debugTileX + ", " + g_debugTileY + "): " + g_world.tiles[g_debugTileX][g_debugTileY],
- "#fff"
- );
- text(253, 20, 10, "setTile: " + g_debugTile, "#fff");
- text(3, 10, 10, "x: " + floor(g_player.x), "#fff");
- text(3, 20, 10, "y: " + floor(g_player.y) + " (" + floor(g_world.h - g_player.y - g_player.h) + ")", "#fff");
- text(113, 10, 10, "inJump: " + g_player.inJump, "#fff");
- text(113, 20, 10, "dy: " + floor(g_player.dy), "#fff");
- text(113, 40, 10, "numJumps: " + g_player.numJumps, "#fff");
- text(113, 50, 10, "inAir: " + g_player.inAir, "#fff");
- }
- // player
- if(g_player.inJump != true){
- rectangle(
- g_player.x - g_camera.x,
- g_player.y - g_camera.y,
- g_player.w,
- g_player.h,
- "white"
- );
- }
- else if(g_player.inJump == true){
- rectangle(
- g_player.x - g_camera.x,
- g_player.y - g_camera.y,
- g_player.w,
- g_player.h,
- "white"
- // "Bob_Jumping_1.png"
- );
- }
- //picture(g_player.x - g_camera.x, g_player.y - g_camera.y, "bosse.png");
- }
- function playerTileCollision(x, y) {
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement