Skip to content

Commit

Permalink
Merge pull request #5 from odanado/impl_direction
Browse files Browse the repository at this point in the history
Impl direction
  • Loading branch information
odanado authored Jul 16, 2016
2 parents 8bf65a4 + 4715caf commit 09ddee6
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 70 deletions.
5 changes: 3 additions & 2 deletions include/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <tuple>
#include <chrono>
#include <cassert>

#include "Config.h"
#include "Direction.h"

namespace oti_oti_fight {
class Board {
Expand All @@ -23,7 +25,6 @@ class Board {
* UNSTABLE: 落下対象になった状態
*/
enum struct State { ENABLE, DISABLE, UNSTABLE };
enum struct AttackDirection { UP, DOWN, LEFT, RIGHT };

Board();

Expand All @@ -35,7 +36,7 @@ class Board {
* @y: 攻撃したマス目のy
* @dir: 攻撃した方向
*/
void attack(int id, int x, int y, AttackDirection dir);
void attack(int id, int x, int y, Direction dir);
void update();

State getState(int x, int y) const {
Expand Down
11 changes: 11 additions & 0 deletions include/Direction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (C) 2016 odanado
* Licensed under the MIT License.
*/

#ifndef INCLUDE_DIRECTION_H_
#define INCLUDE_DIRECTION_H_
namespace oti_oti_fight {
enum struct Direction { UP, DOWN, RIGHT, LEFT };
} // namespace oti_oti_fight
#endif // INCLUDE_DIRECTION_H_
33 changes: 32 additions & 1 deletion include/NCursesUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,41 @@

namespace oti_oti_fight {
namespace NCursesUtil {

// SUB_ は減色?された色
enum struct Color {
BLACK = 1,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
SUB_BLACK,
SUB_RED,
SUB_GREEN,
SUB_YELLOW,
SUB_BLUE,
SUB_MAGENTA,
SUB_CYAN,
SUB_WHITE
};

void init();
void drawString(int x, int y, const std::string &str);
void drawString(int x, int y, const std::string &str, Color = Color::BLACK);
int getWidth();
int getHeight();

void changeColor(Color color);

// (x, y) の属するドットにcolorで点を打つ
void drawDot(Color color, int x, int y);

void clear();

void refresh();

} // namespace NCursesUtil
} // namespace oti_oti_fight

Expand Down
9 changes: 5 additions & 4 deletions include/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@

#include <string>
#include "Config.h"
#include "Direction.h"

namespace oti_oti_fight {
class Player {
public:
enum struct Move { UP, DOWN, LEFT, RIGHT, ATTACK, NONE };

Player() = default;
Player(const std::string &name, int x, int y,
Player(const std::string &name, int x, int y, Direction dir,
int remainingPlayers = Config::MAX_REMAINING_PLAYERS);
std::string getName(void) const { return name; }
int getX(void) const { return x; }
int getY(void) const { return y; }
int getRemainingPlayers(void) { return remainingPlayers; }
bool died(void) const { return remainingPlayers == 0; }

void move(Move);
void move(Direction);
void attack();

private:
void normalizePos(void);
std::string name;
int x, y;
Direction dir;
int remainingPlayers;
};
} // namespace oti_oti_fight
Expand Down
2 changes: 0 additions & 2 deletions include/TitleScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#ifndef INCLUDE_TITLESCENE_H_
#define INCLUDE_TITLESCENE_H_

#include <ncurses.h>

#include "SceneManager.h"

namespace oti_oti_fight {
Expand Down
12 changes: 6 additions & 6 deletions src/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ Board::Board() {
}
}

void Board::attack(int id, int x, int y, AttackDirection dir) {
void Board::attack(int id, int x, int y, Direction dir) {
assert(0 <= id && id < 4);
assert(0 <= x && x < Config::BOARD_WIDTH);
assert(0 <= y && y < Config::BOARD_HEIGHT);
int dx, dy;
switch (dir) {
case AttackDirection::UP:
case Direction::UP:
dx = 0;
dy = -1;
y -= 1 + (y % 2 != 0);
break;
case AttackDirection::DOWN:
case Direction::DOWN:
dx = 0;
dy = 1;
y += 1 + (y % 2 == 0);
break;
case AttackDirection::LEFT:
case Direction::LEFT:
dx = -1;
dy = 0;
x -= 1 + (x % 2 != 0);
break;
case AttackDirection::RIGHT:
case Direction::RIGHT:
dx = 1;
dy = 0;
x += 1 + (x % 2 == 0);
break;
}
attackImpl(id, x, y, dx, dy);
if (dir == AttackDirection::RIGHT || dir == AttackDirection::LEFT) {
if (dir == Direction::RIGHT || dir == Direction::LEFT) {
if (y % 2 == 0) {
attackImpl(id, x, y + 1, dx, dy);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ void Input::operator()() {
keyRight.pressed = ch == KEY_RIGHT;
keyLeft.pressed = ch == KEY_LEFT;
keyX.pressed = ch == 'x';
keyEnter.pressed = ch == KEY_ENTER;
keyEnter.pressed = ch == 10;
}
} // namespace oti_oti_fight
24 changes: 23 additions & 1 deletion src/NCursesUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
#include <locale.h>

#include <string>
#include <cstdlib>

#include "NCursesUtil.h"

namespace oti_oti_fight {
namespace NCursesUtil {
void init() {
setenv("TERM", "xterm-256color", 1);
setlocale(LC_ALL, "");
initscr();
start_color();
keypad(stdscr, TRUE);
crmode();
noecho();
curs_set(0);
timeout(0);
for (int i = 0; i < 8; i++) {
init_pair(i + 1, COLOR_WHITE, i);
}
}
void drawString(int x, int y, const std::string &str) {
void drawString(int x, int y, const std::string &str, Color color) {
changeColor(color);
mvprintw(y, x - str.size() / 2, "%s", str.c_str());
}

Expand All @@ -36,5 +43,20 @@ int getHeight() {
getmaxyx(stdscr, y, x);
return y;
}

void changeColor(Color color) { attron(COLOR_PAIR(static_cast<int>(color))); }
void drawDot(Color color, int x, int y) {
changeColor(color);
mvprintw(y, x - x % 2, " ");
}

void clear() {
::clear();
}

void refresh() {
::refresh();
}

} // namespace NCursesUtil
} // namespace oti_oti_fight
23 changes: 9 additions & 14 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,29 @@

namespace oti_oti_fight {

Player::Player(const std::string &name, int x, int y, int remainingPlayers)
: name(name), x(x), y(y), remainingPlayers(remainingPlayers) {}
Player::Player(const std::string &name, int x, int y, Direction dir,
int remainingPlayers)
: name(name), x(x), y(y), dir(dir), remainingPlayers(remainingPlayers) {}

void Player::move(Player::Move act) {
void Player::move(Direction act) {
switch (act) {
case Move::UP:
case Direction::UP:
--y;
break;

case Move::DOWN:
case Direction::DOWN:
++y;
break;

case Move::LEFT:
case Direction::LEFT:
--x;
break;

case Move::RIGHT:
case Direction::RIGHT:
++x;
break;

case Move::ATTACK:
// TODO(odanado): ato de zisso suru
break;

case Move::NONE:
break;
}
dir = act;
normalizePos();
}

Expand Down
5 changes: 5 additions & 0 deletions src/TitleScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ void TitleScene::update() noexcept {
data->isSoroPlay = true;
}
if (data->input.keyEnter.pressed) {
if (data->isSoroPlay)
changeScene("Game");
// else WIP
}
}

void TitleScene::draw() noexcept {
using NCursesUtil::drawString;
using NCursesUtil::getHeight;
using NCursesUtil::getWidth;
using NCursesUtil::clear;
using NCursesUtil::refresh;

int y = getHeight(), x = getWidth();
auto getMaker = [](bool isSoroPlay) {
Expand Down
36 changes: 18 additions & 18 deletions test/TestBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ TEST_F(TestBoard, attackRight) {
}
};
Board board;
board.attack(0, 6, 6, Board::AttackDirection::RIGHT);
board.attack(0, 6, 6, Direction::RIGHT);
test(board);

board = Board();
board.attack(0, 6, 7, Board::AttackDirection::RIGHT);
board.attack(0, 6, 7, Direction::RIGHT);
test(board);

board = Board();
board.attack(0, 7, 6, Board::AttackDirection::RIGHT);
board.attack(0, 7, 6, Direction::RIGHT);
test(board);

board = Board();
board.attack(0, 7, 7, Board::AttackDirection::RIGHT);
board.attack(0, 7, 7, Direction::RIGHT);
test(board);
}
TEST_F(TestBoard, attackLeft) {
Expand All @@ -73,19 +73,19 @@ TEST_F(TestBoard, attackLeft) {
}
};
Board board;
board.attack(0, 6, 6, Board::AttackDirection::LEFT);
board.attack(0, 6, 6, Direction::LEFT);
test(board);

board = Board();
board.attack(0, 7, 6, Board::AttackDirection::LEFT);
board.attack(0, 7, 6, Direction::LEFT);
test(board);

board = Board();
board.attack(0, 6, 7, Board::AttackDirection::LEFT);
board.attack(0, 6, 7, Direction::LEFT);
test(board);

board = Board();
board.attack(0, 7, 7, Board::AttackDirection::LEFT);
board.attack(0, 7, 7, Direction::LEFT);
test(board);
}
TEST_F(TestBoard, attackUp) {
Expand All @@ -111,19 +111,19 @@ TEST_F(TestBoard, attackUp) {
}
};
Board board;
board.attack(0, 6, 6, Board::AttackDirection::UP);
board.attack(0, 6, 6, Direction::UP);
test(board);

board = Board();
board.attack(0, 7, 6, Board::AttackDirection::UP);
board.attack(0, 7, 6, Direction::UP);
test(board);

board = Board();
board.attack(0, 6, 7, Board::AttackDirection::UP);
board.attack(0, 6, 7, Direction::UP);
test(board);

board = Board();
board.attack(0, 7, 7, Board::AttackDirection::UP);
board.attack(0, 7, 7, Direction::UP);
test(board);
}
TEST_F(TestBoard, attackDown) {
Expand All @@ -149,25 +149,25 @@ TEST_F(TestBoard, attackDown) {
}
};
Board board;
board.attack(0, 6, 6, Board::AttackDirection::DOWN);
board.attack(0, 6, 6, Direction::DOWN);
test(board);

board = Board();
board.attack(0, 7, 6, Board::AttackDirection::DOWN);
board.attack(0, 7, 6, Direction::DOWN);
test(board);

board = Board();
board.attack(0, 6, 7, Board::AttackDirection::DOWN);
board.attack(0, 6, 7, Direction::DOWN);
test(board);

board = Board();
board.attack(0, 7, 7, Board::AttackDirection::DOWN);
board.attack(0, 7, 7, Direction::DOWN);
test(board);
}
TEST_F(TestBoard, multiAttack) {
Board board;
board.attack(0, 0, 0, Board::AttackDirection::RIGHT);
board.attack(1, 6, 6, Board::AttackDirection::UP);
board.attack(0, 0, 0, Direction::RIGHT);
board.attack(1, 6, 6, Direction::UP);
for (int y = 0; y < Config::BOARD_HEIGHT; y++) {
for (int x = 0; x < Config::BOARD_WIDTH; x++) {
int v = x + y * Config::BOARD_HEIGHT;
Expand Down
Loading

0 comments on commit 09ddee6

Please sign in to comment.