-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJewelBoard.cpp
137 lines (110 loc) · 2.72 KB
/
JewelBoard.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// JewelBoard.cpp
// SDL Game Programming Book
//
// Created by shaun mitchell on 26/03/2013.
// Copyright (c) 2013 shaun mitchell. All rights reserved.
//
#include "JewelBoard.h"
#include "JewelObject.h"
#include "TextureManager.h"
#include "InputHandler.h"
#include "Game.h"
#include <model/JewelSwap.h>
#include "utils/utils.h"
#include "utils/log.h"
#include <assert.h>
#include <SDL_events.h>
JewelBoard::JewelBoard() : BoardObject(),
m_model(*this),
m_offset(350, 100),
m_bottomDown(m_offset + Vector2D(JewelObject::WIDTH, JewelObject::HEIGHT) * BoardPos::SIZE ),
m_drag(*this)
{
TheTextureManager::Instance()->load("assets/jewels.png", "jewels", TheGame::Instance()->getRenderer());
createInitialJewelsBoard();
}
void JewelBoard::createInitialJewelsBoard()
{
for (unsigned i = 0 ; i <= BoardPos::BoardPos::SIZE ; ++i)
{
for (unsigned j = 0 ; j < BoardPos::BoardPos::SIZE ; ++j)
{
Jewel &jewel = m_model.getJewel(BoardPos(j, i));
JewelObject *jo = new JewelObject(jewel);
m_jewels[i][j] = jo;
jo->getPixel().setX(m_offset.getX() + jo->getWidth() * j);
jo->getPixel().setY(m_offset.getY() + jo->getHeight() * i);
}
}
}
void JewelBoard::kill(BoardPos pos)
{
}
void JewelBoard::load(std::unique_ptr<LoaderParams> const &pParams)
{
}
/******* Model *******/
#ifdef NDEBUG
inline void assertBoardPos(BoardPos const)
{
}
#else
inline void assertBoardPos(BoardPos const pos)
{
if (!pos.isValid(true))
throw std::runtime_error("BoardPos " + dani::toString(pos) + "is not valid");
}
#endif
JewelObject& JewelBoard::getJewel(BoardPos const pos)
{
assertBoardPos(pos);
return *m_jewels[pos.m_row][pos.m_col];
}
JewelObject const& JewelBoard::getJewel(BoardPos const pos) const
{
assertBoardPos(pos);
return *m_jewels[pos.m_row][pos.m_col];
}
/******* Model *******/
void JewelBoard::draw()
{
forAll([&](JewelObject &jewel)
{
if (jewel.getPixel().getY() + jewel.getHeight() >= m_offset.getY())
jewel.draw();
});
}
BoardPos JewelBoard::getJewelAt(Vector2D const &v) const
{
if (!v.isInside(m_offset, m_bottomDown))
return BoardPos();
return BoardPos((v.getX() - m_offset.getX()) / JewelObject::WIDTH,
(v.getY() - m_offset.getY()) / JewelObject::HEIGHT);
}
bool JewelBoard::swap(BoardPos const pos1, BoardPos const pos2)
{
JewelSwap sw(m_model);
sw.setPositions(pos1, pos2);
return sw.run();
}
void JewelBoard::update()
{
bool clicked = TheInputHandler::Instance()->getMouseButtonState(LEFT);
if (clicked)
m_drag.drag();
else
m_drag.drop();
forAll([&](JewelObject &jewel)
{
jewel.update();
});
}
void JewelBoard::clean()
{
forAll([&](JewelObject &jewel)
{
jewel.clean();
});
BoardObject::clean();
}