Показать сообщение отдельно
  #21 (permalink)  
Старый 11.10.2017, 22:26
Интересующийся
Отправить личное сообщение для malinovsky Посмотреть профиль Найти все сообщения от malinovsky
 
Регистрация: 07.09.2017
Сообщений: 14

Rise,

this.delay -= game.delta; // NaN - а почему?


пока сделал так:

class Game
{
	constructor(canvas) {
		this.canvas = canvas;
		this.ctx = canvas.getContext('2d');
		this.world = new Set();
		this._last = 0;
		this.count = 0;
		this.world.add(new Player(this.canvas.width/2-30, this.canvas.height-30, 30, 20, "rgb(173, 105, 82)", 200));
		this.world.add(new Attack(50));
		this.world.add(new Line(0, 0, 600, 5, "rgb(36, 177, 219)"));
		this.world.add(new Line(0, 395, 600, 5, "rgb(36, 177, 219)"));
		this.lastObjShot = false;
		this._step = (now) => {
			this._loop = requestAnimationFrame(this._step);
			this.delta = Math.min(now - this._last, 100) / 1000;
			this._last = now;
			this.update();
			this.render();
		};
		this._step();
	}

	update() {
		for (let entity of this.world) if (entity.update) entity.update(this);
	}

	render() {
		this.ctx.fillStyle = "rgb(36, 177, 219)";
		this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
		for (let entity of this.world) if (entity.render) entity.render(this);
	}

	collide(entity1, type) {
		for (let entity2 of this.world) {
			if (entity1 != entity2  && 
				entity2.type & type && 
				entity1.x < entity2.x + entity2.w &&
				entity1.x + entity1.w > entity2.x &&
				entity1.y < entity2.y + entity2.h &&
				entity1.h + entity1.y > entity2.y) return true;
		}
		return false;
	}

	stop()
	{
		if (this._loop) this._loop = cancelAnimationFrame(this._loop);
	}
}

class Rect
{
	constructor() {
	}

	render(game) {
		game.ctx.fillStyle = this.color;
		game.ctx.fillRect(this.x, this.y, this.w, this.h);
	}
}

const PLAYER = 1, SHIP = 2, SHOT = 4, LINE = 8;

class Player extends Rect
{
	constructor(px, py, pw, ph, c, v) {
		super();
		Object.assign(this, { type: PLAYER, rate: 0.4, delay: 2, x : px, y : py, w : pw, h : ph, color : c, vel : v });
	}

	update(game) {

		if(keyEvent.left) {
			this.x -= this.vel * game.delta;
		}
		if(keyEvent.right) {
			this.x += this.vel * game.delta;
		}
		this.delay -= 0.05;
		if (keyEvent.space && this.delay < 0) {
			this.delay = this.rate;
			game.world.add(new Shot(this.x+11, this.y-7, 7, 7, 'red', 100));
		}
		if (game.collide(this, PLAYER | SHIP | LINE )) game.stop();
	}
}

class Ship extends Rect
{
	constructor(px, py, pw, ph, c, v) {
		super();
		Object.assign(this, { type: SHIP, x : px, y : py, w : pw, h : ph, color : c, vel : v });
	}

	update(game) {
		this.y += this.vel * game.delta;
		if (game.collide(this, PLAYER | SHOT | LINE )) game.world.delete(this);
	}
}

class Shot extends Rect
{
	constructor(px, py, pw, ph, c, v) {
		super();
		Object.assign(this, { type: SHOT, x : px, y : py, w : pw, h : ph, color : c, vel : v });
	}

	update(game) {
		this.y -= this.vel * game.delta;
		if (game.collide(this, SHIP | SHOT | LINE )) game.world.delete(this);
	}
}

class Line extends Rect
{
	constructor(px, py, pw, ph, c, v) {
		super();
		Object.assign(this, { type: LINE, x : px, y : py, w : pw, h : ph, color : c, vel : v  });
	}
}

class Attack {
	constructor(s) {
		Object.assign(this, { size: s, rate: 0.5, delay: 0 });
	}
	update(game) {
		this.delay -= 0.05;
		if (this.delay < 0) {
			this.delay = this.rate;
			game.world.add(new Ship(Math.random() * 590, 5, 10, 10, 'green', 100));
			if (!--this.size) game.world.delete(this);
		}
	}
}

const keyEvent = {
	left : false,
	right : false,
	space : false
};

window.onkeydown = function(e) {
  switch(e.keyCode) {
  	case 37 : keyEvent.left = true; break;
  	case 39 : keyEvent.right = true; break;
  	case 32 : keyEvent.space = true; break;
  }
};

window.onkeyup = function(e) {
  switch(e.keyCode) {
  	case 37 : keyEvent.left = false; break;
  	case 39 : keyEvent.right = false; break;
  	case 32 : keyEvent.space = false; break;
  }
};

const game = new Game(document.getElementById('canvas'));


и у меня там кажется бардак в конструкторах
Ответить с цитированием