summaryrefslogtreecommitdiff
path: root/src/train.cpp
blob: 632f0f249d55c442f3f18618984515dda51edc38 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "train.h"

#include "level.h"
#include "bitmap.h"

void Train::update(Level& level)
{
	auto tile = level.get(x, y);
	if (tile == 0)
		return;

	if (m_progress < 1.f) {
		m_progress += m_speed;
		return;
	}

	m_progress = 0.f;

	auto n = level.get(x, y - 1);
	auto e = level.get(x + 1, y);
	auto s = level.get(x, y + 1);
	auto w = level.get(x - 1, y);

	if (m_dir == North) {
		if (n == SouthEast) m_dir = East;
		if (n == SouthWest) m_dir = West;
		y--;
	}
	else if (m_dir == East) {
		if (e == NorthWest) m_dir = North;
		if (e == SouthWest) m_dir = South;
		x++;
	}
	else if (m_dir == South) {
		if (s == NorthEast) m_dir = East;
		if (s == NorthWest) m_dir = West;
		y++;
	}
	else if (m_dir == West) {
		if (w == NorthEast) m_dir = North;
		if (w == SouthEast) m_dir = South;
		x--;
	}

    nextTile();
}

void Train::draw(Bitmap& bitmap, int xo, int yo)
{
	float xi = ((float)x + (float)(m_nextX - x) * m_progress) * 24;
	float yi = ((float)y + (float)(m_nextY - y) * m_progress) * 24;
	int xx = (int)((xi - yi) / 2.f - (float)xo);
	int yy = (int)((xi + yi) / 4.f - (float)yo);
	bitmap.blit(m_sprite, xx, yy, 0, 0, 24, 24);
}

void Train::setPosition(int tx, int ty)
{
    x = tx;
    y = ty;
    m_progress = 0.f;
    nextTile();
}

void Train::nextTile()
{
    if (m_dir == North) { m_nextX = x; m_nextY = y - 1; }
    if (m_dir == East) { m_nextX = x + 1; m_nextY = y; }
    if (m_dir == South) { m_nextX = x; m_nextY = y + 1; }
    if (m_dir == West) { m_nextX = x - 1; m_nextY = y; }
}