diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/level.cpp | 68 | ||||
| -rw-r--r-- | src/level.h | 29 | ||||
| -rw-r--r-- | src/train.cpp | 28 | 
3 files changed, 68 insertions, 57 deletions
| diff --git a/src/level.cpp b/src/level.cpp index 92ec208..bdff5fb 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -6,16 +6,15 @@ Level::Level(int width, int height, Bitmap& tileSprites)  	: m_width(width), m_height(height), m_tileSprites(tileSprites)  {  	m_tiles = new uint8_t[width * height]; -	memset(m_tiles, 0, width * height); +	memset(m_tiles, TileGround, width * height);  }  uint8_t Level::get(int x, int y)  { -	if (inBounds(x, y)) { +	if (inBounds(x, y))  		return m_tiles[x + y * m_width]; -	} else { -		return 0; -	} +	else +		return TileGround;  }  void Level::set(int x, int y, uint8_t tile) @@ -43,24 +42,27 @@ void Level::draw(Bitmap& bitmap, int xo, int yo)  			int tx = (x + y) % 2;  			int ty = 0; -			if (tile == NorthSouth) { -				tx = 0; -				ty = 2; -			} else if (tile == EastWest) { -				tx = 1; -				ty = 2; -			} else if (tile == SouthEast) { -				tx = 0; -				ty = 3; -			} else if (tile == SouthWest) { -				tx = 3; -				ty = 3; -			} else if (tile == NorthWest) { -				tx = 1; -				ty = 3; -			} else if (tile == NorthEast) { -				tx = 2; -				ty = 3; +			if (TILE_TYPE(tile) == TileTrack) { +				uint8_t dir = TILE_DATA(tile); +				if (dir == NorthSouth) { +					tx = 0; +					ty = 2; +				} else if (dir == EastWest) { +					tx = 1; +					ty = 2; +				} else if (dir == SouthEast) { +					tx = 0; +					ty = 3; +				} else if (dir == SouthWest) { +					tx = 3; +					ty = 3; +				} else if (dir == NorthWest) { +					tx = 1; +					ty = 3; +				} else if (dir == NorthEast) { +					tx = 2; +					ty = 3; +				}  			}  			bitmap.blit(m_tileSprites, xx, yy, tx * TileSize, ty * TileSize, TileSize, TileSize); @@ -80,17 +82,17 @@ void Level::addVehicle(Train& vehicle)  void Level::toggleTile(int x, int y)  {  	static const auto updateDirection = [&](int xt, int yt) { -		if (get(xt, yt) > 0) -			set(xt, yt, ChooseDirection(*this, xt, yt)); +		if (TILE_TYPE(get(xt, yt)) == TileTrack) +			set(xt, yt, MAKE_TILE(TileTrack, ChooseDirection(*this, xt, yt)));  	};  	if (inBounds(x, y)) {  		auto tile = get(x, y); -		if (tile > 0) -			tile = 0; +		if (TILE_TYPE(tile) == TileTrack) +			tile = TileGround;  		else -			tile = ChooseDirection(*this, x, y); +			tile = MAKE_TILE(TileTrack, ChooseDirection(*this, x, y));  		m_tiles[x + y * m_width] = tile; @@ -101,14 +103,14 @@ void Level::toggleTile(int x, int y)  	}  } -RailDirection ChooseDirection(Level& level, int x, int y) +TrackDirection ChooseDirection(Level& level, int x, int y)  {  	if (!level.inBounds(x, y)) return NorthSouth; -	bool n = level.get(x, y - 1) > 0; -	bool e = level.get(x + 1, y) > 0; -	bool s = level.get(x, y + 1) > 0; -	bool w = level.get(x - 1, y) > 0; +	bool n = TILE_TYPE(level.get(x, y - 1)) == TileTrack; +	bool e = TILE_TYPE(level.get(x + 1, y)) == TileTrack; +	bool s = TILE_TYPE(level.get(x, y + 1)) == TileTrack; +	bool w = TILE_TYPE(level.get(x - 1, y)) == TileTrack;  	if ((n || s) && !(e || w)) {  		return NorthSouth; diff --git a/src/level.h b/src/level.h index e915da3..d24ccb4 100644 --- a/src/level.h +++ b/src/level.h @@ -5,8 +5,26 @@  #include <cstring>  #include <vector> +#define TILE_TYPE(x) ((x)&0xf) +#define TILE_DATA(x) (x >> 4 & 0xf) +#define MAKE_TILE(t, d) ((((d) & 0xf) << 4) + ((t)&0xf)) +  class Train; +enum TileType : uint8_t { +	TileGround, +	TileTrack +}; + +enum TrackDirection : uint8_t { +	NorthSouth, +	EastWest, +	SouthEast, +	SouthWest, +	NorthWest, +	NorthEast, +}; +  class Level {  public:  	Level(int width, int height, Bitmap& tileSprites); @@ -30,13 +48,4 @@ private:  	std::vector<Train*> m_vehicles;  }; -enum RailDirection { -	NorthSouth = 1, -	EastWest, -	SouthEast, -	SouthWest, -	NorthWest, -	NorthEast, -}; - -RailDirection ChooseDirection(Level& level, int x, int y);
\ No newline at end of file +TrackDirection ChooseDirection(Level& level, int x, int y);
\ No newline at end of file diff --git a/src/train.cpp b/src/train.cpp index 2db57ef..7ce1c2d 100644 --- a/src/train.cpp +++ b/src/train.cpp @@ -10,7 +10,7 @@ void Train::update()  		m_progress = m_next->m_progress;  	} else {  		auto tile = m_level.get(x, y); -		if (tile == 0) +		if (TILE_TYPE(tile) != TileTrack)  			return;  	} @@ -66,24 +66,24 @@ void Train::addVehicle(Train* newTrain)  void Train::findDirection()  { -	auto tile = m_level.get(x, y); +	auto dir = TILE_DATA(m_level.get(x, y));  	if (m_dir == North) { -		if (tile == SouthEast) m_dir = East; -		if (tile == SouthWest) m_dir = West; -		if (tile == EastWest) m_dir = East; +		if (dir == SouthEast) m_dir = East; +		if (dir == SouthWest) m_dir = West; +		if (dir == EastWest) m_dir = East;  	} else if (m_dir == East) { -		if (tile == NorthWest) m_dir = North; -		if (tile == SouthWest) m_dir = South; -		if (tile == NorthSouth) m_dir = South; +		if (dir == NorthWest) m_dir = North; +		if (dir == SouthWest) m_dir = South; +		if (dir == NorthSouth) m_dir = South;  	} else if (m_dir == South) { -		if (tile == NorthEast) m_dir = East; -		if (tile == NorthWest) m_dir = West; -		if (tile == EastWest) m_dir = West; +		if (dir == NorthEast) m_dir = East; +		if (dir == NorthWest) m_dir = West; +		if (dir == EastWest) m_dir = West;  	} else if (m_dir == West) { -		if (tile == NorthEast) m_dir = North; -		if (tile == SouthEast) m_dir = South; -		if (tile == NorthSouth) m_dir = North; +		if (dir == NorthEast) m_dir = North; +		if (dir == SouthEast) m_dir = South; +		if (dir == NorthSouth) m_dir = North;  	}  } | 
