bullets
This commit is contained in:
		
							parent
							
								
									2601346af3
								
							
						
					
					
						commit
						623b687a52
					
				
					 5 changed files with 109 additions and 27 deletions
				
			
		
							
								
								
									
										19
									
								
								js/class.js
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								js/class.js
									
									
									
									
									
								
							|  | @ -2,6 +2,7 @@ let mapWidth = 210.; | ||||||
| let mapHeith = 100.; | let mapHeith = 100.; | ||||||
| const playerSize = 50.; | const playerSize = 50.; | ||||||
| const playerSpeed=.2; | const playerSpeed=.2; | ||||||
|  | const bulletSpeed=playerSpeed*2; | ||||||
| const halfSqrtTwo=0.70710678118; | const halfSqrtTwo=0.70710678118; | ||||||
| class Player | class Player | ||||||
| { | { | ||||||
|  | @ -118,12 +119,12 @@ class Bullet | ||||||
|         this.deleted=false; |         this.deleted=false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     update() |     update(dt) | ||||||
|     { |     { | ||||||
|         if(!this.deleted) |         if(!this.deleted) | ||||||
|         { |         { | ||||||
|             this.x+=this.dx; |             this.x+=this.dx*dt*bulletSpeed; | ||||||
|             this.y+=this.dy; |             this.y+=this.dy*dt*bulletSpeed; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -131,30 +132,28 @@ class Bullet | ||||||
|     { |     { | ||||||
|         if(!this.deleted) |         if(!this.deleted) | ||||||
|         { |         { | ||||||
|             if(dist(player,this)<playerSize/2) |             if(player!=null && dist(player,this)<playerSize/2) | ||||||
|             { |             { | ||||||
|                 player.takeDamage(1); |                 player.takeDamage(1); | ||||||
|                 this.deleted=true; |                 this.deleted=true; | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             for(let square in squares) |             squares.forEach((square) => { | ||||||
|             { |  | ||||||
|                 if(square.collide(this)) |                 if(square.collide(this)) | ||||||
|                 { |                 { | ||||||
|                     this.deleted=true; |                     this.deleted=true; | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|             } |             }); | ||||||
| 
 | 
 | ||||||
|             for(let circle in circles) |             circles.forEach((circle) => { | ||||||
|             { |  | ||||||
|                 if(circle.collide(this)) |                 if(circle.collide(this)) | ||||||
|                 { |                 { | ||||||
|                     this.deleted=true; |                     this.deleted=true; | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|             } |             }); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										46
									
								
								js/game.js
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								js/game.js
									
									
									
									
									
								
							|  | @ -16,9 +16,11 @@ let Net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo(), Renderer) | ||||||
| 
 | 
 | ||||||
| let playerId = null; //id of client player
 | let playerId = null; //id of client player
 | ||||||
| let players = []; | let players = []; | ||||||
|  | let bullets = []; | ||||||
|  | let selfBullets = []; | ||||||
| let player = null; | let player = null; | ||||||
| 
 | 
 | ||||||
| Inp = new Input("canvas", Net); | Inp = new Input("canvas", Net,Renderer); | ||||||
| 
 | 
 | ||||||
| playerId=Net.playerId; | playerId=Net.playerId; | ||||||
| player=Net.clientPlayer; | player=Net.clientPlayer; | ||||||
|  | @ -52,10 +54,10 @@ function addPlayers() | ||||||
| 	let playersToAdd = Net.getPlayersToAdd(); | 	let playersToAdd = Net.getPlayersToAdd(); | ||||||
| 	if (playersToAdd.length==0) | 	if (playersToAdd.length==0) | ||||||
| 		return; | 		return; | ||||||
| 	 | 	console.log(playersToAdd); | ||||||
| 	playersToAdd.forEach((p) => { | 	playersToAdd.forEach((p) => { | ||||||
| 		console.log("New player: ",playersToAdd[0].id); | 		console.log("New player: ",p.id); | ||||||
| 		players.push(playersToAdd[0]); | 		players.push(p); | ||||||
| 		Renderer.AddPlayer(p); | 		Renderer.AddPlayer(p); | ||||||
| 	}); | 	}); | ||||||
| } | } | ||||||
|  | @ -81,6 +83,32 @@ function remPlayers() | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function addBullets() | ||||||
|  | { | ||||||
|  | 	let bulletsToAdd = Net.getBulletsToAdd(); | ||||||
|  | 	if (bulletsToAdd.length==0) | ||||||
|  | 		return; | ||||||
|  | 	bulletsToAdd.forEach((b) => { | ||||||
|  | 		console.log("new bullet"); | ||||||
|  | 		bullets.push(p); | ||||||
|  | 		Renderer.addBullet(p); | ||||||
|  | 	}); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function updateBullets(list,target,dt) | ||||||
|  | { | ||||||
|  | 	for(let i = list.length-1;i>=0;i--) | ||||||
|  | 	{ | ||||||
|  | 		list[i].update(dt); | ||||||
|  | 		list[i].checkCollisions(target,squares,circles); | ||||||
|  | 		if(list[i].deleted) | ||||||
|  | 		{ | ||||||
|  | 			list.splice(i,1); | ||||||
|  | 			Renderer.remBullet(list[i]); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| let currentTime = new Date(); | let currentTime = new Date(); | ||||||
| function game() { | function game() { | ||||||
| 	if(playerId==null) | 	if(playerId==null) | ||||||
|  | @ -93,6 +121,7 @@ function game() { | ||||||
| 			//Inp.player=player; //pour connecter les input au joueur client
 | 			//Inp.player=player; //pour connecter les input au joueur client
 | ||||||
| 			console.log("Connected as id ",playerId); | 			console.log("Connected as id ",playerId); | ||||||
| 			Inp.player=player; | 			Inp.player=player; | ||||||
|  | 			Inp.bullets=selfBullets; | ||||||
| 			Renderer.AddPlayer(player) | 			Renderer.AddPlayer(player) | ||||||
| 			players.forEach((p) => { | 			players.forEach((p) => { | ||||||
| 				Renderer.AddPlayer(p) | 				Renderer.AddPlayer(p) | ||||||
|  | @ -104,19 +133,22 @@ function game() { | ||||||
| 		update(); | 		update(); | ||||||
| 		addPlayers(); | 		addPlayers(); | ||||||
| 		remPlayers(); | 		remPlayers(); | ||||||
|  | 		addBullets(); | ||||||
| 		player.update(squares, circles, dt); | 		player.update(squares, circles, dt); | ||||||
| 		for (var i = players.length - 1; i >= 0; i--) { | 		for (var i = players.length - 1; i >= 0; i--) { | ||||||
| 			players[i].update(squares, circles, dt);//squares, circles
 | 			players[i].update(squares, circles, dt); | ||||||
| 		} | 		} | ||||||
| 		Renderer.ReloadAff(); | 		Renderer.ReloadAff(); | ||||||
|  | 		updateBullets(selfBullets,null,dt); | ||||||
|  | 		updateBullets(bullets,player,dt); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	let newTime = new Date(); | 	let newTime = new Date(); | ||||||
| 	dt=newTime - currentTime; | 	dt=newTime - currentTime; | ||||||
| 	currentTime=newTime | 	currentTime=newTime; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Net.connect(); //connect to server, create a player, and retrieve all players info
 | Net.connect(); //connect to server, create a player, and retrieve all players info
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| setInterval(game) | setInterval(game); | ||||||
							
								
								
									
										19
									
								
								js/input.js
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								js/input.js
									
									
									
									
									
								
							|  | @ -1,9 +1,11 @@ | ||||||
| class Input { | class Input { | ||||||
|     constructor(id, net) { |     constructor(id, net,renderer) { | ||||||
|         this.keysDown = new Set() |         this.keysDown = new Set() | ||||||
|         this.dir = 0; |         this.dir = 0; | ||||||
|         this.player=null; |         this.player=null; | ||||||
|  |         this.bullets=null; | ||||||
|         this.canvas = document.getElementById(id); |         this.canvas = document.getElementById(id); | ||||||
|  |         this.renderer=renderer; | ||||||
| 
 | 
 | ||||||
|         this.mouseX = 0 |         this.mouseX = 0 | ||||||
|         this.mouseY = 0 |         this.mouseY = 0 | ||||||
|  | @ -11,10 +13,19 @@ class Input { | ||||||
|         this.network = net |         this.network = net | ||||||
| 
 | 
 | ||||||
|         // Event listener pour la position de la souris
 |         // Event listener pour la position de la souris
 | ||||||
|         this.canvas.addEventListener("mousemove", this.handleMouseMove.bind(this)) |          | ||||||
|  |         this.canvas.addEventListener("mousemove", this.handleMouseMove.bind(this)); | ||||||
| 
 | 
 | ||||||
|         this.canvas.addEventListener("click", function(event) { |         this.canvas.addEventListener("click", (e) => { | ||||||
|             console.log("Clic de la souris"); |             if(this.player==null || this.bullets==null) | ||||||
|  |                 return; | ||||||
|  |             let dx = this.mouseX-this.player.x; | ||||||
|  |             let dy = this.mouseY-this.player.y; | ||||||
|  |             let norm = Math.sqrt(dx*dx+dy*dy); | ||||||
|  |             let b = new Bullet(this.player.x,this.player.x,dx/norm,dy/norm); | ||||||
|  |             this.bullets.push(b); | ||||||
|  |             this.renderer.addBullet(b); | ||||||
|  |             this.network.newBullet(this.player.x,this.player.y,dx/norm,dy/norm); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         /*window.addEventListener("keydown", function(event) { |         /*window.addEventListener("keydown", function(event) { | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ class Network{ | ||||||
| 		this.playersToAdd = []; | 		this.playersToAdd = []; | ||||||
| 		this.playersToRemove = []; | 		this.playersToRemove = []; | ||||||
| 		this.playersToUpdate = []; | 		this.playersToUpdate = []; | ||||||
|  | 		this.bulletsToAdd = []; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	message(data){ | 	message(data){ | ||||||
|  | @ -33,6 +34,9 @@ class Network{ | ||||||
| 			case "removePlayer": | 			case "removePlayer": | ||||||
| 				this.playersToRemove.push(data.data.id); | 				this.playersToRemove.push(data.data.id); | ||||||
| 				break; | 				break; | ||||||
|  | 			case "newBullet": | ||||||
|  | 				this.bulletsToAdd.push(new Bullet(data.x,data.y,data.dx,data.dy)); | ||||||
|  | 				break; | ||||||
| 			default: | 			default: | ||||||
| 				break; | 				break; | ||||||
| 		} | 		} | ||||||
|  | @ -52,7 +56,12 @@ class Network{ | ||||||
| 		this.socket.send(JSON.stringify({ | 		this.socket.send(JSON.stringify({ | ||||||
| 			type: "update", | 			type: "update", | ||||||
| 			data: obj | 			data: obj | ||||||
| 		})) | 		})); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	newBullet(x_,y_,dx_,dy_) | ||||||
|  | 	{ | ||||||
|  | 		this.socket.send(JSON.stringify({type:"newBullet",x: x_,y: y_,dx: dx_,dy: dy_})); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	getPlayersToAdd(){  //returns the list of new players
 | 	getPlayersToAdd(){  //returns the list of new players
 | ||||||
|  | @ -61,6 +70,12 @@ class Network{ | ||||||
| 		return tmp; | 		return tmp; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	getBulletsToAdd(){  //returns the list of new players
 | ||||||
|  | 		let tmp = this.bulletsToAdd; | ||||||
|  | 		this.bulletsToAdd = []; | ||||||
|  | 		return tmp; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	getPlayersToRemove(){ //returns the list of player who have left the game
 | 	getPlayersToRemove(){ //returns the list of player who have left the game
 | ||||||
| 		let tmp = this.playersToRemove; | 		let tmp = this.playersToRemove; | ||||||
| 		this.playersToRemove = []; | 		this.playersToRemove = []; | ||||||
|  |  | ||||||
							
								
								
									
										35
									
								
								js/render.js
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								js/render.js
									
									
									
									
									
								
							|  | @ -1,13 +1,15 @@ | ||||||
| const img = new Image(); | const imgPlayer = new Image(); | ||||||
| img.src = "./assets/body.png"; | const imgBullet = new Image(); | ||||||
| 
 | imgPlayer.src = "./assets/body.png"; | ||||||
|  | imgBullet.src = "./assets/body.png"; | ||||||
| class Render { | class Render { | ||||||
| 
 | 
 | ||||||
| 	constructor(id, mapsrc) { | 	constructor(id, mapsrc) { | ||||||
| 		this.canvas = document.getElementById(id); | 		this.canvas = document.getElementById(id); | ||||||
| 		this.ctx = canvas.getContext("2d"); | 		this.ctx = canvas.getContext("2d"); | ||||||
| 		this.players = []; | 		this.players = []; | ||||||
| 		this.mapsrc=mapsrc | 		this.bullets = []; | ||||||
|  | 		this.mapsrc=mapsrc; | ||||||
| 		this.ReloadAff(); | 		this.ReloadAff(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -15,6 +17,16 @@ class Render { | ||||||
| 		this.players.push(player); | 		this.players.push(player); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	addBullet(bullet) { | ||||||
|  | 		this.bullets.push(bullet); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	remBullet(bullet) { | ||||||
|  | 		for(let i=0;i<this.bullets.length;i++) | ||||||
|  | 			if(bullet==this.bullets[i]) | ||||||
|  | 				this.bullets.splice(i,1); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	RemPlayer(id) { | 	RemPlayer(id) { | ||||||
| 		for(let i=0;i<this.players.length;i++) | 		for(let i=0;i<this.players.length;i++) | ||||||
| 		{ | 		{ | ||||||
|  | @ -30,7 +42,17 @@ class Render { | ||||||
| 	    this.ctx.save(); | 	    this.ctx.save(); | ||||||
| 	    this.ctx.translate(player.x, player.y); | 	    this.ctx.translate(player.x, player.y); | ||||||
| 	    this.ctx.rotate(player.angle); | 	    this.ctx.rotate(player.angle); | ||||||
| 	    this.ctx.drawImage(img, -playerSize / 2, -playerSize / 2, playerSize, playerSize); | 	    this.ctx.drawImage(imgPlayer, -playerSize / 2, -playerSize / 2, playerSize, playerSize); | ||||||
|  | 	    this.ctx.restore(); | ||||||
|  |         //this.ctx.fillStyle="#FF0000";
 | ||||||
|  |         //this.ctx.font="10pt arial";
 | ||||||
|  |         //this.ctx.fillText(player.name,player.x-player.name.length*10/3,player.y-playerSize/1.8);
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	RenderBullet(bullet) { | ||||||
|  | 	    this.ctx.save(); | ||||||
|  | 	    this.ctx.translate(bullet.x, bullet.y); | ||||||
|  | 	    this.ctx.drawImage(imgBullet, -playerSize / 2, -playerSize / 2, playerSize, playerSize); | ||||||
| 	    this.ctx.restore(); | 	    this.ctx.restore(); | ||||||
|         this.ctx.fillStyle = 'white'; |         this.ctx.fillStyle = 'white'; | ||||||
|         this.ctx.font="10pt arial"; |         this.ctx.font="10pt arial"; | ||||||
|  | @ -51,5 +73,8 @@ class Render { | ||||||
|    		this.players.forEach((player) => { |    		this.players.forEach((player) => { | ||||||
|    			this.RenderPlayer(player); |    			this.RenderPlayer(player); | ||||||
|    		}) |    		}) | ||||||
|  | 		this.bullets.forEach((bullet) => { | ||||||
|  | 			this.RenderBullet(bullet); | ||||||
|  | 		}); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
		Loading…
	
		Reference in a new issue