Skip to content

Commit

Permalink
Merge pull request #44 from soltycabbage/feature/exp
Browse files Browse the repository at this point in the history
経験値を獲得できるようにした
  • Loading branch information
nise-nabe committed Jan 7, 2014
2 parents a54a76f + f530522 commit 3beef46
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 13 deletions.
7 changes: 5 additions & 2 deletions app/beamQuest/activeEvent/mob.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ Mob.prototype.spawnMob_ = function(map) {
for(var i = map.mobCount;i < map.maxMobCount; i++) {
// TODO: mapごとに出現モンスターとか決める
var position = this.randomPosition_(map);
var mobType = bq.Params.Entities.KAMUTARO;
var mob = new mobModel({
id: 'mob_kamuraro_' + map.id + '_' + i + '_' + timeStamp,
name: 'カム太郎',
id: mobType.id + '_' + map.id + '_' + i + '_' + timeStamp,
name: mobType.name,
hp: mobType.hp,
exp: mobType.exp,
position: position
});
entitiesStore.addMob(map, mob);
Expand Down
6 changes: 6 additions & 0 deletions app/beamQuest/listener/beam.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ exports.listen = function(socket, io) {
var newHp = newEntity.hp + damage;
newEntity.hp = newHp;

// 攻撃を与えたユーザのIDをヘイトリストに突っ込む
// TODO: ヘイト値の導入
if (!_.contains(newEntity.hateList, data.shooterId)) {
newEntity.hateList.push(data.shooterId);
}

io.sockets.emit('notify:beam:hit', {
entity: newEntity,
beamTag: data.tag,
Expand Down
16 changes: 16 additions & 0 deletions app/beamQuest/listener/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Entity = function() {
Entity.prototype.listen = function(socket, io) {
this.socket_ = socket;
this.io_ = io;
this.entitiesStore_ = require('beamQuest/store/entities');
};

/**
Expand All @@ -28,6 +29,21 @@ Entity.prototype.popMob = function(mob) {
Entity.prototype.kill = function(entity) {
var data = {entity: entity.toJSON()};
this.io_.sockets.emit('notify:entity:kill', data);
_.each(entity.hateList, function(playerId) {
this.addExp(playerId, entity);
}.bind(this));
};

/**
* entityのもつ経験値をplayerに与える
* @param {string} playerId
* @param {model.Entity} entity
*/
Entity.prototype.addExp = function(playerId, entity) {
var mapId = entity.position.mapId;
var player = this.entitiesStore_.getPlayerById(mapId, playerId);
player.exp += entity.exp;
player.socket.emit('user:status:exp:update', {exp: entity.exp});
};


Expand Down
7 changes: 7 additions & 0 deletions app/beamQuest/model/mob.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ var Mob = function(opt_data) {
* @type {number}
*/
this.exp = this.data.exp || Mob.DEFAULT_EXP;

/**
* ヘイトリスト
* @type {Array.<string>}
*/
this.hateList = [];
};
util.inherits(Mob, Entity);

Expand All @@ -22,6 +28,7 @@ Mob.DEFAULT_EXP = 1;
Mob.prototype.toJSON = function() {
var json = Mob.super_.prototype.toJSON.apply(this);
json.exp = this.exp;
json.hateList = this.hateList;
return json;
};

Expand Down
8 changes: 8 additions & 0 deletions app/beamQuest/model/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ var Player = function(opt_data) {
/** @type {number} */
this.bp = this.data.bp || this.maxBp;

/** @type {number} */
this.exp = this.data.exp || 0;

/** @type {number} */
this.lv = this.data.lv || 1;

/** @type {Socket} */
this.socket = this.data.socket || null;
};
Expand All @@ -26,6 +32,8 @@ Player.prototype.toJSON = function() {
var json = Player.super_.prototype.toJSON.apply(this);
json.maxBp = this.maxBp;
json.bp = this.bp;
json.exp = this.exp;
json.lv = this.lv;
return json;
};

Expand Down
2 changes: 1 addition & 1 deletion app/beamQuest/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bq.Params = {

Entities: {
// Mobs
KAMUTARO: {hp: 100, atk: 10, exp: 10}
KAMUTARO: {id: 'mob_kamutaro', name: 'カム太郎', hp: 100, atk: 10, exp: 10}
}
};

Expand Down
20 changes: 16 additions & 4 deletions app/beamQuest/store/entities.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var mapStore = require('beamQuest/store/maps'),
entityListener = require('beamQuest/listener/entity');
var mapStore = require('beamQuest/store/maps');


/**
* ゲーム内のEntityの状態を保持しておくクラス
Expand Down Expand Up @@ -31,6 +31,7 @@ var Entities = function() {
*/
this.mapNpcs_ = {};

this.entityListener_ = require('beamQuest/listener/entity');
this.init_();
};

Expand Down Expand Up @@ -60,6 +61,17 @@ Entities.prototype.addPlayer = function(mapId, player) {
logger.info('player add [mapId=' + mapId + ',playerId=' + player.id + ',isAdd=' + isAdd + ']');
};

/**
* @param {number} mapId
* @param {string} playerId
* @return {model.Player}
*/
Entities.prototype.getPlayerById = function(mapId, playerId) {
if (this.mapPlayers_[mapId]) {
return this.mapPlayers_[mapId][playerId] || null;
}
};

/**
* @param {number} mapId
* @param {model.Player} player
Expand All @@ -85,7 +97,7 @@ Entities.prototype.addMob = function(map, mob) {
if (!_.contains(mobs, mob.id)) {
mobs[mob.id] = mob;
map.mobCount++;
entityListener.popMob(mob);
this.entityListener_.popMob(mob);
}
};

Expand Down Expand Up @@ -166,7 +178,7 @@ Entities.prototype.updateMobStatus = function(mapId, mob) {
if (target) {
target = mob;
if (target.hp < 0) { // 死
entityListener.kill(mob);
this.entityListener_.kill(mob);
this.removeMob(mapStore.getMapById(mapId), mob);
}
}
Expand Down
1 change: 1 addition & 0 deletions public/cocos2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'js/src/model/playerMove.js',
'js/src/model/beamPos.js',
'js/src/model/chat.js',
'js/src/model/mob.js',

'js/src/entityManager.js',
'js/src/socket/socket.js',
Expand Down
1 change: 1 addition & 0 deletions public/js/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var BQConfig = cc.Class.extend({
this.tags = {
BASE_LAYER: 0,
PLAYER: 100,
EXP_LABEL: 101,
CHAT: 500,
DEBUG_PING: 1000000
};
Expand Down
29 changes: 26 additions & 3 deletions public/js/src/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bq.entity.Entity = cc.Sprite.extend({
collideRect_: null, // 当たり判定の範囲
currentState:null,
currentDirection:null,
model_: null,

/**
* @param {string} spriteFrameName *.plistの<key>に設定されてるframeName
Expand All @@ -39,6 +40,20 @@ bq.entity.Entity = cc.Sprite.extend({
this.collideRect_ = cc.rect(0, 0, bbox.width, bbox.height);
},

/**
* @param {bq.model.Model}model
*/
setModel: function(model) {
this.model_ = model;
},

/**
* @return {bq.model.Model}
*/
getModel: function() {
return this.model_;
},

/**
* Entityの頭上にキャラ名を表示する
*/
Expand All @@ -51,10 +66,18 @@ bq.entity.Entity = cc.Sprite.extend({
},

/**
* @return {cc.rect}
* 獲得経験値をポーンって出す
* @param {number} exp
* @private
*/
getCollideRect: function() {
return this.collideRect_;
popExpLabel: function(exp) {
var label = bq.Label.createWithShadow(exp + 'exp', 18);
var pos = this.getPosition();
var fadeOut = cc.FadeOut.create(1);
var moveTo = cc.MoveTo.create(1, cc.p(0, 40));
label.runAction(cc.Spawn.create(fadeOut, moveTo));
label.setPosition(pos.x, pos.y);
bq.baseLayer.addChild(label, bq.config.tags.EXP_LABEL);
},

/**
Expand Down
8 changes: 5 additions & 3 deletions public/js/src/entityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ bq.EntityManager = cc.Class.extend({
* @param {Object} mob
*/
createMob: function(mob) {
var x = mob.position.x;
var y = mob.position.y;
var mobModel = new bq.model.Mob(mob);
var x = mobModel.position.x;
var y = mobModel.position.y;
var enemy_id = 1;
var enemy = new bq.entity.Enemy(enemy_id);
enemy.setModel(mobModel);
enemy.setPosition(cc.p(x, y));
bq.baseLayer.addChild(enemy, 50);
this.enemys_[mob.id] = enemy;
this.enemys_[mobModel.id] = enemy;
}
});

Expand Down
33 changes: 33 additions & 0 deletions public/js/src/model/mob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
bq.model.Mob = bq.model.extend({
ctor: function(json) {
/** @type {string} */
this.id;

/** @type {string} */
this.name;

/** @type {number} */
this.maxHp;

/** @type {number} */
this.hp;

/** @type {number} */
this.exp;

/** @type {cc.p} */
this.position;

this._super(json);
},

/** @override */
parse: function(json) {
this.id = json['id'];
this.name = json['name'];
this.maxHp = json['maxHp'];
this.hp = json['hp'];
this.exp = json['exp'];
this.position = cc.p(json['position'].x, json['position'].y);
}
});
13 changes: 13 additions & 0 deletions public/js/src/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ bq.Socket = cc.Class.extend({
initAfterLogin: function() {
var entityManager = bq.EntityManager.getInstance();

/**
* ブロードキャストされてきたやつ
*/
// チャット受信
this.socket.on('notify:message', function (data) {
var chatData = new bq.model.Chat(data);
Expand Down Expand Up @@ -49,6 +52,16 @@ bq.Socket = cc.Class.extend({
this.socket.on('notify:entity:mob:pop', function(data) {
entityManager.popMob(data);
});

/**
* 1対1の通信
*/
// 経験値貰ったよって
this.socket.on('user:status:exp:update', function(data) {
bq.player.popExpLabel(data.exp);
// TODO:システムメッセージに表示する。
});

},

/**
Expand Down

0 comments on commit 3beef46

Please sign in to comment.