123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- require('MouseInput');
- require('Camera');
- require('HumanController');
- require('Player');
- require('HeadsUpDisplay');
- require('Sheep');
- require('SheepController');
- class World {
- constructor() {
- this.game = null;
- this.system = null;
- this.player = null;
- this.mouse = null;
- this.camera = null;
- this.dialog = null;
- this.ui = null;
- this.curtain = null;
- this.isStarted = false;
- this.sheeps = [];
- }
- init(systemObject, canvas) {
- this.game = this;
- this.canvas = canvas;
- this.canvas.style.cursor = "auto";
- this.system = systemObject;
- this.mouse = new MouseInput();
- this.mouse.attach(canvas, this);
- this.camera = new Camera();
- this.camera.init(canvas);
- this.player = new Player(this.game, new HumanController());
- this.player.id = 1;
- this.player.init(systemObject);
- this.sheeps = [];
- for(let i = 0; i < 4; i++) {
- let sheep = new Sheep(this.game, new SheepController());
- sheep.init(systemObject);
- this.sheeps.push(sheep);
- }
- this.ui = new HeadsUpDisplay(this.game, this.camera);
- this.ui.init(this.player, canvas);
- var stage = this;
- this.curtain = new Curtain();
- this.curtain.init(systemObject, canvas);
- this.curtain.open(700, function () {
- stage.isStarted = true;
- });
- }
- end() {
- this.mouse.detach();
- }
- updateDelta(delta, canvas) {
- if (this.system.keyboard.isDown(Keys.Escape)) {
- this.curtain.close(1000, function () {
- this.system.theater.changeStage("mainmenu");
- });
- }
- this.update(canvas);
- this.player.update(delta);
- for(let index in this.sheeps) {
- this.sheeps[index].update(delta);
- }
- this.camera.update(canvas, this.player, delta);
- this.ui.update(delta);
- this.curtain.updateDelta(delta);
- }
- update(canvas) {
- this.player.control();
- for(let index in this.sheeps) {
- this.sheeps[index].control(this.player);
- }
- }
- draw(context) {
- context.save();
- context.translate(this.camera.position.x, this.camera.position.y);
- for(let index in this.sheeps) {
- this.sheeps[index].draw(context);
- }
- this.player.draw(context);
- context.restore();
- this.ui.draw(context);
- this.curtain.draw(context);
- }
- handleResize(canvas) {
- this.camera.handleResize(canvas);
- }
- mouseMove(canvas, x, y) {
- this.player.controller.mouseMove(this.camera, x, y);
- }
- mouseDown(canvas, button, x, y) {
- this.player.controller.mouseDown(this.camera, button, x, y);
- }
- mouseUp(canvas, button, x, y) {
- this.player.controller.mouseUp(this.camera, button, x, y);
- }
- touchStart(canvas, x, y) {
- this.player.controller.touchStart(this.camera, x, y);
- }
- touchMove(canvas, x, y) {
- this.player.controller.touchMove(this.camera, x, y);
- }
- touchEnd(canvas, x, y) {
- this.player.controller.touchEnd(this.camera, x, y);
- }
- contextMenu(canvas, event) {
- this.player.controller.contextMenu(this.camera, event);
- }
- };
|