Skip to content

Commit

Permalink
Merge pull request #25 from soltycabbage/feature/camera
Browse files Browse the repository at this point in the history
カメラ実装
  • Loading branch information
karihei committed Jan 1, 2014
2 parents 5c5f441 + 8019e20 commit 256cd3f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
1 change: 1 addition & 0 deletions public/cocos2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'js/src/entity/enemy.js',
'js/src/entity/beam.js',
'js/src/ping.js',
'js/src/camera.js',

// scene
'js/src/scene/scene.js',
Expand Down
21 changes: 21 additions & 0 deletions public/js/src/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
bq.Camera = cc.Class.extend({
ctor: function(follower) {
this.follower_ = follower;
},

lookAt: function(target) {
var follow = cc.Follow.create(target);
this.follower_.runAction(follow);
},

/**
* window座標系をゲーム内座標系に変換して返す
* @param {cc.p} src
* @return {cc.p}
*/
convertWindowPositionToWorldPosition: function(src) {
var basePosition = this.follower_.getPosition();
return cc.p(src.x - basePosition.x, src.y - basePosition.y);
},
});

1 change: 1 addition & 0 deletions public/js/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var BQConfig = cc.Class.extend({
// Spriteのタグ(IDみたいなの)
this.tags = {
BASE_LAYER: 0,
PLAYER: 100,
CHAT: 500,
DEBUG_PING: 1000000
};
Expand Down
27 changes: 7 additions & 20 deletions public/js/src/entity/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bq.entity.Player = bq.entity.Entity.extend({
*/
sendPosition: function() {
if (!bq.baseLayer) {return;}
var absolPos = this.convertAbsolutePosition(this.getPosition());
var absolPos = this.getPosition();
var posData = {
userId: this.name,
mapId: 1, // TODO: MapID の実装
Expand All @@ -53,19 +53,6 @@ bq.entity.Player = bq.entity.Entity.extend({
this.socket.sendPlayerPosition(posData);
},

/**
* 引数に与えられた座標をbaseLayerから見た座標に変換して返す
* @param {cc.p} src
* @return {Object}
*/
convertAbsolutePosition: function(src) {
var baseLayerP = bq.baseLayer.getPosition();
return {
x: src.x - baseLayerP.x,
y: src.y - baseLayerP.y
};
},

/**
* 向きと状態を更新してそれにもとづいてアニメーションを更新する
* @param {bq.entity.EntityState.Direction} dir 向き (nullなら更新しない)
Expand Down Expand Up @@ -144,8 +131,8 @@ bq.entity.Player = bq.entity.Entity.extend({
* @private
*/
shootInternal_: function(beam, destination) {
var src = this.convertAbsolutePosition(this.getPosition());
var dest = this.convertAbsolutePosition(destination);
var src = this.getPosition();
var dest = bq.camera.convertWindowPositionToWorldPosition(destination);

var json = { // TODO モデル化したい気持ち
userId: this.name,
Expand Down Expand Up @@ -190,19 +177,19 @@ bq.entity.Player.InputHandler = cc.Class.extend({
switch (key) {
// 重複多いのでリファクタリングした結果wwwww
case cc.KEY.a:
startWalking(this.player_.moveSpeed, this.dy);
startWalking(-this.player_.moveSpeed, this.dy);
break;

case cc.KEY.s:
startWalking(this.dx, this.player_.moveSpeed);
startWalking(this.dx, -this.player_.moveSpeed);
break;

case cc.KEY.d:
startWalking(-this.player_.moveSpeed, this.dy);
startWalking(this.player_.moveSpeed, this.dy);
break;

case cc.KEY.w:
startWalking(this.dx, -this.player_.moveSpeed);
startWalking(this.dx, this.player_.moveSpeed);
break;

default:
Expand Down
14 changes: 7 additions & 7 deletions public/js/src/scene/beamQuest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ bq.scene.BeamQuestWorld = cc.Layer.extend({
baseLayer.setPosition(cc.p(0,0));
this.addChild(baseLayer, 1, bq.config.tags.BASE_LAYER);

var playerLayer = cc.Layer.create();
playerLayer.setPosition(cc.p(0,0));
this.addChild(playerLayer, 100, bq.config.tags.BASE_LAYER);

bq.Beam.setup(bq.player.beamId[0], baseLayer);

bq.player.setPosition(cc.p(size.width / 2, size.height / 2));
playerLayer.addChild(bq.player, 0);
baseLayer.addChild(bq.player, 100, bq.config.tags.PLAYER);

this.camera = new bq.Camera(baseLayer);
this.camera.lookAt(bq.player);
bq.camera = this.camera;

var tileMap = new cc.TMXTiledMap();
tileMap.initWithTMXFile(bq.config.maps.area.SHINJUKU);
Expand Down Expand Up @@ -56,11 +56,11 @@ bq.scene.BeamQuestWorld = cc.Layer.extend({
update: function() {
'use strict';

var baseP = bq.baseLayer.getPosition();
var baseP = bq.player.getPosition();
var dx = this.playerHandler.dx;
var dy = this.playerHandler.dy;
if (dx !== 0 || dy !== 0) {
bq.baseLayer.setPosition(cc.p(baseP.x + dx, baseP.y + dy));
bq.player.setPosition(cc.p(baseP.x + dx, baseP.y + dy));
}
},

Expand Down

0 comments on commit 256cd3f

Please sign in to comment.