Untitled

 avatar
unknown
plain_text
a month ago
6.9 kB
2
Indexable
    
  
var keys = [];
var game = function() {
    this.level = myMap;
    this.players = [];
    this.blocks = [];
    this.airs = [];
    this.blockSize = 20;
    this.tagID = 1;
};
var block = function(x,y,id,scal) {
    
    this.x = x;
    this.y = y;
    this.id = id;
    this.scal = scal;
};
block.prototype.collision = function(x,y) {
    if(x >= this.x && y >= this.y && x <= this.x+this.scal && y <= this.y+this.scal){
        return true;
    }else{
        return false;
    }
};
block.prototype.draw = function() {
    noStroke();
    switch(this.id){
        default:
        break;
        case 1:
            stroke(250, 0, 17);
            fill(0, 0, 0);
            rect(this.x,this.y,this.scal,this.scal);
        break;
    }
};
var player = function(x,y,blocks,scal,controls,id,isIt) {
    this.blocks = blocks;
    this.x = x;
    this.y = y;
    this.scal = scal/2;
    this.acc = 0.1;
    this.grav = 0.1;
    this.velX = 0;
    this.velY = 0;
    this.jumpCount = 0;
    this.jumped = false;
    this.cS = controls;
    this.id = id;
    this.isIt = isIt;
};
player.prototype.update = function() {
    if(this.velY >= 0){
         this.jumped = false;
    }
    if(this.jumped){
        for(var i = 0; i < abs(this.velY); i++){
            if(!this.checkCollision(0) && this.y > 0){
                this.y--;
            }else{
                this.jumped = false;
                this.velY = 0;
            }
        }
    }else{
        for(var i = 0; i < this.velY; i++){
            if(!this.checkCollision(2)){
                this.y++;
            }
        }
    }
    if(!this.checkCollision(2)){
        this.velY+=0.1;
    }else if(this.jumped === false){
        this.velY = 0;
        this.jumpCount = 0;
    }
    this.move();
};
player.prototype.draw = function() {
    noStroke();
    fill(0, 0, 255);
    rect(this.x,this.y,this.scal,this.scal);
    if(this.isIt){
        fill(255, 0, 0);
        rect(this.x,this.y,this.scal/1,this.scal/1);
    }
    textAlign(CENTER,BOTTOM);
    fill(179, 0, 0);
    text("P" + this.id,this.x+(this.scal/2),this.y);
};
player.prototype.checkCollision = function(side) {
    //0 = top, 1 = right, 2 = bottom, 3 = left
    this.collision = false;
    for(var i in this.blocks){
        if(side === 0){
            this.s1 = this.blocks[i].collision(this.x+1,this.y);
            this.s2 = this.blocks[i].collision(this.x+this.scal-1,this.y);
        }else if(side === 1){
            this.s1 = this.blocks[i].collision(this.x+this.scal,this.y+1);
            this.s2 = this.blocks[i].collision(this.x+this.scal,this.y+this.scal-1);
        }else if(side === 2){
            this.s1 = this.blocks[i].collision(this.x+1,this.y+this.scal);
            this.s2 = this.blocks[i].collision(this.x+this.scal-1,this.y+this.scal);
         }else if(side === 3){
            //This is buggy for some odd reason
            this.s1 = this.blocks[i].collision(this.x,this.y+1);
            this.s2 = this.blocks[i].collision(this.x,this.y+this.scal-1);
        }
        if(this.s1 || this.s2){
            this.collision = true;
        }
    }
    if(this.collision){
        return true;
    }else{
        return false;
    }
};
player.prototype.move = function() {
    if(keys[this.cS[0]] && this.jumped === false && this.jumpCount < 1 && this.velY === 0){
        playSound(getSound("retro/jump2"));
        this.velY = -4;
        this.jumped = true;
        this.jumpCount++;
    }
    if(!this.checkCollision(2) && keys[this.cS[1]]){
        
    }
    if(keys[this.cS[2]]){
        for(var i = 0; i < 3; i++){
            if(!this.checkCollision(3) && this.x > 0){
                this.x--;
            }
        }
    }
    if(keys[this.cS[3]]){
        for(var i = 0; i < 3; i++){
            if(!this.checkCollision(1) && this.x+this.scal  < 400){
                this.x++;
            }
        }
    }
};
var controls = [[UP,DOWN,LEFT,RIGHT],[87,83,65,68],[73,75,74,76]];
game.prototype.setupLevel = function() {
    for(var y in this.level){
        for(var x in this.level[y]){
            if(this.level[y][x] !== 0 && this.level[y][x] !== 2){
                this.blocks.push(new block(x*this.blockSize,y*this.blockSize,this.level[y][x],this.blockSize));
            }
        }
    }
    for(var y in this.level){
        for(var x in this.level[y]){
            if(this.level[y][x] === 2 && this.tagID === this.players.length+1){
                this.players.push(new player(x*this.blockSize,y*this.blockSize,this.blocks,this.blockSize,controls[this.players.length % controls.length],this.players.length+1,true));
            }else if(this.level[y][x] === 2){
                this.players.push(new player(x*this.blockSize,y*this.blockSize,this.blocks,this.blockSize,controls[this.players.length % controls.length],this.players.length+1,false));
            }
        }
    }
};
game.prototype.setupBlocks = function() {
    for(var y in this.level){
        for(var x in this.level[y]){
            if(this.level[y][x] !== 0 && this.level[y][x] !== 2){
                this.blocks.push(new block(x*this.blockSize,y*this.blockSize,this.level[y][x],this.blockSize));
            }
        }
    }
};
game.prototype.update = function() {
    this.pCords = [];
    for(var i in this.players){
        this.players[i].update();
        this.pCords.push([this.players[i].x,this.players[i].y,this.players[i].scal]);
    }
    if(this.players[0].x>=this.players[1].x&&
    this.players[0].y>=this.players[1].y&&
    this.players[0].x<=this.players[1].x+this.players[1].scal&&
    this.players[0].y<=this.players[1].y+this.players[1].scal && this.tagID !== 3||
    this.players[0].x+this.players[0].scal>=this.players[1].x&&
    this.players[0].y+this.players[0].scal>=this.players[1].y&&
    this.players[0].x+this.players[0].scal<=this.players[1].x+this.players[1].scal&&
    this.players[0].y+this.players[0].scal<=this.players[1].y+this.players[1].scal && this.tagID !== 3){
        //println("P" + this.tagID + "Tagged!");
        if(this.tagID === 2){
            this.tagID = 1;
        }else{
            this.tagID = 2;
        }
        this.players = [];
        this.blocks = [];
        this.setupLevel();
    }
    
    
};
game.prototype.render = function() {
    for(var i in this.airs) {
        this.airs[i].draw();
    }
    for(var i in this.blocks){
        this.blocks[i].draw();
    }
    for(var i in this.players){
        this.players[i].draw();
    }
};
game.prototype.input = function() {
    for(var i in this.players){
        //this.players[i].move();
    }
};
var Game = new game();
Game.setupLevel();
background(59, 64, 199);
draw = function() {
    fill(0, 0, 0,10);
    rect(0,0,400,400);
    Game.render();
    Game.update();
};
keyPressed = function() {
    keys[keyCode] = true;
};
keyReleased = function() {
    keys[keyCode] = false;
};
Editor is loading...
Leave a Comment