Pastebin

New Paste · Browse · Stats

Latest Pastes

Newguy HTML5 Engine source

function Character()
 {
	this.x = 640/2-32;
	this.y = 480/2-32;
	this.globalx = this.x/32;
	this.globaly = this.y/32;
	this.size = 32;
	
	this.framex = 32;
	this.framey = 0;
	this.frames = 3;
	this.cframe = 0;
	
	this.image = new Image();
	this.image.src = "./gfx/player.png";
	this.direction = 1;
	this.moving = false;
	this.charging = false;
	this.chargetime = 0;
	
	this.maxenergy = 25;
	this.energy = 25;
	this.health = 100;
	this.maxhealth = 100;
	
	this.healthregen = 3;
	this.energyregen = 1;
	
	this.power = 2;
	
	this.kills = 0;
	
	
 }
function Enemy(x,y,img)
{
	this.x = x;
	this.y = x;
	this.checkx = 0;
	this.checky = 0;
	this.direction = 0;
	this.image = img;
	
	this.health = 10;
	this.speed = .1;
	
	this.frames = 3;
	this.cframe = 0;
	this.framex = 0;
	this.framey = 0;
	
	
}
function Camera()
{
	this.x = 640/2-32;
	this.y = 480/2-32;
	this.cx = 0;
	this.cy = 0;
	this.speed = 3;
	this.canMoveUp = true;
	this.canMoveDown = true;
	this.canMoveLeft = true;
	this.canMoveRight = true;
}
function Object(x,y,type,image)
{
	this.x = x;
	this.y = y;
	this.size = 16;
	this.type = type;
	this.image = image;
	this.direction = 0;
	
	
	this.frames = 0
	this.cframe = 0;
	this.framex = 0;
	this.framey = 0;
	this.speed = 6;
	this.damage = 2;
}
function ImageLoader()
{
	this.grass = new Image();
    this.dirt = new Image();
	this.eball = new Image();
    this.lava = new Image();
	this.eblast = new Image();
	this.enemy1 = new Image();
	this.dirt.src = "./gfx/dirt.png";
	this.eball.src = "./gfx/energyBall.png";
	this.eblast.src = "./gfx/energyBlastSheet.png";
	this.lava.src = "./gfx/burningGrass.png";
	this.grass.src = "./gfx/gtile.png";
	this.enemy1.src = "./gfx/thief.png";
	
	

}
function drawGui()
{
	
	//back rectangle
	context.beginPath();
    context.rect(0, 20, 100, 20);
    context.fillStyle = "#000000";
    context.fill();
	//back health rectangle
	context.beginPath();
    context.rect(0, 0, 100, 20);
    context.fillStyle = "#000000";
    context.fill();
	//energy rectangle
	context.beginPath();
    context.rect(0, 20, 4*player.energy, 20);
    context.fillStyle = "#8ED6FF";
    context.fill();
	//health rectangle
	context.beginPath();
    context.rect(0, 0, player.health, 20);
    context.fillStyle = "red";
    context.fill();
	//energy text
	context.fillStyle = "white";
	context.fillText("Energy:" + player.energy + "/" + player.maxenergy,0,30);
	//health text
	context.fillStyle = "white";
	context.fillText("Health:" + player.health + "/" + player.maxhealth,0,10);
	//kills text
	context.fillText("Kills:" + player.kills,player.x,player.y);
}
function ObjectManager()
 {
	//Manages all map objects including projectiles
	this.objects = [];
	this.monsters = [];
	
	this.frameCheck = function ()
	{
		
		for (i = 0; i < this.objects.length;i++)
		{
			temp=this.objects[i];
			
			
			if (temp.type == 4)
			{
				if (temp.cframe == temp.frames) temp.cframe = 0;
				temp.framex = 32*temp.cframe;
				temp.cframe+=1;
			}
		}
		for (i = 0; i < this.monsters.length;i++)
		{
			var temp = this.monsters[i];
			if (temp.cframe == temp.frames) temp.cframe = 0;
				temp.framex = 32*temp.cframe;
				temp.cframe+=1;
		}
	
	}
	this.addMob = function (o)
	{
		this.monsters.push(o);
	
	}
	this.moveMobs = function ()
	{
		for (i = 0; i < this.monsters.length;i++)
		{	
			
			var temp = this.monsters[i];
			var dir = Math.floor(Math.random()*4);
			temp.direction=dir;
			// var maxx = 50*32;
			// var maxy = 50*32;
			// var minx = -0*32;
			// var miny = -0*32;
			// var checkx, checky;
			// checkx = temp.x;
			// checky = temp.y;
			// if (temp.direction == 0 && temp.y*32 > miny) checky-=temp.speed;
			// else if (temp.direction == 1 && temp.y*32 < maxy) checky+=temp.speed;
			// else if (temp.direction == 2 && temp.x*32 > minx) checkx-=temp.speed;
			// else if (temp.direction == 3 && temp.x*32 < maxx) checkx+=temp.speed;
			
			// for (l = 0; l < this.objects.length; l++)
			// {
				// o = this.objects[l];
				// if (checkMobCollison(o,temp) == false && o.type != 4) 
			// {
			// temp.x = checkx;
			// temp.y = checky;
			// }
				
			// }
			
			
			
			
			
		
		}
	
	}
	this.add = function (o)
	{
		if (o.type != 2 && o.type !=4)this.objects.push(o);
		if (o.type == 2)
		{
			o.direction = player.direction;
			o.damage = 2;
			this.objects.push(o);
		}
		if (o.type == 4)
		{
			o.direction = player.direction;
			o.damage = 10;
			o.frames = 2;
			o.speed = 8;
			o.size = 32;
			var temp = o;
			if (temp.direction == 0)
			{
		
				temp.framey = 32;
			}
			if (temp.direction == 1)
			{
		
				temp.framey = 96;
			}
			if (temp.direction == 2)
			{
		
				temp.framey = 0;
			}
			if (temp.direction == 3)
			{
		
				temp.framey = 64;
			}
			this.objects.push(o);
		}
	}
	this.draw = function ()
	{
		for (i = 0; i < this.objects.length;i++)
		{
			//alert(this.objects[i].image.src);
			temp = this.objects[i];
			if (this.objects[i].type == 1)context.drawImage(this.objects[i].image, this.objects[i].x*32-camera.x, this.objects[i].y*32-camera.y);
			if (this.objects[i].type == 2)context.drawImage(this.objects[i].image, this.objects[i].x-camera.x, this.objects[i].y-camera.y);
			if (this.objects[i].type == 3)context.drawImage(this.objects[i].image, this.objects[i].x*32-camera.x, this.objects[i].y*32-camera.y);
			if (this.objects[i].type == 4)context.drawImage(temp.image,temp.framex,temp.framey,32,32,temp.x-camera.x,temp.y-camera.y,32,32);
		}
		for (i = 0; i < this.monsters.length;i++)
		{
			temp = this.monsters[i];
			if (temp.direction == 0)
			{
		
				temp.framey = 96;
			}
			if (temp.direction == 1)
			{
		
				temp.framey = 0;
			}
			if (temp.direction == 2)
			{
		
				temp.framey = 32;
			}
			if (temp.direction == 3)
			{
		
				temp.framey = 64;
			}
			context.beginPath();
			context.rect(temp.x*32-camera.x, temp.y*32-camera.y-5, 3*temp.health, 5);
			context.fillStyle = "red";
			context.fill();
			context.drawImage(temp.image,temp.framex,temp.framey,32,32,temp.x*32-camera.x,temp.y*32-camera.y,32,32);
			
		}
	}
	this.updatePositions = function ()
	{
		for (i = 0; i < this.objects.length;i++)
		{
			
			var temp =this.objects[i];
			if (temp.type == 2 || temp.type == 4)
			{
				if (temp.direction == 0) temp.y-=temp.speed;
				if (temp.direction == 1) temp.y+=temp.speed;
				if (temp.direction == 2) temp.x-=temp.speed;
				if (temp.direction == 3) temp.x+=temp.speed;
				
			
				
			}
			
		}
		for (i = 0; i < this.monsters.length;i++)
		{
			var temp = this.monsters[i];
			var maxx = 50*32;
			var maxy = 50*32;
			var minx = -0*32;
			var miny = -0*32;
			var checkx, checky;
			checkx = temp.x;
			checky = temp.y;
			if (temp.direction == 0 && temp.y*32 > miny) checky-=temp.speed;
			else if (temp.direction == 1 && temp.y*32 < maxy) checky+=temp.speed;
			else if (temp.direction == 2 && temp.x*32 > minx) checkx-=temp.speed;
			else if (temp.direction == 3 && temp.x*32 < maxx) checkx+=temp.speed;
			
			for (l = 0; l < this.objects.length; l++)
			{
				o = this.objects[l];
				if (checkMobCollison(o,temp) == true && o.type == 1) 
				{
					checkx = temp.x;
					checky = temp.y
					
				}
				temp.x = checkx;
				temp.y = checky;
				
			}
			
			
			}
	}
	this.checkWorldCollisons = function()
	{
		for (i = 0; i < this.objects.length;i++)
		{
			for (l = 0; l < this.monsters.length;l++)
			{
				var mob = this.monsters[l];
				var o = this.objects[i];
				
				
				
					if (o.type == 2 || o.type == 4)
					{
						if (checkMobCollison(o,mob) == true)
						{
							mob.health-=o.damage;
							if (mob.health <= 0)
							{
							this.monsters.splice(l,1);
							player.kills+=1;
							}
							this.objects.splice(i,1);
							break;
						}
					
					}
					else 
					{
						if (checkMobCollison(o,mob) == true)
						{
							
							if (o.type == 3) mob.health-=o.damage;
							
							if (mob.health <= 0)
							{
								this.monsters.splice(l,1);
								break;
							}
							
						
						}
					
					}
					
					
				
				
			}
		
		}
		
	
	}
	this.checkCollisons = function (cx,cy)
	{
		for (i = 0; i < this.objects.length;i++)
		{
			var temp = this.objects[i];
			if(checkCollison(this.objects[i].x*32-cx,this.objects[i].y*32-cy) == true)
			{
				
				if (temp.type == 1)
				{
					
					return true;
				}
				if (temp.type == 3)
				{
					
					if (player.health > 0)player.health-=temp.damage;
					return false;
				}
				
			}			
		}
		return false;
	}
	
 }
 var w = 0;
 var a = 0;
 var s = 0;
 var d = 0;
 var map = new Array();
 var tiles =  new Array();
 var canvas = document.getElementById("myCanvas");
 var context = canvas.getContext("2d");
 var player = new Character();
 var camera = new Camera();
 var omanager = new ObjectManager();
 var iloader = new ImageLoader();
 omanager.add(new Object(20,15,1,iloader.dirt));
 omanager.add(new Object(21,15,1,iloader.dirt));
 omanager.add(new Object(22,15,1,iloader.dirt));
 omanager.add(new Object(23,15,1,iloader.dirt));
 omanager.add(new Object(20,16,1,iloader.dirt));
 omanager.add(new Object(21,16,1,iloader.dirt));
 omanager.add(new Object(22,16,1,iloader.dirt));
 omanager.add(new Object(23,16,1,iloader.dirt));
 omanager.add(new Object(10,11,3,iloader.lava));
 omanager.add(new Object(10,12,3,iloader.lava));
 omanager.add(new Object(10,13,3,iloader.lava));
 omanager.add(new Object(10,14,3,iloader.lava));
 generateMobs(7);
 
 
 player.x = 640/2-32;
 player.y = 480/2-32;
 
 
 var frame = 0;
 function generateMobs(number)
 {
	for (i = 0; i < number; i++)
	{
		omanager.addMob(new Enemy(Math.floor(Math.random()*40),Math.floor(Math.random()*40),iloader.enemy1));
	}
	
 
 }
 function checkMobCount()
 {
	var mcount = omanager.monsters.length;
	if (mcount <= 2) generateMobs(4);
 }
 function checkCollison(x,y)
 {
	
		
		//if (player.x == x && player.y == y) return true;
		if ((player.x <= x+32 && player.x +32 >= x) || player.x+32 >= x && player.x <= x+32 )
                     {
                         if (player.y <= y + 32 && player.y+32 >= y)
                         {
                             return true;
                         }
                     }

		return false;
	
 }
 function checkMobCollison(object,mob)
 {
	
		
		
		if (object.type == 2 || object.type == 4){
		if ((mob.x*32 <= object.x+object.size && mob.x*32 +32 >= object.x) || mob.x*32+32 >= object.x && mob.x*32 <= object.x+object.size )
                     {
                         if (mob.y*32 <= object.y + object.size && mob.y*32+32 >= object.y)
                         {
                             return true;
                         }
                     }

		return false;
		}
		// else
		// {
			// if ((Math.floor(mob.x) <= object.x+object.size && Math.floor(mob.x) +32 >= object.x) || Math.floor(mob.x)+32 >= object.x && Math.floor(mob.x) <= object.x+object.size )
                     // {
                         // if (Math.floor(mob.y) <= object.y + object.size && Math.floor(mob.y)+32 >= object.y)
                         // {
                             // return true;
                         // }
                     // }

		// return false;
		
		// }
		else if (Math.round(mob.x) == object.x && Math.round(mob.y) == object.y) return true;
		return false
	
 }
 function DrawPlayer()
 {
	
	if (player.direction == 0)
	{
		
		player.framey = 96;
	}
	if (player.direction == 1)
	{
		
		player.framey = 0;
	}
	if (player.direction == 2)
	{
		
		player.framey = 32;
	}
	if (player.direction == 3)
	{
		
		player.framey = 64;
	}
	if (player.moving == false) player.framex = 32;
	
	
	context.drawImage(player.image,player.framex,player.framey,player.size,player.size,player.x,player.y,player.size,player.size);
 }
 function ClearCanvas()
 {
	canvas.width = canvas.width;
 }
 function DrawBackground()
 {
	
	for (i = 0; i < 50; i++)
	{
		for (g = 0; g < 50; g++)
		{
			context.drawImage(iloader.grass, i*32-camera.x, g*32-camera.y);
			
		}
	}
	
 }
 function checkInput()
 {

   document.onkeydown=function(e){
	var e=window.event || e
	
	if (e.keyCode == 87) w = 1;
	else if (e.keyCode == 65) a = 1;
	else if (e.keyCode == 83) s = 1;
	else if (e.keyCode == 68) d = 1; 
	else if (e.keyCode == 32) {
	
	if (player.energy > 1) 
	{
	//omanager.add(new Object(camera.x+320-24,camera.y+240,2,iloader.eball));
	//omanager.add(new Object(camera.x+320-32,camera.y+240-32,2,iloader.eball));
	//player.energy-=2;
	player.charging = true;
	}
	}
	}
	document.onkeyup=function(e){
	var e=window.event || e
	if (e.keyCode == 87) w = 0;
	else if (e.keyCode == 65) a = 0;
	else if (e.keyCode == 83) s = 0;
	else if (e.keyCode == 68) d = 0;
	else if (e.keyCode == 32) {
		player.charging = false;
		
	    if (player.chargetime >= 1.5 && player.energy > 4) 
		{
			omanager.add(new Object(camera.x+320-32,camera.y+240-32,4,iloader.eblast));
			player.energy-=4;
		}
		else if (player.energy > 1) {
		omanager.add(new Object(camera.x+320-24,camera.y+240-24,2,iloader.eball));
		player.energy-=2;
		}
		player.chargetime = 0;
	}
}

}


 
 function handleInput()
 {
    var checkx = camera.x;
	var checky = camera.y;
	if(w == 1){
		if (camera.canMoveUp == true)checky-=camera.speed;
		player.direction = 0;
		player.moving = true;
	}
	if(s == 1)
	{	
		if (camera.canMoveDown == true)checky+=camera.speed;
		player.direction = 1;
		player.moving = true;
	}
	if (a == 1)
	{
		if (camera.canMoveLeft == true)checkx-=camera.speed;
		player.direction = 2;
		player.moving = true;
	}
	if (d == 1)
	{
		
		if (camera.canMoveRight == true)checkx+=camera.speed;
		player.direction = 3;
		player.moving = true;
	}
	
	if (omanager.checkCollisons(checkx,checky) == false) 
	{
		camera.x = checkx;
		camera.y = checky;
	}
 }
 function Update()
 {
	//Renders and Checks for input
	//map width/height*tilewidth/height-halfof screen height/width
	
	var screenymax = 50*32-240;
	var screenxmax = 50*32-320;
	var screenxmin = -288;
	var screenymin = -208;
	var dposx = 10*32-camera.x;
	var dposy = 10*32-camera.y;
	camera.canMoveUp = true;
	camera.canMoveDown = true;
	camera.canMoveLeft = true;
	camera.canMoveRight = true;
	//omanager.checkCollisons();
	
	omanager.updatePositions();
	omanager.checkWorldCollisons();
	checkMobCount();
	checkInput();
	handleInput();
	if (camera.x > screenxmax) camera.x = screenxmax;
	if (camera.x < screenxmin) camera.x = screenxmin;
	if (camera.y > screenymax) camera.y = screenymax;
	if (camera.y < screenymin) camera.y = screenymin;
	
	 //document.getElementById("keys").value = "W:" + w + " A:" + a + " S:" + s + " D:" + d;
	 //document.getElementById("energy").value = player.energy + "/" + player.maxenergy; 
	//document.getElementById("charpos").value = "X:" + player.x + " Y:"  + player.y;
	//document.getElementById("screenpos").value = "X:" + screenxmax + " Y:" + screenymax;
	//document.getElementById("screenpos").value = "X:" +chx +" Y:" + chy;
	DrawGame();
	player.moving = false;
 }
 function onSecond()
 {
	//Game Based events happen here. Operates every one second.
	
	if (player.energy < player.maxenergy) player.energy+=player.energyregen;
	if (player.health< player.maxhealth) player.health+=player.healthregen;
	if (player.health > player.maxhealth) player.health = player.maxhealth;
	if (player.energy > player.maxenergy) player.energy = player.maxenergy;
	omanager.moveMobs();
	
 }
 function FrameCheck()
 {
	if (player.charging == true) player.chargetime+=.4;
	if (player.cframe == player.frames) player.cframe = 0;
	player.framex = 32*player.cframe;
	player.cframe+=1;
	
	omanager.frameCheck();
	
	
 }
 function DrawGame()
 {
	//The render function, put anything and everything that need to be rendered here.
	ClearCanvas();
	DrawBackground();
	omanager.draw();
	drawGui();
	DrawPlayer();
 }