Skip to content

Roguelike DxLib

Kasugaccho edited this page Nov 14, 2019 · 1 revision
#include "DxLib.h"
#include <DTL.hpp>
#include <array>
#include <bitset>

namespace As {
	constexpr dtl::type::size window_size_x{ 560 }; // ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใฎXๆ–นๅ‘ใฎๅคงใใ•
	constexpr dtl::type::size window_size_y{ 400 }; // ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใฎYๆ–นๅ‘ใฎๅคงใใ•
	constexpr const char* const window_title{ "Roguelike" }; // ใ‚ฒใƒผใƒ ใ‚ฟใ‚คใƒˆใƒซๅ
	constexpr dtl::type::size map_chip_size{ 16 }; // 1ใƒžใƒƒใƒ—ใ‚ใŸใ‚Šใฎๅคงใใ•
	constexpr dtl::type::size matrix_size_x{ window_size_x / map_chip_size }; // ใƒฏใƒผใƒซใƒ‰ใฎXๆ–นๅ‘ใฎๅคงใใ•
	constexpr dtl::type::size matrix_size_y{ window_size_y / map_chip_size }; // ใƒฏใƒผใƒซใƒ‰ใฎYๆ–นๅ‘ใฎๅคงใใ•
	constexpr dtl::base::MatrixRange matrix_range(1, 1, matrix_size_x - 2, matrix_size_y - 2); // ใƒฏใƒผใƒซใƒ‰็ฏ„ๅ›ฒ

	enum : dtl::type::uint8 {
		map_frame, // 0
		map_empty, // 1
		map_wall, // 2
		map_room, // 3
		map_way, // 4
		map_door, // 5
		map_up // 6
	};
	constexpr dtl::shape::Rect<dtl::type::uint8> world_init(matrix_range, map_empty);

	bool update() { return (DxLib::ScreenFlip() != -1 && DxLib::ClearDrawScreen() != -1 && DxLib::ProcessMessage() != -1); }

	unsigned int getColorDungeon(dtl::type::uint8 num_) {
		switch (num_) {
		case map_frame:return DxLib::GetColor(0, 100, 100);
		case map_empty:return DxLib::GetColor(35, 100, 178);
		case map_wall:return DxLib::GetColor(203, 203, 203);
		case map_room:return DxLib::GetColor(53, 158, 69);
		case map_way:return DxLib::GetColor(60, 50, 75);
		case map_door:return DxLib::GetColor(167, 159, 70);
		case map_up:return DxLib::GetColor(180, 120, 120);
		case 255:return DxLib::GetColor(60, 180, 180);
		}
		return DxLib::GetColor(0, 0, 0);
	}

	void drawMap(const int x, const int y, const int chip_size, const dtl::type::uint8 num) {
		DrawBox(x * chip_size, y * chip_size, x * chip_size + chip_size, y * chip_size + chip_size, getColorDungeon(num), TRUE);
		DrawBox(x * chip_size, y * chip_size, x * chip_size + chip_size, y * chip_size + chip_size, GetColor(0, 80, 80), FALSE);
	}

	// ใ‚ญใƒผใฎๅ…ฅๅŠ›็Šถๆ…‹ใ‚’ๆ›ดๆ–ฐใ™ใ‚‹
	template<typename Frame_, typename Up_, typename Down_>
	void updateKey(Frame_& key_frame, Up_& up_key, Down_& down_key) noexcept {
		std::array<char, 256> tmp_key{ {} }; // ็พๅœจใฎใ‚ญใƒผใฎๅ…ฅๅŠ›็Šถๆ…‹ใ‚’ๆ ผ็ดใ™ใ‚‹
		DxLib::GetHitKeyStateAll(tmp_key.data()); // ๅ…จใฆใฎใ‚ญใƒผใฎๅ…ฅๅŠ›็Šถๆ…‹ใ‚’ๅพ—ใ‚‹
		for (dtl::type::size i{}; i < 256; ++i) {
			down_key[i] = (tmp_key[i] != 0 && key_frame[i] == 0); // ๆŠผใ•ใ‚ŒใŸใ‚‰true
			up_key[i] = (tmp_key[i] == 0 && key_frame[i] != 0); // ้›ขใ•ใ‚ŒใŸใ‚‰true
			if (tmp_key[i] != 0) ++key_frame[i]; // i็•ชใฎใ‚ญใƒผใ‚ณใƒผใƒ‰ใซๅฏพๅฟœใ™ใ‚‹ใ‚ญใƒผใŒๆŠผใ•ใ‚Œใฆใ„ใŸใ‚‰ๅŠ ็ฎ—
			else key_frame[i] = 0; // ๆŠผใ•ใ‚Œใฆใ„ใชใ‘ใ‚Œใฐ0ใซใ™ใ‚‹
		}
	}

