Tiles now record their textures and this is loaded from the map then converted to an indexed integer on the fly.
This commit is contained in:
parent
9e3e347e4a
commit
a67d25ee10
10 changed files with 64 additions and 66 deletions
|
@ -15,7 +15,7 @@ void Animator::step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int
|
||||||
{rect_w, rect_h}}));
|
{rect_w, rect_h}}));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Animator::play() {
|
void Animator::play(bool sound_too) {
|
||||||
playing = true;
|
playing = true;
|
||||||
sound.play();
|
if(sound_too) sound.play();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,5 +22,5 @@ struct Animator {
|
||||||
|
|
||||||
void step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h);
|
void step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h);
|
||||||
|
|
||||||
void play();
|
void play(bool sound_too=true);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,13 +1,4 @@
|
||||||
{
|
{
|
||||||
"textures": [
|
|
||||||
"assets/wall_simple-256.png",
|
|
||||||
"assets/wall_with_vines-256.png",
|
|
||||||
"assets/wall_with_pillars-256.png",
|
|
||||||
"assets/wall_texture_test-256.png",
|
|
||||||
"assets/floor_tile_test-256.png",
|
|
||||||
"assets/ceiling_test-256.png",
|
|
||||||
"assets/wood_wall-256.png"
|
|
||||||
],
|
|
||||||
"sprites": {
|
"sprites": {
|
||||||
"armored_knight": "assets/armored_knight_1-256.png",
|
"armored_knight": "assets/armored_knight_1-256.png",
|
||||||
"sword": "assets/cinqueda_1-512.png",
|
"sword": "assets/cinqueda_1-512.png",
|
||||||
|
|
|
@ -1,27 +1,27 @@
|
||||||
{
|
{
|
||||||
"FLOOR_TILE": {
|
"FLOOR_TILE": {
|
||||||
"id": 0,
|
"texture": "assets/floor_tile_test-256.png",
|
||||||
"foreground": [40, 15, 125],
|
"foreground": [40, 15, 125],
|
||||||
"background": [200, 15, 75],
|
"background": [200, 15, 75],
|
||||||
"collision": false,
|
"collision": false,
|
||||||
"display":"\u289e"
|
"display":"\u289e"
|
||||||
},
|
},
|
||||||
"WALL_PLAIN": {
|
"WALL_PLAIN": {
|
||||||
"id": 1,
|
"texture": "assets/wall_simple-256.png",
|
||||||
"foreground": [230, 20, 30],
|
"foreground": [230, 20, 30],
|
||||||
"background": [230, 20, 120],
|
"background": [230, 20, 120],
|
||||||
"collision": true,
|
"collision": true,
|
||||||
"display": "\ua5b8"
|
"display": "\ua5b8"
|
||||||
},
|
},
|
||||||
"WALL_VINES": {
|
"WALL_VINES": {
|
||||||
"id": 2,
|
"texture": "assets/wall_with_vines-256.png",
|
||||||
"foreground": [40, 15, 125],
|
"foreground": [40, 15, 125],
|
||||||
"background": [200, 29, 75],
|
"background": [200, 29, 75],
|
||||||
"collision": false,
|
"collision": false,
|
||||||
"display":"\u19f0"
|
"display":"\u19f0"
|
||||||
},
|
},
|
||||||
"WALL_PILLAR": {
|
"WALL_PILLAR": {
|
||||||
"id": 3,
|
"texture": "assets/wall_with_pillars-256.png",
|
||||||
"foreground": [40, 15, 125],
|
"foreground": [40, 15, 125],
|
||||||
"background": [200, 29, 75],
|
"background": [200, 29, 75],
|
||||||
"collision": false,
|
"collision": false,
|
||||||
|
|
55
main.cpp
55
main.cpp
|
@ -5,7 +5,10 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "stats.hpp"
|
#include "stats.hpp"
|
||||||
#include "worldbuilder.hpp"
|
#include "levelmanager.hpp"
|
||||||
|
#include "components.hpp"
|
||||||
|
|
||||||
|
using namespace components;
|
||||||
|
|
||||||
void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) {
|
void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) {
|
||||||
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT});
|
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT});
|
||||||
|
@ -20,36 +23,13 @@ void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) {
|
||||||
window.draw(text);
|
window.draw(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix generate_map(Map& map, Point &player) {
|
Matrix generate_map(TexturePack &textures, GameLevel &level, Point &player_out) {
|
||||||
// generate the world and make the map
|
auto &tiles = level.map->tiles();
|
||||||
WorldBuilder builder(map);
|
auto &player = level.world->get_the<Player>();
|
||||||
builder.generate_map();
|
auto &player_position = level.world->get<Position>(player.entity);
|
||||||
|
player_out = player_position.location;
|
||||||
|
|
||||||
bool can_place = map.place_entity(1, player);
|
return textures.convert_char_to_texture(tiles.$tile_ids);
|
||||||
dbc::check(can_place, "couldn't place the player");
|
|
||||||
|
|
||||||
auto &tiles = map.tiles();
|
|
||||||
tiles.dump(player.x, player.y);
|
|
||||||
|
|
||||||
auto bad_map = matrix::make(tiles.width(), tiles.height());
|
|
||||||
|
|
||||||
for(matrix::each_cell it(tiles.$tile_ids); it.next();) {
|
|
||||||
switch(tiles.$tile_ids[it.y][it.x]) {
|
|
||||||
case 0x289e:
|
|
||||||
bad_map[it.y][it.x] = 0; break;
|
|
||||||
case 0xa5b8:
|
|
||||||
bad_map[it.y][it.x] = 1; break;
|
|
||||||
case 0x19f0:
|
|
||||||
bad_map[it.y][it.x] = 2; break;
|
|
||||||
case 0x16de:
|
|
||||||
bad_map[it.y][it.x] = 3; break;
|
|
||||||
default:
|
|
||||||
bad_map[it.y][it.x] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
matrix::dump("CONVERTED MAP", bad_map);
|
|
||||||
return bad_map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_weapon(sf::RenderWindow &window, sf::Sprite &weapon, float rotation) {
|
void draw_weapon(sf::RenderWindow &window, sf::Sprite &weapon, float rotation) {
|
||||||
|
@ -66,11 +46,18 @@ int main() {
|
||||||
text.setFillColor({255,255,255});
|
text.setFillColor({255,255,255});
|
||||||
text.setPosition({10,10});
|
text.setPosition({10,10});
|
||||||
|
|
||||||
Map map(30, 30);
|
|
||||||
Point player{0, 0};
|
Point player{0, 0};
|
||||||
auto MAP = generate_map(map, player);
|
LevelManager levels;
|
||||||
|
GameLevel &cur_level = levels.current();
|
||||||
|
|
||||||
Raycaster rayview(window, MAP, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
|
TexturePack textures;
|
||||||
|
textures.load_tiles();
|
||||||
|
textures.load_sprites();
|
||||||
|
textures.position_sprite(4.0, 3.55, "evil_eye");
|
||||||
|
|
||||||
|
auto map = generate_map(textures, cur_level, player);
|
||||||
|
|
||||||
|
Raycaster rayview(window, textures, map, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
|
||||||
rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
|
rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
|
||||||
rayview.position_camera(player.x, player.y);
|
rayview.position_camera(player.x, player.y);
|
||||||
rayview.init_shaders();
|
rayview.init_shaders();
|
||||||
|
@ -126,7 +113,7 @@ int main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
|
||||||
rayview.$anim.play();
|
rayview.$anim.play(false);
|
||||||
rotation = -30.0f;
|
rotation = -30.0f;
|
||||||
} else {
|
} else {
|
||||||
rotation = -10.0f;
|
rotation = -10.0f;
|
||||||
|
|
|
@ -27,8 +27,8 @@ inline uint32_t dumb_lighting(uint32_t pixel, double distance) {
|
||||||
return conv.as_int;
|
return conv.as_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Raycaster::Raycaster(sf::RenderWindow& window, TexturePack &textures, Matrix &map, int width, int height) :
|
||||||
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
|
$textures(textures),
|
||||||
$view_texture({(unsigned int)width, (unsigned int)height}),
|
$view_texture({(unsigned int)width, (unsigned int)height}),
|
||||||
$view_sprite($view_texture),
|
$view_sprite($view_texture),
|
||||||
$width(width), $height(height),
|
$width(width), $height(height),
|
||||||
|
@ -41,9 +41,6 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
|
||||||
{
|
{
|
||||||
$view_sprite.setPosition({0, 0});
|
$view_sprite.setPosition({0, 0});
|
||||||
$pixels = make_unique<RGBA[]>($width * $height);
|
$pixels = make_unique<RGBA[]>($width * $height);
|
||||||
$textures.load_textures();
|
|
||||||
$textures.load_sprites();
|
|
||||||
$textures.position_sprite(4.0, 3.55, "evil_eye");
|
|
||||||
$view_texture.setSmooth(false);
|
$view_texture.setSmooth(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ struct Raycaster {
|
||||||
int $pitch=0;
|
int $pitch=0;
|
||||||
sf::Clock $clock;
|
sf::Clock $clock;
|
||||||
|
|
||||||
TexturePack $textures;
|
TexturePack &$textures;
|
||||||
double $posX = 0;
|
double $posX = 0;
|
||||||
double $posY = 0;
|
double $posY = 0;
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ struct Raycaster {
|
||||||
sf::Shader $paused;
|
sf::Shader $paused;
|
||||||
sf::Shader* $active_shader = nullptr;
|
sf::Shader* $active_shader = nullptr;
|
||||||
|
|
||||||
Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height);
|
Raycaster(sf::RenderWindow& window, TexturePack &textures, Matrix &map, int width, int height);
|
||||||
|
|
||||||
void draw_pixel_buffer();
|
void draw_pixel_buffer();
|
||||||
void clear();
|
void clear();
|
||||||
|
|
33
texture.cpp
33
texture.cpp
|
@ -29,21 +29,29 @@ void TexturePack::load_sprites() {
|
||||||
}
|
}
|
||||||
|
|
||||||
sword = sprite_textures["sword"];
|
sword = sprite_textures["sword"];
|
||||||
|
|
||||||
|
floor = load_image(assets["sprites"]["floor"]);
|
||||||
|
ceiling = load_image(assets["sprites"]["ceiling"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TexturePack::position_sprite(double x, double y, string name) {
|
void TexturePack::position_sprite(double x, double y, string name) {
|
||||||
sprites.emplace_back(x, y, sprite_textures[name]);
|
sprites.emplace_back(x, y, sprite_textures[name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TexturePack::load_textures() {
|
void TexturePack::load_tiles() {
|
||||||
Config assets("assets/config.json");
|
Config assets("assets/tiles.json");
|
||||||
|
auto &tiles = assets.json();
|
||||||
|
|
||||||
for(string tile_path : assets["textures"]) {
|
for(auto &el : tiles.items()) {
|
||||||
surfaces.emplace_back(load_image(tile_path));
|
auto &config = el.value();
|
||||||
|
surfaces.emplace_back(load_image(config["texture"]));
|
||||||
|
|
||||||
|
std::wstring display = assets.wstring(el.key(), "display");
|
||||||
|
int surface_i = surfaces.size() - 1;
|
||||||
|
wchar_t tid = display[0];
|
||||||
|
|
||||||
|
char_to_texture[tid] = surface_i;
|
||||||
}
|
}
|
||||||
|
|
||||||
floor = load_image(assets["sprites"]["floor"]);
|
|
||||||
ceiling = load_image(assets["sprites"]["ceiling"]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint32_t* TexturePack::get_surface(size_t num) {
|
const uint32_t* TexturePack::get_surface(size_t num) {
|
||||||
|
@ -53,3 +61,14 @@ const uint32_t* TexturePack::get_surface(size_t num) {
|
||||||
Sprite &TexturePack::get_sprite(size_t sprite_num) {
|
Sprite &TexturePack::get_sprite(size_t sprite_num) {
|
||||||
return sprites[sprite_num];
|
return sprites[sprite_num];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) {
|
||||||
|
auto result = matrix::make(matrix::width(tile_ids), matrix::height(tile_ids));
|
||||||
|
|
||||||
|
for(matrix::each_cell it(tile_ids); it.next();) {
|
||||||
|
wchar_t tid = tile_ids[it.y][it.x];
|
||||||
|
result[it.y][it.x] = char_to_texture.at(tid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "matrix.hpp"
|
||||||
|
|
||||||
struct SpriteTexture {
|
struct SpriteTexture {
|
||||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||||
|
@ -22,15 +23,19 @@ struct TexturePack {
|
||||||
std::vector<sf::Image> surfaces;
|
std::vector<sf::Image> surfaces;
|
||||||
std::vector<Sprite> sprites;
|
std::vector<Sprite> sprites;
|
||||||
std::unordered_map<std::string, SpriteTexture> sprite_textures;
|
std::unordered_map<std::string, SpriteTexture> sprite_textures;
|
||||||
|
std::unordered_map<wchar_t, int> char_to_texture;
|
||||||
sf::Image floor;
|
sf::Image floor;
|
||||||
sf::Image ceiling;
|
sf::Image ceiling;
|
||||||
SpriteTexture sword;
|
SpriteTexture sword;
|
||||||
|
|
||||||
void load_textures();
|
void load_tiles();
|
||||||
void load_sprites();
|
void load_sprites();
|
||||||
sf::Image load_image(std::string filename);
|
sf::Image load_image(std::string filename);
|
||||||
Sprite& get_sprite(size_t sprite_num);
|
Sprite& get_sprite(size_t sprite_num);
|
||||||
const uint32_t* get_surface(size_t num);
|
const uint32_t* get_surface(size_t num);
|
||||||
// this needs to go into a map place
|
// this needs to go into a map place
|
||||||
void position_sprite(double x, double y, std::string name);
|
void position_sprite(double x, double y, std::string name);
|
||||||
|
|
||||||
|
// ZED: this is ugly so maybe you should like rewrite it or something
|
||||||
|
matrix::Matrix convert_char_to_texture(matrix::Matrix &from);
|
||||||
};
|
};
|
||||||
|
|
|
@ -110,7 +110,6 @@ void WorldBuilder::stylize_room(int room, string tile_name, float size) {
|
||||||
bool placed = $map.place_entity(room, pos_out);
|
bool placed = $map.place_entity(room, pos_out);
|
||||||
dbc::check(placed, "failed to place style in room");
|
dbc::check(placed, "failed to place style in room");
|
||||||
|
|
||||||
dbc::log("FIX THE SYTLE ROOM TO VARY FLOOR OR WALL");
|
|
||||||
tile_name = tile_name == "FLOOR_TILE" ? "WALL_PLAIN" : tile_name;
|
tile_name = tile_name == "FLOOR_TILE" ? "WALL_PLAIN" : tile_name;
|
||||||
|
|
||||||
for(matrix::circle it{$map.$walls, pos_out, size}; it.next();) {
|
for(matrix::circle it{$map.$walls, pos_out, size}; it.next();) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue