===== 純粋仮想関数で、ベース作ってエネミークラスを作るテスト =====
まずはベースクラス。純粋仮想関数を含んでいます。\\
=== ベースクラス cGameChar.h cGameChar.cpp ===
**試験に出る仮想関数**\\
* 純粋仮想関数ってなんだっけ?\\
* 純粋仮想関数を1つでも含むクラスを何と呼びますか?
* また、その特徴はどんなでしたか?
#pragma once
//キャラクターのベースになるクラス
class cGameChar
{
public:
Vec2 position_; //位置(位置ベクトル)
Vec2 direction_; //向き(単位ベクトル)
double rot_;//回転角 (radian)
cGameChar(Vec2 _pos);
virtual void Update(); //位置情報や、キャラの状態の更新
virtual void Draw(); //画面への描画処理
};
#include "stdafx.h"
#include "cGameChar.h"
cGameChar::cGameChar(Vec2 _pos)
:position_(_pos)
{
}
void cGameChar::Update()
{
//何もしない
}
void cGameChar::Draw()
{
//何もしない
}
=== エネミークラス cEnemy.h cEnemy.cpp ===
class cEnemy :
public cGameChar
{
Mat2x2 afn_; //変換用行列
double speed_; //移動速度
double radius_; //キャラの大きさ
//各ベクトルと回転角からそのフレームの変換行列を求める
void SetAffineMatrixRadians(double _angle_rad);
void SetAffineMatrixDegrees(double _angle_deg);
//void SetMirrorTransformMatrix();
public:
cEnemy(Vec2 _pos)
:cGameChar(_pos)
{
radius_ = 20.0;
direction_ = Vec2(1, 0);
position_ = Vec2(100, 100);
speed_ = 2.0; //とりあえずスピード2.0 pix/frameで動くことにする
//rot_ = 0;
afn_ = Mat2x2::Identity();
}
void Update() override;
void Draw() override;
};
#include "stdafx.h"
#include "cEnemy.h"
void cEnemy::SetAffineMatrixRadians(double _angle_rad)
{
afn_ = Mat2x2::Identity();
afn_._11 = static_cast(-Math::Cos(_angle_rad));
afn_._22 = static_cast(Math::Sin(_angle_rad));
}
void cEnemy::SetAffineMatrixDegrees(double _angle_deg)
{
afn_ = Mat2x2::Identity();
afn_._11 = static_cast(-Math::Cos(Math::ToRadians(_angle_deg)));
afn_._22 = static_cast(Math::Sin(Math::ToRadians(_angle_deg)));
}
//void cEnemy::SetMirrorTransformMatrix()
//{
// afn_ = Mat2x2::Identity();
// afn_._11 = -1;
//}
void cEnemy::Update()
{
Mat2x2 invMat(-1.0f, 0.0f, 0.0f, 1.0f); //y軸反転のアフィン変換行列
//往復運動させよう!
if (position_.x < radius_ || position_.x > Scene::Size().x - radius_)
Mat2x2::multiMatAndMat(afn_, afn_, invMat);
direction_ = afn_.transformPoint(direction_); //キャラの方向を更新
position_ = position_ + speed_ * direction_; //キャラの方向にspeed_分進める
}
void cEnemy::Draw()
{ //輪郭付きの円を書いてキャラを表す
Circle(position_.x, position_.y, radius_)
.drawFrame(1.0, Palette::Black)
.draw(Palette::Yellow);
//キャラの向いている方向に矢印を書くよ
Vec2 endPoint = position_ + direction_ * radius_;
Line{ position_, endPoint }
.drawArrow(3, Vec2{ 15, 10 }, Palette::Red);
}