====== ソースファイル一覧 ====== ===== cPoint.h ===== #pragma once class cPoint { float m_x, m_y; public: cPoint(); cPoint(float _x, float _y); //引数なしと、引数付きのコンストラクタ void setXY(float _x, float _y); float getX(); float getY(); //自分の座標を表示する関数 void printPoint(); //cPoint同士の距離を測って返す関数 float calcDistanceTo(cPoint &_p); //cPoint getPointXY(); }; ===== cPoint.cpp ===== #include #include #include "cPoint.h" using namespace std; cPoint::cPoint() { m_x = 0.0; m_y = 0.0; } cPoint::cPoint(float _x, float _y) { m_x = _x; m_y = _y; } void cPoint::setXY(float _x, float _y) { m_x = _x; m_y = _y; } float cPoint::getX() { return(m_x); } float cPoint::getY() { return(m_y); } void cPoint::printPoint() { cout << "(" << m_x << ", " << m_y << ")" << endl; } float cPoint::calcDistanceTo(cPoint& _p) { //pow(N,x) Nのx乗を計算する関数 return(sqrt(pow(_p.getX() - this->m_x, 2.0) + pow(_p.getY() - this->m_y, 2.0))); } //cPoint cPoint::getPointXY() //{ // return(*this); //} ===== cMychara.h ===== #pragma once #include "cPoint.h" #include class cMychara { //キャラの座標 cPoint m_position; std::string m_name; public: //m_position:(0,0) //m_name:"" //で初期化する cMychara(); //名前と座標で初期化する //コンストラクタとかいいよねぇ cMychara(float _x, float _y, std::string _name); //名前と座標(cPoint型)で初期化する //コンストラクタとかいいよねぇ cMychara(const cPoint &_p, std::string _name); //名前をセット void setName(std::string _name); //座標をセット void setPosition(float _x, float _y); //キャラの名前:座標を表示する //例 名前:(x座標, y座標) 改行 //キャラが5左に動く void moveLeft(); //キャラが5右に動く void moveRight(); //cPoint型の現在座標を返すゲッター //後ろのconstは、私はメンバ変数の変更は //しませんという宣言 cPoint getPoint() const; //引数についたconstは関数内で引数が変更されない //ことを保証する float calcDistanceTo(const cMychara &_target); void printPosition(); }; ===== cMychara.cpp ===== #include "cMychara.h" #include cMychara::cMychara() { m_position.setXY(0, 0); m_name = ""; } cMychara::cMychara(float _x, float _y, std::string _name) { m_position.setXY(_x, _y); m_name = _name; } cMychara::cMychara(const cPoint& _p, std::string _name) { m_position = _p; m_name = _name; } void cMychara::printPosition() { std::cout << m_name << " : "; m_position.printPoint(); std::cout << std::endl; } void cMychara::setName(std::string _name) { m_name = _name; } void cMychara::setPosition(float _x, float _y) { m_position.setXY(_x, _y); } void cMychara::moveLeft() { float tmpx = m_position.getX(); float tmpy = m_position.getY(); tmpx = tmpx - 5; m_position.setXY(tmpx, tmpy); } void cMychara::moveRight() { float tmpx = m_position.getX(); float tmpy = m_position.getY(); tmpx = tmpx + 5; m_position.setXY(tmpx, tmpy); } cPoint cMychara::getPoint() const { //m_positionは現在の座標 return(m_position); } float cMychara::calcDistanceTo(const cMychara& _target) { cPoint p1 = _target.getPoint(); cPoint p2 = this->getPoint(); float d = p1.calcDistanceTo(p2); return(d); } ===== theMain.cpp ===== #include #include "cpoint.h" #include "cMychara.h" using namespace std; int main() { cMychara hero(cPoint(0, 0), "hero"); //座標を取得して表示 cPoint p1 = hero.getPoint(); p1.printPoint(); //hero.printPosition(); //hero.moveRight(); //hero.printPosition(); cMychara gob(cPoint(0,10), "Gob"); //gob君の座標をセット gob.setPosition(20, 30); //gob.printPosition(); cPoint p2 = gob.getPoint(); p2.printPoint(); float distance = hero.calcDistanceTo(gob); cout << "distance: " << distance << endl; gob.moveLeft(); distance = hero.calcDistanceTo(gob); cout << "distance: " << distance << endl; //課題:当たり判定を作ってみよう //void Mychara::printCollision(const Mychar &_target); //を作ってみよう。 //_targetとの距離を測り、 // 32以内に入ったら: ”衝突” //     それ以外:”距離は保たれている” //と表示する関数 //以下の関数を作ると便利かな? //bool Mychara::detectCollision(const Mychar &_target); //キャラ同士が距離32以内に入ってるかどうかを //true,falseで返す関数 //float distance = sqrt( // (p2.getX() - p1.getX()) * (p2.getX() - p1.getX()) // + (p2.getY() - p1.getY()) * (p2.getY() - p1.getY())); //cout << "distance:" << hero.calcDistanceTo(gob); return 0; } //cPoint p1;//引数なしで初期化 //cPoint p2(3, 8);//引数付きで初期化. //p1.setXY(15, 0); //cout << "p1:"; //p1.printPoint(); //cout << "p2:"; //p2.printPoint(); // ////cout << "p1:(" << p1.getX() << ", " //// << p1.getY() << ")" << endl; ////cout << "p2:(" << p2.getX() << ", " //// << p2.getY() << ")" << endl;