123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- function ExampleGame(connection) {
- var game = null;
- var system = null;
- var player = null;
- var otherPlayers = [];
- var keyboard = null;
- var camera = {x: 0, y: 0, speed: 5};
- this.init = function(systemObject, canvas) {
- game = this;
- system = systemObject;
- keyboard = new KeyboardInput();
- keyboard.attach();
- camera = {x: 0, y: 0, speed: 5};
- var defaultData = {
- position: {x: 60, y: 60},
- velocity: {x: 0, y: 0},
- moveToTarget: false,
- targetDestination: {x: 60, y: 60},
- targetAngle: 0,
- shipColor: "#DD0000"
- };
-
- player = new Player(camera, defaultData);
- };
- this.sendMessage = function(data) {
- connection.send(JSON.stringify(data, null, "\t"));
- }
- this.receiveMessage = function(data) {
- if(data.hasOwnProperty("player")) {
- player.sync(data.timestamp, data.player);
- }
- if(data.hasOwnProperty("other")) {
- if(otherPlayers[data.other.id] == null) {
- otherPlayers[data.other.id] = new Player(camera, data.other);
- } else {
- otherPlayers[data.other.id].sync(data.timestamp, data.other);
- }
- }
- if(data.hasOwnProperty("remove")) {
- delete otherPlayers[data.remove];
- }
- }
- this.updateDelta = function(canvas, delta){
- this.update(canvas);
- player.update(delta);
- for(var index in otherPlayers) {
- var other = otherPlayers[index];
- other.update(delta);
- }
- };
- this.update = function(canvas) {
- var time = new Date().getTime();
- if(keyboard.isDown(Keys.Up) || keyboard.isDown(Keys.W)) {
- camera.y += camera.speed;
- }
- if(keyboard.isDown(Keys.Down) || keyboard.isDown(Keys.S)) {
- camera.y -= camera.speed;
- }
- if(keyboard.isDown(Keys.Left)|| keyboard.isDown(Keys.A)) {
- camera.x += camera.speed;
- }
- if(keyboard.isDown(Keys.Right)|| keyboard.isDown(Keys.D)) {
- camera.x -= camera.speed;
- }
- if(keyboard.isPressed(Keys.Space)) {
- camera.x = -player.position.x + (canvas.width / 2);
- camera.y = -player.position.y + (canvas.height / 2);
- }
- };
- this.draw = function(context) {
- for(var index in otherPlayers) {
- var other = otherPlayers[index];
- other.draw(context);
- }
- player.draw(context);
- };
- this.mouseMove = function(canvas, x, y) {
- //calculate angle between player and cursor
- if(!player.moveToTarget) {
- var angleRadians = Math.atan2(y - (player.position.y + camera.y), x - (player.position.x + camera.x));
- player.targetAngle = angleRadians;
- }
- };
- this.mouseClick = function(canvas, button, x, y) {
- var time = new Date().getTime();
- switch(button) {
- case 0:
- var origin = {x: player.position.x, y: player.position.y};
- var target = {x: x - camera.x, y: y - camera.y};
- this.sendMessage({"action":"target", "start": time, "origin": origin, "position": target});
- player.moveToTarget = true;
- player.targetDestination = target;
- break;
- case 1:
-
- break;
- case 2:
-
- break;
- }
- };
- this.contextMenu = function(canvas, event) {
- };
- this.disconnect = function() {
- delete player;
- }
- }
|