I having many troubles on this Javascript 2d shooter for a college project. I did already the tile map, the player sprite, bullets mechanics, but now i got to the part of colisions... And i'm not understanding how to start any code for this.
I want my player on top of the platform, i did playerx = map [10] and playery = map [9], but if i put any more values this is the position i'm getting and i know this is not the right way but i'm really lost:
I just want to understand how can i link my player to my tile map, since my player has no tile properties? What variables or controllers i need to get this to work? And more important do i need to change this code that i had already done in order to get this working??
window.onload = function () {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');
var cols = 25;
var rows = 18;
var tileWidth = 32;
var tileHeight = 32;
var tileX = 0;
var tileY = 0;
canvas.width = cols * tileWidth;
canvas.height = rows * tileHeight;
var map = [
467,468,468,468,468,468,468,467,468,468,468,468,468,468,468,468,467,468,468,468,468,468,467,468,468,
499,500,501,468,468,468,468,499,500,501,468,468,468,468,468,468,499,500,501,468,468,468,499,500,501,
468,468,468,468,468,467,468,468,468,468,468,499,500,501,468,468,468,468,468,467,468,468,468,468,468,
468,468,468,468,468,499,500,501,468,468,468,468,468,468,468,468,468,468,468,499,500,501,468,468,468,
468,467,468,468,468,468,468,468,468,467,468,468,468,468,467,468,468,468,468,468,468,468,467,468,468,
468,499,500,501,468,468,468,468,468,499,500,501,468,468,499,500,501,468,468,468,468,468,499,500,501,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,282,282,282,282,282,282,282,282,282,468,468,468,468,468,468,468,468,
468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,
258,258,258,258,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,468,258,258,258,258,
528,528,528,528,258,258,258,258,258,258,258,258,258,258,258,258,258,258,258,258,258,528,528,528,528,
528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,
528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528,528
];
var map467 = new Image();
map467.src = "467_tile.png"
var map468 = new Image();
map468.src = "468_tile.png"
var map499 = new Image();
map499.src = "499_tile.png"
var map500 = new Image();
map500.src = "500_tile.png"
var map501 = new Image();
map501.src = "501_tile.png"
var map528 = new Image();
map528.src = "528_tile.png"
var map258 = new Image();
map258.src = "258_tile.png"
var map282 = new Image();
map282.src = "282_tile.png"
var keys = [];
var player = {
x: map [10],
y: map [9],
width: 48,
height: 48,
frameX: 0,
frameY: 1,
speed: 3,
moving: false,
movingLeft: false,
movingRight: false,
jump: false,
jumpSpeed:10,
jumpLeft: false,
jumpRight: false,
gravity: 0.5,
jumpY: 500,
velY: 0,
ground: true,
groundY: 552,
direction: 0,
facing: 'right',
shot:false
};
var enemy = {
enemyX: 0,
enemyY:600-48,
enemyWidth:48,
enemyHeight:48,
enemySpeed: 2,
enemyFrameX: 0,
enemyFrameY: 0,
};
var bullets = [];
var bullet = {
color: '#FFFFFF',
width: 4,
height: 2,
x: 0,
y: 0,
speed: 3.5,
direction: null
}
var enemySprite = new Image();
enemySprite.src = "Sprites/trump_run_sprite.png";
var playerSprite = new Image();
playerSprite.src = "Sprites/sprite_sheet_soldier3.png";
function drawEnemy(img, sX, sY, sW, sH, dX, dY, dW, dH) {
ctx.drawImage(img, sX, sY, sW, sH, dX, dY, dW, dH);
}
function drawSprite(img, sX, sY, sW, sH, dX, dY, dW, dH) {
ctx.drawImage(img, sX, sY, sW, sH, dX, dY, dW, dH);
}
function drawMap() {
for (var y = 0; y < rows; y++){
for (var x = 0; x<cols; x++) {
switch (map[((y*cols)+x)]){
case 467:
ctx.drawImage(map467, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
case 468:
ctx.drawImage(map468, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
case 499:
ctx.drawImage(map499, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
case 500:
ctx.drawImage(map500, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
case 501:
ctx.drawImage(map501, x*tileWidth,y*tileHeight, tileWidth, tileHeight);
break;
case 528:
ctx.drawImage(map528, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
case 282:
ctx.drawImage(map282, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
case 258:
ctx.drawImage(map258, x*tileWidth ,y*tileHeight, tileWidth, tileHeight);
break;
}
}
}
}
function animate() { // animation loop
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawMap();
drawEnemy(enemySprite, enemy.enemyWidth * enemy.enemyFrameX, enemy.enemyHeight * enemy.enemyFrameY, enemy.enemyWidth, enemy.enemyHeight, enemy.enemyX, enemy.enemyY, enemy.enemyWidth, enemy.enemyHeight);
drawSprite(playerSprite, player.width * player.frameX, player.height * player.frameY, player.width, player.height, player.x, player.y, player.width, player.height);
movePlayer();
drawBullet();
MoveEnemy();
MovingSprite();
requestAnimationFrame(animate);
}
setTimeout(animate, 100);
function movePlayer() {
//Left
if (keys[37] && player.x > 0) {
player.x -= player.speed;
player.frameY = 2;
player.movingLeft = true;
player.movingRight = false;
player.direction =0
player.facing = "left"
}
//Right
if (keys[39] && player.x < canvas.width - player.width) {
player.x += player.speed;
player.frameY = 3;
player.movingRight = true
player.movingLeft = false
player.direction = 1;
player.facing = "right";
}
// UP
if (keys[38]) {
player.jump = true;
player.ground = false;
player.y -= player.jumpSpeed;
player.frameY = 5;
}
// JUMP + GRAVITY (BOUNCE EFFECT)
if (player.jump === true) {
player.velY += player.gravity
player.y += player.velY
if (player.y >= canvas.height - player.height && player.jump === true) {
player.y = canvas.height - player.height
player.velY = 0
player.jump = false;
player.ground = true
}
}
}
function MovingSprite() {
// RUN LEFT & RIGHT + IDLE ANIMATION SPRITE + JUMP (LATER)
if (player.frameX < 5) {
player.frameX++
} else player.frameX = 0;
if (player.moving === false && player.movingLeft === true && player.movingRight === false ) {
player.frameY = 4;
player.direction = 0;
if (player.frameX < 4) {
player.frameX++
} else player.frameX = 0;
}
if ( player.moving=== false && player.movingRight === true && player.movingLeft === false) {
player.frameY = 1;
player.direction = 1;
if (player.frameX < 4) {
player.frameX++
} else player.frameX = 0;
}
}