Skip to content

target 作成

tkr987 edited this page Aug 17, 2018 · 5 revisions

基底クラスのH2NLIB::NyaTargetを継承してヘッダファイルを作成する。攻撃処理を実装するためにHNLIB::DevicePropertyX1型の変数をメンバに加える。

#pragma once
#include "NyaTarget.h"

namespace HNLIB
{
	class DevicePropertyX1;
	class GraphicPropertyX4;
	class EffectPropertyX1;
}

class TeemoDevice
{
public:
	H2NLIB::DevicePropertyX1* dpx_;
	H2NLIB::GraphicPropertyX4* gpx_;
	TeemoDevice();
	~TeemoDevice();
};

class TeemoTargetMain
{
public:
	TeemoDevice device_;
	H2NLIB::GraphicPropertyX4* gpx_;
	TeemoTargetMain();
	~TeemoTargetMain();
};

class TeemoTarget : public H2NLIB::NyaTarget 
{
public:
	void Act(void);
	void Draw(void);
	TeemoTarget();
private:
	unsigned int count_frame_;
	TeemoTargetMain main_;
};

NyaTargetクラスは2つのメンバ関数を持つ。
virtual void Act(void) = 0;
virtual void Draw(void) = 0;
Act関数とDraw関数の仕様はNyaUserと同じである。

攻撃処理をするにはNyaDevice::Attack14()を利用する。

NyaDevice::Attack14()のプロトタイプ:
void NyaDevice::Attack14(const DevicePropertyX1* const dpx, GraphicPropertyX4* const device_gpx, eOBJECT device_type, unsigned int collision_accuracy = 1)

  • dpx; デバイスのプロパティ
  • device_gpx; デバイスのグラフィックプロパティ
  • device_type デバイスのオブジェクトタイプ
  • collision_accuracy 衝突判定精度(デフォルトで1倍)

NyaDevice::Attack14()にはDevicePropertyX1とGraphicPropertyX4を引数に渡す必要がある。DevicePropertyX1クラスは様々なメンバを持っているが(詳細はNyaDeviceの項目を参照)、例えば座標(400, 200)から90度の角度で速度3の攻撃処理をするには4つの変数の値を設定すれば良い。また、攻撃処理を描画する最低限の設定項目はGraphicPropertyX4のメンバのうちfile_1つだけで良い。

座標(400, 200)から90度の角度で速度3の攻撃処理をするために設定する変数:

  • DevicePropertyX1::create_x_; 攻撃処理生成X座標
  • DevicePropertyX1::create_y_; 攻撃処理生成Y座標
  • DevicePropertyX1::move_angle_deg_; 角度
  • DevicePropertyX1::move_speed_; 速度

攻撃処理の画像を描画するために設定する変数:

  • GraphicPropetyX4::file_; ロードした画像ファイルを格納する変数

DevicePropertyX1とGraphicPropertyX4のメンバを設定後、NyaDevice::Attack14()の第一引数と第二引数に渡す。第三引数にはeOBJECTの値を渡して攻撃処理のオブジェクトタイプを指定する。Act関数は毎フレーム呼び出されるので、count_frame_を60で剰余を取れば60フレーム毎に攻撃処理をすることができる。

#include "HNLIB.h"
#include "TeemoTarget.h"
using namespace std;
using namespace HNLIB;

TeemoDevice::TeemoDevice()
{
	dpx_ = new DevicePropertyX1;
	dpx_->create_x_ = 400;
	dpx_->create_y_ = 200;
	dpx_->move_angle_deg_ = 90;
	dpx_->move_speed_ = 3;
	gpx_ = new GraphicPropertyX4;
	NyaGraphic::LoadGraphicFile("img/target/attack_red1.png", &gpx_->file_);
}

TeemoDevice::~TeemoDevice()
{
	delete dpx_;
	dpx_ = nullptr;
	delete gpx_;
	gpx_ = nullptr;
}

TeemoTargetMain::TeemoTargetMain()
{
	gpx_ = new GraphicPropertyX4;
	gpx_->draw_grid_cx_ = 400;
	gpx_->draw_grid_cy_ = 200;
	NyaGraphic::LoadGraphicFile("img/teemo_target.png", &gpx_->file_);
}

TeemoTargetMain::~TeemoTargetMain()
{
	delete gpx_;
	gpx_ = nullptr;
}

TeemoTarget::TeemoTarget()
{
	count_frame_ = 0;
}

void TeemoTarget::Act(void)
{	
	if (count_frame_ % FPS_MAX == 0)
		NyaDevice::Attack14(main_.device_.dpx_, main_.device_.gpx_,eOBJECT::TARGET_ATTACK1);

	count_frame_++;
}

void TeemoTarget::Draw(void)
{
	NyaGraphic::Draw(main_.gpx_, eOBJECT::TARGET1);
}

TeemoMission::Create()にtargetを追加する処理を記述する。targetの追加にはNyaMission::AddTarget()を使用する。

NyaMission::AddTarget()のプロトタイプ:
void NyaMission::AddTarget(int start_time_sec, int end_time_sec, NyaTarget* target);

  • start_time_sec; targetの処理開始時間(単位は秒)
  • end_time_sec; targetの処理終了時間(単位は秒)
  • target; 追加するtarget
#include "TeemoMission.h"
#include "TeemoTarget.h"
#include "TeemoUser.h"
using namespace HNLIB;

void TeemoMission::Create(void)
{
	AddTarget(1, 100, new TeemoTarget);
	AddUser(new TeemoUser);
}

void TeemoMission::Delete(void)
{
	ClearTarget();
	ClearUser();
}

コンパイルして実行する。
target_run

Clone this wiki locally