Conver to using \ for member variables in classes. In structs just use the name.
This commit is contained in:
parent
187edb898e
commit
5cf66aad02
8 changed files with 143 additions and 144 deletions
116
gui.cpp
116
gui.cpp
|
@ -40,80 +40,80 @@ sf::Color GUI::color(Value val) {
|
|||
return VALUES[size_t(val)];
|
||||
}
|
||||
|
||||
GUI::GUI() : game_map_(50, 20),
|
||||
canvas_(60 * 2, 20 * 4),
|
||||
window_(sf::VideoMode(1600,900), "Roguish"),
|
||||
screen_(0,0)
|
||||
GUI::GUI() : $game_map(50, 20),
|
||||
$canvas(60 * 2, 20 * 4),
|
||||
$window(sf::VideoMode(1600,900), "Roguish"),
|
||||
$screen(0,0)
|
||||
{
|
||||
int res = hit_buf_.loadFromFile("./assets/hit.wav");
|
||||
int res = $hit_buf.loadFromFile("./assets/hit.wav");
|
||||
dbc::check(res, "failed to load hit.wav");
|
||||
hit_sound_.setBuffer(hit_buf_);
|
||||
$hit_sound.setBuffer($hit_buf);
|
||||
|
||||
font_.loadFromFile("./assets/text.otf");
|
||||
text_.setFont(font_);
|
||||
text_.setCharacterSize(30);
|
||||
text_.setFillColor(color(Value::LIGHT_DARK));
|
||||
game_map_.generate();
|
||||
player_.location = game_map_.place_entity(0);
|
||||
enemy_.location = game_map_.place_entity(1);
|
||||
goal_ = game_map_.place_entity(game_map_.room_count() - 1);
|
||||
screen_ = Screen::Create(Dimension::Full());
|
||||
$font.loadFromFile("./assets/text.otf");
|
||||
$text.setFont($font);
|
||||
$text.setCharacterSize(30);
|
||||
$text.setFillColor(color(Value::LIGHT_DARK));
|
||||
$game_map.generate();
|
||||
$player.location = $game_map.place_entity(0);
|
||||
$enemy.location = $game_map.place_entity(1);
|
||||
$goal = $game_map.place_entity($game_map.room_count() - 1);
|
||||
$screen = Screen::Create(Dimension::Full());
|
||||
}
|
||||
|
||||
void GUI::create_renderer() {
|
||||
map_view_ = Renderer([&] {
|
||||
Matrix &walls = game_map_.walls();
|
||||
game_map_.set_target(player_.location);
|
||||
game_map_.make_paths();
|
||||
Matrix &paths = game_map_.paths();
|
||||
$map_view = Renderer([&] {
|
||||
Matrix &walls = $game_map.walls();
|
||||
$game_map.set_target($player.location);
|
||||
$game_map.make_paths();
|
||||
Matrix &paths = $game_map.paths();
|
||||
|
||||
if(player_.in_state(EntityState::DEAD)) {
|
||||
status_text_ = "DEAD!";
|
||||
if($player.in_state(EntityState::DEAD)) {
|
||||
$status_text = "DEAD!";
|
||||
}
|
||||
|
||||
for(size_t x = 0; x < walls[0].size(); ++x) {
|
||||
for(size_t y = 0; y < walls.size(); ++y) {
|
||||
string tile = walls[y][x] == 1 ? "#" : format("{}", paths[y][x]);
|
||||
if(tile == "#") {
|
||||
canvas_.DrawText(x*2, y*4, tile);
|
||||
} else if(show_paths_) {
|
||||
$canvas.DrawText(x*2, y*4, tile);
|
||||
} else if($show_paths) {
|
||||
//int pnum = paths[y][x];
|
||||
canvas_.DrawText(x*2, y*4, tile);
|
||||
$canvas.DrawText(x*2, y*4, tile);
|
||||
} else {
|
||||
canvas_.DrawText(x*2, y*4, ".");
|
||||
$canvas.DrawText(x*2, y*4, ".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
canvas_.DrawText(enemy_.location.x*2, enemy_.location.y*4, "!");
|
||||
canvas_.DrawText(player_.location.x*2, player_.location.y*4, "@");
|
||||
canvas_.DrawText(goal_.x*2, goal_.y*4, "$");
|
||||
$canvas.DrawText($enemy.location.x*2, $enemy.location.y*4, "!");
|
||||
$canvas.DrawText($player.location.x*2, $player.location.y*4, "@");
|
||||
$canvas.DrawText($goal.x*2, $goal.y*4, "$");
|
||||
|
||||
return canvas(canvas_);
|
||||
return canvas($canvas);
|
||||
});
|
||||
|
||||
document_ = Renderer([&]{
|
||||
$document = Renderer([&]{
|
||||
return hbox({
|
||||
hflow(
|
||||
vbox(
|
||||
text(format("HP: {}", player_.hp)) | border,
|
||||
text(status_text_) | border
|
||||
text(format("HP: {}", $player.hp)) | border,
|
||||
text($status_text) | border
|
||||
) | xflex_grow
|
||||
),
|
||||
separator(),
|
||||
hbox(map_view_->Render()),
|
||||
hbox($map_view->Render()),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void GUI::handle_events() {
|
||||
sf::Event event;
|
||||
while(window_.pollEvent(event)) {
|
||||
while($window.pollEvent(event)) {
|
||||
if(event.type == sf::Event::Closed) {
|
||||
window_.close();
|
||||
$window.close();
|
||||
} else if(event.type == sf::Event::KeyPressed) {
|
||||
size_t x = player_.location.x;
|
||||
size_t y = player_.location.y;
|
||||
size_t x = $player.location.x;
|
||||
size_t y = $player.location.y;
|
||||
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||
x -= 1;
|
||||
|
@ -125,44 +125,44 @@ void GUI::handle_events() {
|
|||
y += 1;
|
||||
}
|
||||
|
||||
if(game_map_.inmap(x,y) && !game_map_.iswall(x,y)) {
|
||||
game_map_.clear_target(player_.location);
|
||||
player_.move({x, y});
|
||||
if($game_map.inmap(x,y) && !$game_map.iswall(x,y)) {
|
||||
$game_map.clear_target($player.location);
|
||||
$player.move({x, y});
|
||||
} else {
|
||||
hit_sound_.play();
|
||||
$hit_sound.play();
|
||||
}
|
||||
|
||||
// move enemy_ here
|
||||
bool found = game_map_.neighbors(enemy_.location, true);
|
||||
// move $enemy here
|
||||
bool found = $game_map.neighbors($enemy.location, true);
|
||||
if(!found) {
|
||||
status_text_ = "ENEMY STUCK!";
|
||||
$status_text = "ENEMY STUCK!";
|
||||
}
|
||||
|
||||
if(enemy_.location.x == player_.location.x && enemy_.location.y == player_.location.y) {
|
||||
player_.event(EntityEvent::HIT);
|
||||
} else if(goal_.x == player_.location.x && goal_.y == player_.location.y) {
|
||||
status_text_ = "YOU WIN!";
|
||||
if($enemy.location.x == $player.location.x && $enemy.location.y == $player.location.y) {
|
||||
$player.event(EntityEvent::HIT);
|
||||
} else if($goal.x == $player.location.x && $goal.y == $player.location.y) {
|
||||
$status_text = "YOU WIN!";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GUI::render_scene() {
|
||||
Render(screen_, document_->Render());
|
||||
std::string screen_out = screen_.ToString();
|
||||
std::wstring utf8 = converter_.from_bytes(screen_out);
|
||||
text_.setString(utf8);
|
||||
text_.setPosition({0,0});
|
||||
Render($screen, $document->Render());
|
||||
std::string $screenout = $screen.ToString();
|
||||
std::wstring utf8 = $converter.from_bytes($screenout);
|
||||
$text.setString(utf8);
|
||||
$text.setPosition({0,0});
|
||||
|
||||
window_.clear();
|
||||
window_.draw(text_);
|
||||
window_.display();
|
||||
$window.clear();
|
||||
$window.draw($text);
|
||||
$window.display();
|
||||
}
|
||||
|
||||
int GUI::main() {
|
||||
create_renderer();
|
||||
|
||||
while(window_.isOpen()) {
|
||||
while($window.isOpen()) {
|
||||
render_scene();
|
||||
handle_events();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue