Last active
February 15, 2017 14:11
-
-
Save Taka414/886a85a986d09624811a0bc1ea38029f to your computer and use it in GitHub Desktop.
Cocos2d-x over 3.1x HPBar Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#pragma execution_character_set("utf-8") | |
namespace HpBar | |
{ | |
// バーの状態を表します。 | |
enum class BarState : int | |
{ | |
Unknwon = 0, //未設定 | |
Green, Yellow, Red, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma execution_character_set("utf-8") | |
#include "ui\UIScale9Sprite.h" | |
#include "HpBar.h" | |
#include "SpriteUtil.h" | |
using namespace cocos2d; | |
using namespace cocos2d::ui; | |
namespace aLife | |
{ | |
// | |
// Constants | |
// - - - - - - - - - - - - - - - - - - - - | |
// バーのオフセット(高さ) | |
const int bar_padding_height = 2.0; | |
// // バーのオフセット(幅) | |
const int bar_padding_width = 2.0; | |
// | |
// Constructors | |
// - - - - - - - - - - - - - - - - - - - - | |
HpBar::HpBar(float bar_width, float bar_height, int max, int current) | |
{ | |
// 指定された値は覚えておく | |
this->bar_height_ = bar_height; | |
this->bar_width_ = bar_width; | |
this->max_ = max; | |
this->current_value_ = current; | |
// ゲージのフレーム部分 | |
const Rect outer_rect(0, 0, 24, 24); | |
const Rect inner_rect(1, 1, 22, 22); | |
Scale9Sprite* frame = | |
Scale9Sprite::create("hp_frame.png", outer_rect, inner_rect); | |
SpriteUtil::setDisableAntiAlias(frame); | |
// Scale9はContentSizeで大きさが変更できるので拡大はしなくていい | |
// → むしろ拡大するとおかしいことになる。 | |
// Size frame_size = frame->getContentSize(); | |
// frame->setScale(bar_width / frame_size.width, bar_height / frame_size.height); | |
frame->setContentSize(Size(bar_width, bar_height)); | |
this->frame_ = frame; | |
this->frame_->retain(); | |
// バーのロード | |
this->initBarSprites(); | |
// バーを新規に設定 | |
this->setBarNew(current); | |
// 表示を更新 | |
this->setCurrent(current); | |
} | |
HpBar::~HpBar() | |
{ | |
this->bar_green_->removeFromParentAndCleanup(true); | |
CC_SAFE_RELEASE_NULL(this->bar_green_); | |
this->bar_yellow_->removeFromParentAndCleanup(true); | |
CC_SAFE_RELEASE_NULL(this->bar_yellow_); | |
this->bar_red_->removeFromParentAndCleanup(true); | |
CC_SAFE_RELEASE_NULL(this->bar_red_); | |
this->frame_->removeFromParentAndCleanup(true); | |
CC_SAFE_RELEASE_NULL(this->frame_); | |
} | |
void HpBar::setCurrent(int value) | |
{ | |
// 前回更新したときと同じなら更新を飛ばす | |
if (current_value_ == value) | |
{ | |
return; | |
} | |
// ゲージがマイナスは不許可 | |
this->current_value_ = value < 0 ? 0 : value; | |
// 新しい状態が異なる場合バーの種類を変更 | |
if (this->current_bar_state_ != this->getBarState(value)) | |
{ | |
this->bar_current_->removeFromParent(); | |
this->setBarNew(value); | |
} | |
// 管理値の最大値に対する現在値の比率を計算 | |
float ratio = (float)this->current_value_ / this->max_; | |
// 新しいゲージの横幅 | |
float newWidth = (this->bar_width_ - bar_padding_width) * ratio; | |
// Spriteへゲージの幅を設定 | |
SpriteUtil::changeScale(this->bar_current_, | |
newWidth, this->bar_height_ - bar_padding_height); | |
} | |
HpBar* HpBar::create(float bar_width, | |
float bar_height, int max, int current) | |
{ | |
HpBar *pRet = new(std::nothrow) HpBar(bar_width, bar_height, max, current); | |
if (pRet && pRet->init()) | |
{ | |
pRet->autorelease(); | |
return pRet; | |
} | |
else | |
{ | |
delete pRet; | |
pRet = nullptr; | |
return nullptr; | |
} | |
} | |
bool HpBar::init() | |
{ | |
return true; | |
} | |
void HpBar::initBarSprites() | |
{ | |
// 稼働するバーの部分 | |
Sprite* bar_green = Sprite::create("hp_bar_green.png"); | |
bar_green->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); | |
bar_green->setPosition(Vec2(1, 1)); | |
SpriteUtil::setDisableAntiAlias(bar_green); | |
this->bar_green_ = bar_green; | |
this->bar_green_->retain(); | |
Sprite* bar_yellow = Sprite::create("hp_bar_yellow.png"); | |
bar_yellow->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); | |
bar_yellow->setPosition(Vec2(1, 1)); | |
SpriteUtil::setDisableAntiAlias(bar_yellow); | |
this->bar_yellow_ = bar_yellow; | |
this->bar_yellow_->retain(); | |
Sprite* bar_red = Sprite::create("hp_bar_red.png"); | |
bar_red->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); | |
bar_red->setPosition(Vec2(1, 1)); | |
SpriteUtil::setDisableAntiAlias(bar_red); | |
this->bar_red_ = bar_red; | |
this->bar_red_->retain(); | |
} | |
void HpBar::setBarNew(int value) | |
{ | |
// バーがをどの状態にするか | |
BarState newState = this->getBarState(value); | |
// 現在のバーの状態を画像を設定 | |
this->bar_current_ = this->getBarSpriteByState(newState); | |
this->current_bar_state_ = newState; | |
// フレームに追加 | |
this->frame_->addChild(bar_current_); | |
} | |
BarState HpBar::getBarState(int val) | |
{ | |
// 割合を出す | |
float rate = ((float)val / this->max_) * 100; | |
if (rate < 25 /* 25%未満ならレッド */) | |
{ | |
return BarState::Red; | |
} | |
else if (rate < 50 /* 50%未満ならイエロー */) | |
{ | |
return BarState::Yellow; | |
} | |
return BarState::Green; | |
} | |
Sprite* HpBar::getBarSpriteByState(BarState state) | |
{ | |
switch (state) | |
{ | |
case aLife::BarState::Green: | |
return this->bar_green_; | |
case aLife::BarState::Yellow: | |
return this->bar_yellow_; | |
case aLife::BarState::Red: | |
return this->bar_red_; | |
default: | |
return nullptr; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#pragma execution_character_set("utf-8") | |
#include "cocos2d.h" | |
#include "ui\UIScale9Sprite.h" | |
#include "BarState.h" | |
namespace HpBar | |
{ | |
/// <summary> | |
/// HPゲージを表します。 | |
/// </summary> | |
class HpBar : public cocos2d::Ref | |
{ | |
// | |
// Constants | |
// - - - - - - - - - - - - - - - - - - - - | |
// バーのオフセット(高さ) | |
static const int bar_col_height; | |
// バーのオフセット(幅) | |
static const int bar_col_width; | |
// | |
// Fields | |
// - - - - - - - - - - - - - - - - - - - - | |
// バーの表示上の幅 | |
float bar_width_; | |
// バーの表示上の高さ | |
float bar_height_; | |
// ゲージの最大値 | |
int max_; | |
// ゲージの現在の状態(100分率) | |
int current_value_; | |
// バーのフレーム | |
cocos2d::ui::Scale9Sprite* frame_ = nullptr; | |
// 緑のバー | |
cocos2d::Sprite* bar_green_ = nullptr; | |
// 黄色のバー | |
cocos2d::Sprite* bar_yellow_ = nullptr; | |
// 赤のバー | |
cocos2d::Sprite* bar_red_ = nullptr; | |
// 現在表示中のバー | |
cocos2d::Sprite* bar_current_ = nullptr; | |
// 現在のバーの状態 | |
BarState current_bar_state_ = BarState::Unknwon; | |
// -x- 禁止操作 -x- | |
// バーサイズを指定してオブジェクトを初期化します。 | |
HpBar(float bar_width, float bar_height, int max, int current); | |
// -x- 禁止操作 -x- | |
public: | |
// | |
// Constructors | |
// - - - - - - - - - - - - - - - - - - - - | |
// 使用している資源を解放してオブジェクトを破棄します。 | |
~HpBar(); | |
// | |
// Props | |
// - - - - - - - - - - - - - - - - - - - - | |
// バーの幅を取得します。 | |
float getBarWidth() { return this->bar_width_; } | |
// バーの高さを取得します。 | |
float getBarHeight() { return this->bar_height_; } | |
// ゲージの最大値を取得します。 | |
int getMax() { return this->max_; } | |
// ゲージの最大値を設定します。 | |
void setMax(int max) { this->max_ = max; } | |
// ゲージの最大値を取得します。 | |
int getCurrent() { return this->current_value_; } | |
// ゲージの最大値を設定します。 | |
void setCurrent(int value); | |
// このオブジェクトが管理しているBarSpriteを取得します。 | |
cocos2d::Node* getBarSprite() { return this->frame_; } | |
// | |
// Methods | |
// - - - - - - - - - - - - - - - - - - - - | |
// CREATE_FUNCの実装 | |
static HpBar* HpBar::create(float bar_width, | |
float bar_height, int max, int current); | |
// CREATE_FUNCの実装 | |
bool HpBar::init(); | |
private: | |
// 初期化時の各種バーを読み込みます。 | |
void initBarSprites(); | |
// 指定した値でバーを新規作成します。 | |
void setBarNew(int val); | |
// 指定した値に対応するバーの状態を取得します。 | |
BarState getBarState(int val); | |
// 指定した状態に対応するバーのSpriteを取得します。 | |
cocos2d::Sprite* getBarSpriteByState(BarState state); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment