123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- function Player(game, camera) {
- this.id = 0;
- this.position = {x: 0, y: 0};
- this.targetPosition = {x: 0, y: 0};
- this.velocity = {x: 0.0, y: 0.0};
- this.acceleration = {x: 0, y: 0, speed: 0.08};
- this.width = 32;
- this.height = 32;
- this.maxSpeed = 2;
- this.turnRate = Math.PI / 64;
- this.targetAngle = 0;
- this.angleRadians = 0;
- this.color = "crimson";
- this.name = "Player";
- this.spriteCanvas = null;
- this.spriteContext = null;
- this.isAccelerating = false;
- this.sprite = "human1";
- this.isDead = false;
- this.health = 5;
- this.maxHealth = 5;
- this.seen = [];
- this.waitHere = false;
- this.mousePosition = {x: 0, y: 0};
- this.init = function() {
- this.color = "crimson";
- this.respawn();
- var joinAction = {action: "join",
- data: {
- //name: this.name,
- position: this.position,
- targetPosition: this.targetPosition
- }
- };
- system.theater.socketSend(joinAction);
- }
- this.update = function(delta) {
- if(this.isDead) {
- return;
- }
- if(this.health <= 0) {
- this.die();
- return;
- }
- this.angleRadians = system.angleTo(this.position, this.targetPosition);
- if(this.waitHere) {
- return;
- }
-
- if(this.targetPosition.x != this.position.x) {
- if(Math.abs(this.position.x - this.targetPosition.x) < this.maxSpeed) {
- this.position.x = this.targetPosition.x;
- this.targetAngle = this.angleRadians;
- } else {
- this.targetAngle = this.angleRadians;
- this.position.x += this.maxSpeed * Math.cos(this.angleRadians) * delta;
- }
- }
- if(this.targetPosition.y != this.position.y) {
- if(Math.abs(this.position.y - this.targetPosition.y) < this.maxSpeed) {
- this.position.y = this.targetPosition.y;
- this.targetAngle = this.angleRadians;
- } else {
- this.targetAngle = this.angleRadians;
- this.position.y += (this.maxSpeed) * Math.sin(this.angleRadians) * delta;
- }
- }
- }
- this.control = function(input) {
- /*if(system.keyboard.isDown(Keys.Up) || system.keyboard.isDown(Keys.W)) {
- this.walkUp();
- }
- if(system.keyboard.isDown(Keys.Down) || system.keyboard.isDown(Keys.S)) {
- this.walkDown();
- }
- if(system.keyboard.isDown(Keys.Left)|| system.keyboard.isDown(Keys.A)) {
- this.walkLeft();
- }
- if(system.keyboard.isDown(Keys.Right)|| system.keyboard.isDown(Keys.D)) {
- this.walkRight();
- }
- if(system.keyboard.isPressed(Keys.Space)) {
- this.jump();
- }*/
- }
- this.respawn = function() {
- this.position.x = parseInt(200 * Math.random() - 100);
- this.position.y = parseInt(200 * Math.random() - 100);
- this.targetPosition.x = this.position.x;
- this.targetPosition.y = this.position.y;
- }
- this.die = function() {
- this.health = 0;
- if(!this.isDead) {
- var player = this;
- setTimeout(function() {player.respawn();}, 3000);
- this.isDead = true;
- system.theater.socketSend({action: "die", playerid:this.id, data:{}});
- }
- }
- this.syncData = function() {
- system.theater.socketSend({action: "sync",
- playerid: this.id,
- data: this
- });
- this.spriteCanvas = document.createElement("canvas");
- this.spriteCanvas.width = this.width;
- this.spriteCanvas.height = this.height;
- this.spriteContext = this.spriteCanvas.getContext("2d");
- this.preRender(this.spriteContext);
- }
- this.walkUp = function() {
- this.targetPosition.y = this.position.y - this.maxSpeed;
- }
- this.walkDown = function() {
- this.targetPosition.y = this.position.y + this.maxSpeed;
- }
- this.walkLeft = function() {
- this.targetPosition.x = this.position.x - this.maxSpeed;
- }
- this.walkRight = function() {
- this.targetPosition.x = this.position.x + this.maxSpeed;
- }
- this.walkTowards = function(newPosition) {
- this.targetPosition.x = parseInt(newPosition.x);
- this.targetPosition.y = parseInt(newPosition.y);
- var moveAction = {action: "move", playerid:this.id, data:{position: this.position, targetPosition: this.targetPosition}};
- system.theater.socketSend(moveAction);
- }
- this.jump = function() {
- console.log("jump");
- }
- this.preRender = function(context, angle) {
- if(angle == null) {
- angle = 0;
- }
- context.fillStyle = this.color;
- context.strokeStyle = this.color;
- context.lineWidth = 4;
- context.save();
- context.translate(0.5, 0.5);
- //context.rotate(angle);
- /*context.fillStyle = "rgba(0,0,0,0.1)";
- system.ellipse(context, 0, 8, 16, 4);
- context.fill();*/
- context.drawImage(system.assets.getSprite(this.sprite), 0, 0, 16, 16, 0, 0, this.width, this.height);
- /*context.beginPath();
- context.moveTo(this.width / 2, 0);
- context.lineTo(-(this.width / 2), -(this.height / 3));
- context.lineTo(-(this.width / 2), this.height / 3);
- context.lineTo(this.width / 2, 0);
- context.fill();*/
- context.restore();
- }
- this.draw = function(context) {
- if(this.spriteContext == null) {
- this.spriteCanvas = document.createElement("canvas");
- this.spriteCanvas.width = this.width;
- this.spriteCanvas.height = this.height;
- this.spriteContext = this.spriteCanvas.getContext("2d");
- this.preRender(this.spriteContext);
- }
-
- if(this.isDead) {
- return;
- }
- context.save();
- context.translate(this.position.x - (this.width / 2), this.position.y - (this.height / 2));
- //context.rotate(this.targetAngle);
- if(this.isAccelerating) {
- context.fillStyle = "#7777FF";
-
- context.beginPath();
- context.moveTo(-this.width - 5, 0);
- context.lineTo(-this.width / 2, 3);
- context.lineTo(-this.width / 2, -3);
- context.lineTo(-this.width - 5, 0);
- context.fill();
- }
- context.drawImage(this.spriteCanvas, 0, 0);
- //bounds box
- /*context.strokeStyle = "#FF7733";
- context.beginPath();
- context.moveTo(0, 0);
- context.lineTo(this.width, 0);
- context.lineTo(this.width, this.height);
- context.lineTo(0, this.height);
- context.lineTo(0, 0);
- context.stroke();*/
- context.strokeStyle = "#00FF00";
- if(this.isTakingDamage) {
- context.strokeStyle = "#FF0000";
- this.isTakingDamage = false;
- }
- context.restore();
- context.save();
- context.translate(this.position.x, this.position.y);
- context.font = "16px sans-serif";
- context.textAlign = "center";
- context.textBaseline = "middle";
- context.fillStyle = "#DDDDDD";
- context.strokeStyle = "#182208";
- context.lineWidth = 2;
- var textMetrics = context.measureText(this.name);
- var textWidth = textMetrics.width;
- context.strokeText(this.name, 0, -this.height);
- context.fillText(this.name, 0, -this.height);
- context.restore();
- }
- this.trigger = function(eventId) {
- if(this.seen.indexOf(eventId) == -1) {
- this.seen.push(eventId);
- return true;
- }
- return false;
- }
- this.inConversation = function() {
- this.waitHere = true;
- this.targetPosition.x = this.position.x;
- this.targetPosition.y = this.position.y;
- }
- this.endConversation = function() {
- this.waitHere = false;
- }
- this.mouseMove = function(camera, x, y) {
- this.mousePosition.x = x - camera.position.x;
- this.mousePosition.y = y - camera.position.y;
- this.targetAngle = system.angleTo(this.position, this.mousePosition);
- }
- this.mouseDown = function(camera, button, x, y) {
- this.mouseMove(camera, x, y);
- if(button == 2) {
- //TODO: determine if an item or mobile is clicked on
- this.walkTowards(this.mousePosition);
- }
- }
- this.mouseUp = function(camera, button, x, y) {
- };
- this.touchStart = function(camera, x, y) {
- this.touchMove(camera, x, y);
- //TODO: determine if an item or mobile is clicked on
- this.walkTowards(this.mousePosition);
- };
- this.touchMove = function(camera, x, y) {
- this.mousePosition.x = x - camera.position.x;
- this.mousePosition.y = y - camera.position.y;
- this.targetAngle = system.angleTo(this.position, this.mousePosition);
- };
- this.touchEnd = function(camera, x, y) {
- };
- this.contextMenu = function() {
- };
- };
|