mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
implemented mouse looking
This commit is contained in:
parent
88d70f2549
commit
5f7917c5cc
9 changed files with 117 additions and 19 deletions
38
app/Game/Client/Control/Input/MouseInput.js
Normal file
38
app/Game/Client/Control/Input/MouseInput.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
define([
|
||||
"Game/Client/Control/Input/XyInput",
|
||||
"Game/Client/View/DomController",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function (Parent, DomController, Settings) {
|
||||
|
||||
function MouseInput() {
|
||||
Parent.call(this);
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
MouseInput.prototype = Object.create(Parent.prototype);
|
||||
|
||||
MouseInput.prototype.init = function() {
|
||||
|
||||
var canvas = null;
|
||||
var self = this;
|
||||
canvas = DomController.getCanvas();
|
||||
|
||||
canvas.onmousemove = function(e){
|
||||
|
||||
// -1 +1 +1 +1 xy scaling should
|
||||
// -1 -1 +1 -1 be like this
|
||||
|
||||
var x = (((e.clientX - this.offsetLeft) / Settings.STAGE_WIDTH) * 2) - 1;
|
||||
var y = (((Settings.STAGE_HEIGHT - (e.clientY - this.offsetTop)) / Settings.STAGE_HEIGHT) * 2) -1;
|
||||
|
||||
self.onXyChange(x, y);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return MouseInput;
|
||||
|
||||
});
|
||||
22
app/Game/Client/Control/Input/XyInput.js
Normal file
22
app/Game/Client/Control/Input/XyInput.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
define([
|
||||
"Game/Core/NotificationCenter"
|
||||
],
|
||||
|
||||
function (NotificationCenter) {
|
||||
|
||||
function XyInput() {
|
||||
this.x = null;
|
||||
this.y = null
|
||||
}
|
||||
|
||||
XyInput.prototype.onXyChange = function(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
NotificationCenter.trigger('onXyChange', x, y);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return XyInput;
|
||||
|
||||
});
|
||||
|
|
@ -1,16 +1,20 @@
|
|||
define([
|
||||
"Game/Core/Control/PlayerController",
|
||||
"Game/Client/Control/KeyboardInput",
|
||||
"Game/Client/Control/Input/MouseInput",
|
||||
"Game/Core/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, KeyboardInput, NotificationCenter) {
|
||||
function (Parent, KeyboardInput, MouseInput, NotificationCenter) {
|
||||
|
||||
function PlayerController (me) {
|
||||
|
||||
Parent.call(this, me);
|
||||
|
||||
this.keyboardInput = new KeyboardInput(this);
|
||||
this.xyInput = new MouseInput(this);
|
||||
|
||||
NotificationCenter.on("onXyChange", this.setXY, this);
|
||||
|
||||
var keys = {
|
||||
w:87,
|
||||
|
|
@ -21,7 +25,7 @@ function (Parent, KeyboardInput, NotificationCenter) {
|
|||
up: 38,
|
||||
left: 37,
|
||||
down: 40,
|
||||
right: 39
|
||||
right: 39
|
||||
}
|
||||
|
||||
this.init(keys);
|
||||
|
|
@ -61,10 +65,17 @@ function (Parent, KeyboardInput, NotificationCenter) {
|
|||
NotificationCenter.trigger('sendGameCommand', 'jump');
|
||||
}
|
||||
|
||||
PlayerController.prototype.setXY = function(x, y) {
|
||||
Parent.prototype.lookAt.call(this, x, y);
|
||||
NotificationCenter.trigger('sendGameCommand', 'lookAt', x, y); // implement that x and y are received
|
||||
};
|
||||
|
||||
PlayerController.prototype.update = function () {
|
||||
this.keyboardInput.update();
|
||||
Parent.prototype.update.call(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return PlayerController;
|
||||
});
|
||||
|
|
@ -13,10 +13,12 @@ define([
|
|||
function (Parent, Box2D, PhysicsEngine, ViewController, PlayerController, NotificationCenter, requestAnimFrame, Settings, Stats) {
|
||||
|
||||
function GameController () {
|
||||
this.viewController = new ViewController();
|
||||
|
||||
|
||||
Parent.call(this, new PhysicsEngine());
|
||||
|
||||
this.viewController = new ViewController();
|
||||
|
||||
this.physicsEngine.setCollisionDetector();
|
||||
|
||||
this.me = null;
|
||||
|
|
|
|||
|
|
@ -54,9 +54,11 @@ define(requires, function (DomController, Three, Settings, CameraController) {
|
|||
this.scene.add(this.cameraController.getCamera());
|
||||
|
||||
|
||||
var ambientLight = new Three.AmbientLight(0xffffff);
|
||||
var ambientLight = new Three.AmbientLight(0xffaaaa);
|
||||
this.scene.add(ambientLight);
|
||||
|
||||
|
||||
|
||||
//var directionalLight = new Three.DirectionalLight(0xffffff);
|
||||
//directionalLight.position.set(1, 0, 10).normalize();
|
||||
//this.scene.add(directionalLight);
|
||||
|
|
@ -105,13 +107,19 @@ define(requires, function (DomController, Three, Settings, CameraController) {
|
|||
ViewController.prototype.render = function () {
|
||||
|
||||
if(this.me) {
|
||||
var pos = this.me.getDoll().getBody().GetPosition();
|
||||
this.cameraController.setPosition(pos.x * Settings.RATIO, -(pos.y * Settings.RATIO));
|
||||
var pos = this.me.getPosition();
|
||||
var x = pos.x * Settings.RATIO;
|
||||
var y = -(pos.y * Settings.RATIO);
|
||||
|
||||
x += this.me.playerController.xyInput.x * Settings.STAGE_WIDTH / 4;
|
||||
y += this.me.playerController.xyInput.y * Settings.STAGE_HEIGHT / 4;
|
||||
|
||||
this.cameraController.setPosition(x, y);
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.movableObjects.length; i++) {
|
||||
var obj = this.movableObjects[i];
|
||||
var pos = obj.player.getDoll().getBody().GetPosition();
|
||||
var pos = obj.player.getPosition();
|
||||
obj.mesh.position.x = pos.x * Settings.RATIO;
|
||||
obj.mesh.position.y = -pos.y * Settings.RATIO + 21;
|
||||
}
|
||||
|
|
@ -141,7 +149,7 @@ define(requires, function (DomController, Three, Settings, CameraController) {
|
|||
ViewController.prototype.addPlayer = function(player) {
|
||||
var self = this;
|
||||
var mesh = null;
|
||||
var pos = player.getDoll().getBody().GetPosition();
|
||||
var pos = player.getPosition();
|
||||
var size = {w:10, h:42};
|
||||
var callback = function(mesh) {
|
||||
self.scene.add(mesh);
|
||||
|
|
|
|||
|
|
@ -16,12 +16,12 @@ define({
|
|||
GRAPHICS_SUBPATH_CHARACTERS: 'characters/',
|
||||
|
||||
RATIO: 35,
|
||||
TILE_SIZE: 15,
|
||||
TILE_SIZE: 40, //15
|
||||
CAMERA_IS_ORTHOGRAPHIC: true,
|
||||
|
||||
// GAME PLAY
|
||||
WALK_SPEED: 2.5,
|
||||
RUN_SPEED: 4.0,
|
||||
RUN_SPEED: 4,
|
||||
FLY_SPEED: 3.2,
|
||||
JUMP_SPEED: 4.0,
|
||||
JUMP_UPLIFT: 0.05,
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ define(function () {
|
|||
this.player.jump();
|
||||
}
|
||||
|
||||
PlayerController.prototype.lookAt = function (x, y) {
|
||||
this.player.lookAt(x, y);
|
||||
}
|
||||
|
||||
PlayerController.prototype.update = function () {
|
||||
//console.log(this._walkingDirectionStatus)
|
||||
if(this._walkingDirectionStatus != 0) {
|
||||
|
|
|
|||
|
|
@ -7,15 +7,16 @@ function (Doll, Settings) {
|
|||
|
||||
function Player (id, physicsEngine) {
|
||||
this.physicsEngine = physicsEngine;
|
||||
this.playerController = null;
|
||||
|
||||
this.id = id;
|
||||
this.standing = false;
|
||||
this.doll;
|
||||
this.mc;
|
||||
this.currentAnimationState = 'stand';
|
||||
this.lookDirection = 1;
|
||||
this.moveDirection = 0;
|
||||
this.lookDirection = 0;
|
||||
this.isSpawned = false;
|
||||
this.playerController = null;
|
||||
|
||||
}
|
||||
|
||||
Player.prototype.spawn = function (x, y) {
|
||||
|
|
@ -113,17 +114,20 @@ function (Doll, Settings) {
|
|||
}
|
||||
|
||||
Player.prototype.calculateWalkAnimation = function () {
|
||||
if (this.moveDirection == this.lookDirection) {
|
||||
// FIXME dont know if this is right
|
||||
if (this.moveDirection == (this.lookDirection > 0)) {
|
||||
return 'run';
|
||||
}
|
||||
return 'walkback';
|
||||
}
|
||||
|
||||
Player.prototype.look = function (x, y) {
|
||||
/*
|
||||
var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
|
||||
Player.prototype.lookAt = function (x, y) {
|
||||
//console.log("player looking at ", x, y);
|
||||
|
||||
|
||||
var lastLookDirection = this.lookDirection;
|
||||
|
||||
/*
|
||||
var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
|
||||
if (x < Settings.STAGE_WIDTH / 2) {
|
||||
this.mc.scaleX = -1;
|
||||
this.lookDirection = -1;
|
||||
|
|
@ -135,10 +139,17 @@ function (Doll, Settings) {
|
|||
degree = (45 + -degree / 2) - 90;
|
||||
this.mc.head.rotation = degree;
|
||||
}
|
||||
*/
|
||||
|
||||
if(x < 0) {
|
||||
this.lookDirection = -1;
|
||||
} else {
|
||||
this.lookDirection = 1;
|
||||
}
|
||||
|
||||
if (this.lookDirection != lastLookDirection && this.isWalking()) {
|
||||
this.animate(this.calculateWalkAnimation());
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
Player.prototype.isWalking = function () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue