Map.cppのコメント部のここですのコード部分なのですが落下時の座標を修正するコードですが左右に動いてる時も値が修正されてしまいブロックの上に上がってしまいます。これをどうしたら上から落下した時だけの落下時の時だけに修正できるのでしょうか?
y座標だけ見て前の座標を違う場合のみ実行したいのですが上手く来ません
※マリオ風アクションゲーム
Map.cpp
#include "Map.h" #include <fstream> #include "Position.h" #include "Block.h" #include <optional> #define CELL 64 #define MAP_WIDTH 20 #define MAP_HEIGHT 11 static std::ofstream ofs("Log.txt"); enum class mapChip { eBrick = 5, eQuestion = 0, eNo = -1, }; int Map::map_set[MAP_HEIGHT][MAP_WIDTH] = {//番号管理 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,0,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,0-11,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1}, }; /*マップセルのオブジェクト数を返す関数*/ int Map::getCount_mapCell(int n) { return -1; } /*コンストラクタ*/ Map::Map() { /*画像設定*/ for (int y = 0; y < 11; y++) { for (int x = 0; x < 20; x++) { if (get_mapCell(x,y) == (int)mapChip::eQuestion) { //画像ファイル名、座標、画像の横枚数、縦枚数、最初の画像 bmap_set.push_back(Block("mario_resource/Block_64px.png",Position(x * 64,y * 64),10,4, (int)mapChip::eQuestion)); } if (get_mapCell(x, y) == (int)mapChip::eBrick) { //画像ファイル名、座標、画像の横枚数、縦枚数、最初の画像 bmap_set.push_back(Block("mario_resource/Block_64px.png", Position(x * 64, y * 64), 10, 4, (int)mapChip::eBrick)); } } } } int Map::get_mapCell(int& x, int& y) { return map_set[y][x]; } /*描画 更新*/ void Map::Draw_Update() { for (int i = 0; i < bmap_set.size(); i++) { bmap_set[i].Draw_Update(); } } /*全方位の当たり判定 return セル番号を返す*/ std::optional<Position> Map::Collision::player_col(const Position *pos) { for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { if (get_mapCell(x, y) == (int)mapChip::eBrick || get_mapCell(x, y) == (int)mapChip::eQuestion) { if ( (pos->get_x() < (x * CELL) && pos->get_x() + CELL >= (x * CELL) || pos->get_x() > (x * CELL) && pos->get_x() <= (x * CELL) + CELL) && (pos->get_y() < (y * CELL) && pos->get_y() + CELL >= (y * CELL) || pos->get_y() > (y * CELL) && pos->get_y() <= (y * CELL) + CELL) ) { ofs<< "player_col()\n"; std::optional<Position> p = Position(x, y); return p.value();//セルを返す // return true; } } //pos->get_x() <= (x * CELL) && pos->get_x() + CELL >= (x * CELL) } } return std::nullopt; } /*計算 更新*/ void Map::Update() { /* int idx = 0; for (int i = 0; bmap_set.size(); i++) { } */ for (int i = 0; i < bmap_set.size(); i++) { bmap_set[i].Update(); } //bmap_set[0].Update(); //bmap_set[0].Debug_Log(); //Block b("mario_resource/Block_64px.png", Position(1 * 64, 1 * 64), 10, 4, 5); //b.Debug_Log(); //std::ofstream ofs("Log.txt"); // ofs << bmap_set.size(); for (int y = 0; y < 11; y++) { for (int x = 0; x < 20; x++) { if (get_mapCell(x,y) == 0) { //idx += 1; //bmap_set[y][x]->Update_changeAnime(anime_s::ewait); //bmap_set[idx].Update(); } if (get_mapCell(x, y) == 5) { //idx += 1; //画像ファイル名、座標、画像の横枚数、縦枚数、最初の画像 //bmap_set[idx].Update(); } } } } //////////////////////////////////////////////////////ここのコード //引数は今の座標と前の座標 std::optional<Position> Map::Collision::under_col(const Position *pos,const Position *prev)//下の当たり判定 { if (Map::Collision::player_col(pos) != std::nullopt) { if (pos->get_y() > prev->get_y()) { std::optional<Position> p = Map::Collision::player_col(pos); p->new_pos_x(p->get_x() * CELL); p->new_pos_y((p->get_y() * CELL) - CELL); return p.value(); // return //return true; } else { return std::nullopt; } } else { return std::nullopt; } }
Player.cpp
#include <iostream> #include <fstream> #include "Input.h" #include "Player.h" #include "DxLib.h" #include "Map.h" #include "Animation.h" #define MOVE_SPD 5 #define JUMP_FORCE 4.0624 * 3 static std::ofstream ofs("Log.txt"); /*コンストラクタ*/ Player::Player(const char* str, int xx, int yy) { //LoadDivGraph(str,7,7,1,64,64,g_handle); AnimeClip = new Animation(anime_s::ewait,str,7,7,1); pos = new Position(); prev = new Position(); AnimeClip->setAnime(anime_s::ewalk,1,3,3); AnimeClip->setAnime(anime_s::ewait, 0, 0, 1); AnimeClip->setAnime(anime_s::ejump, 5, 5, 1); } /*計算更新*/ void Player::Update() { input_key(); jump_up(); //side_move();// gravity(); AnimeClip->Update(); DrawFormatString(100, 200, GetColor(255, 255, 255), "isGround %d", isGround); DrawFormatString(100, 280, GetColor(255, 255, 255), "isJump %d", isJump); *prev = *pos; } /*描画更新*/ void Player::Draw_Update() { if ( key == -1) { //DrawTurnGraph(get_x(), get_y(), g_handle[0], true); /////// DrawTurnGraph(pos->get_x(), pos->get_y(), AnimeClip->draw_setClip(), true); } /*右→*/ if (key == 1) { /// DrawGraph(pos->get_x(), pos->get_y(), AnimeClip->draw_setClip(), true); } AnimeClip->Draw_Update(); } /*キー入力*/ void Player::input_key() { if (keybord(KEY_INPUT_LEFT) > 0)//← { key = -1; AnimeClip->Update_changeAnime(anime_s::ewalk,key); pos->set_x(-MOVE_SPD); }else if (keybord(KEY_INPUT_RIGHT) > 0)//→ { key = 1; AnimeClip->Update_changeAnime(anime_s::ewalk,key); pos->set_x(+MOVE_SPD); }else if (keybord(KEY_INPUT_UP) > 0)//上 { pos->set_y(+MOVE_SPD); }else if (keybord(KEY_INPUT_DOWN) > 0)//下 { pos->set_y(-MOVE_SPD); } else if (keybord(KEY_INPUT_SPACE) > 0)//ジャンプ { //AnimeClip->Update_changeAnime(anime_s::ejump); //if(Map::Collision::under_col(pos->refget_x(), pos->refget_y()) == false) if (isGround == true && jf == 0) { //isJump = true; isGround = false; jf = JUMP_FORCE; } AnimeClip->Update_changeAnime(anime_s::ejump); } else//何も押していない時 { AnimeClip->Update_changeAnime(anime_s::ewait); } } /************************ジャンプ ***********************/ void Player::jump_up() { pos->set_y((int)jf); //if (Fps::gframe() % 20 == 0) { jf = ((jf -0.126953125f * 3.0f)); if (jf < -0.126953125f * 3.0f * 10.0f) { jf = -0.126953125f * 3.0f * 10.0f; //pos->set_y(); } //} } /*********************************************************/ /***********************重力*******************************/ void Player::gravity() { //落下 if (Map::Collision::under_col(pos, prev) == std::nullopt && isGround == false) { //DrawFormatString(200, 200, GetColor(255, 255, 255), "x %d", pos->get_x()); pos->set_y((int)jf); jf = ((jf -0.126953125f * 3.0f)); if (jf < -0.126953125f * 3.0f * 10.0f) { jf = -0.126953125f * 3.0f * 10.0f; //pos->set_y(); } } /*地面着地*/ if (Map::Collision::under_col(pos, prev) != std::nullopt && jf < 0) { DrawFormatString(200, 280, GetColor(255, 255, 255), "y %d", pos->get_y()); std::optional<Position> p = Map::Collision::under_col(pos, prev); pos->new_pos_y(p->get_y()); ofs << "under_col true\n"; } else { DrawFormatString(200, 280, GetColor(255, 255, 255), "y %d", pos->get_y()); ofs << "under_col" <<pos->get_y()<<"\n"; } } /**********************************************************/
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/22 11:11
退会済みユーザー
2019/08/22 11:14
2019/08/22 11:29
2019/08/22 11:36
退会済みユーザー
2019/08/22 11:40
退会済みユーザー
2019/08/22 11:42
2019/08/22 11:52
2019/08/22 11:57
退会済みユーザー
2019/08/22 11:57
2019/08/22 12:04
退会済みユーザー
2019/08/22 12:07
2019/08/23 02:24