summary refs log tree commit diff stats
path: root/src/direction.h
blob: ebc0e46f603a66d01e1e71069260545d4ea341d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef DIRECTION_H_AB66A90E
#define DIRECTION_H_AB66A90E

#include <string>
#include <stdexcept>
#include "vector.h"

enum class Direction {
  up,
  up_right,
  right,
  down_right,
  down,
  down_left,
  left,
  up_left
};

inline Direction directionFromString(std::string_view str) {
  if (str == "up") return Direction::up;
  if (str == "up_right") return Direction::up_right;
  if (str == "right") return Direction::right;
  if (str == "down_right") return Direction::down_right;
  if (str == "down") return Direction::down;
  if (str == "down_left") return Direction::down_left;
  if (str == "left") return Direction::left;
  if (str == "up_left") return Direction::up_left;
  throw std::invalid_argument("Invalid direction: " + std::string(str));
}

inline vec2i unitVecInDirection(Direction dir) {
  switch (dir) {
    case Direction::up: return { 0, -1 };
    case Direction::up_right: return { 1, -1 };
    case Direction::right: return { 1, 0 };
    case Direction::down_right: return { 1, 1 };
    case Direction::down: return { 0, 1 };
    case Direction::down_left: return { -1, 1 };
    case Direction::left: return { -1, 0 };
    case Direction::up_left: return { -1, -1 };
  }
}

#endif /* end of include guard: DIRECTION_H_AB66A90E */