	template<typename Matrix_>
	void create(Matrix_& matrix, int& player_x, int& player_y) {

		using shape_t = dtl::type::uint8;
		world_init.draw(matrix);
		dtl::shape::RogueLike<shape_t>(matrix_range, ::dtl::base::RogueLikeList<shape_t>(1, 2, 3, 4, 5), 20, dtl::base::MatrixRange(3, 3, 2, 2), dtl::base::MatrixRange(3, 3, 4, 4)).draw(matrix);


		std::vector<DTL_TYPE_PAIR<dtl::type::size, dtl::type::size>> x_list{};

		for (dtl::type::size y{}; y < matrix.size(); ++y)
			for (dtl::type::size x{}; x < matrix[y].size(); ++x)
				if (matrix[y][x] == map_room)
					x_list.emplace_back(x, y);

		const dtl::type::size select_list2{ (dtl::type::size)DTL_RANDOM_ENGINE.get(x_list.size()) };
		matrix[x_list[select_list2].DTL_TYPE_PSECOND][x_list[select_list2].DTL_TYPE_PFIRST] = 6;

		x_list.resize(0);

		for (dtl::type::size y{}; y < matrix.size(); ++y)
			for (dtl::type::size x{}; x < matrix[y].size(); ++x)
				if (matrix[y][x] == map_room)
					x_list.emplace_back(x, y);

		const dtl::type::size select_list{ (dtl::type::size)DTL_RANDOM_ENGINE.get(x_list.size()) };
		player_x = (int)x_list[select_list].DTL_TYPE_PFIRST;
		player_y = (int)x_list[select_list].DTL_TYPE_PSECOND;

	}

	void Main() {

		using shape_t = dtl::type::uint8;
		std::array<std::array<shape_t, matrix_size_x>, matrix_size_y> matrix{ {} };

		int player_x{}, player_y{};

		create(matrix, player_x, player_y);

		// ใ‚ญใƒผใŒๆŠผใ•ใ‚Œใฆใ„ใ‚‹ใƒ•ใƒฌใƒผใƒ ๆ•ฐใ‚’ๆ ผ็ดใ™ใ‚‹
		std::array<std::int_fast32_t, 256> key_frame{ {} };
		std::bitset<256> up_key{};
		std::bitset<256>  down_key{};

		while (As::update()) {
			updateKey(key_frame, up_key, down_key);
			for (dtl::type::size y{}; y < matrix.size(); ++y)
				for (dtl::type::size x{}; x < matrix[y].size(); ++x)
					drawMap((int)x, (int)y, map_chip_size, matrix[y][x]);
			if ((up_key[KEY_INPUT_A] || up_key[KEY_INPUT_LEFT]) && matrix[player_y][player_x - 1] > 2) --player_x;
			else if ((up_key[KEY_INPUT_D] || up_key[KEY_INPUT_RIGHT]) && matrix[player_y][player_x + 1] > 2) ++player_x;
			else if ((up_key[KEY_INPUT_W] || up_key[KEY_INPUT_UP]) && matrix[player_y - 1][player_x] > 2) --player_y;
			else if ((up_key[KEY_INPUT_S] || up_key[KEY_INPUT_DOWN]) && matrix[player_y + 1][player_x] > 2) ++player_y;
			if (matrix[player_y][player_x] == 6 || up_key[KEY_INPUT_SPACE]) create(matrix, player_x, player_y);
			drawMap(player_x, player_y, map_chip_size, 255);
		}
	}
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
	DxLib::SetOutApplicationLogValidFlag(0);
	DxLib::ChangeWindowMode(1);
	DxLib::SetWindowIconID(22);
	DxLib::SetWindowSizeChangeEnableFlag(1);
	DxLib::SetGraphMode(As::window_size_x, As::window_size_y, 32);
	DxLib::SetMainWindowText(As::window_title);
	if (DxLib::DxLib_Init() == -1) return -1;
	DxLib::SetDrawScreen(DX_SCREEN_BACK);
	As::Main();
	return DxLib::DxLib_End();
}

/*
	enum : dtl::type::size { /* 0: ไธŠ /dir_up, /* 1: ไธ‹ /dir_down,/* 2: ๅทฆ /dir_left,/* 3: ๅณ /dir_right };


		dtl::type::size player_dir{ dir_down };

		while (As::update()) {
			updateKey(key_frame, up_key, down_key);
			for (dtl::type::size y{}; y < matrix.size(); ++y)
				for (dtl::type::size x{}; x < matrix[y].size(); ++x)
					drawMap((int)x, (int)y, map_chip_size, matrix[y][x]);
			if (up_key[KEY_INPUT_A] && matrix[player_y][player_x - 1] > 2) {
				player_dir = dir_left;
				--player_x;
			}
			else if (up_key[KEY_INPUT_D] && matrix[player_y][player_x + 1] > 2) {
				player_dir = dir_right;
				++player_x;
			}
			else if (up_key[KEY_INPUT_W] && matrix[player_y - 1][player_x] > 2) {
				player_dir = dir_up;
				--player_y;
			}
			else if (up_key[KEY_INPUT_S] && matrix[player_y + 1][player_x] > 2) {
				player_dir = dir_down;
				++player_y;
			}
			else if (up_key[KEY_INPUT_SPACE]) {
				int dig_x{ player_x }, dig_y{ player_y };
				switch (player_dir) {
				case dir_down:++dig_y; break;
				case dir_up:--dig_y; break;
				case dir_left:--dig_x; break;
				case dir_right:++dig_x; break;
				}
				matrix[dig_y][dig_x] = 2;
			}
*/
Clone this wiki locally