Skip to content

Commit aa9449d

Browse files
committed
增加进度条 瓦片地图 解决故障等
1 parent dfd837c commit aa9449d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+653
-420
lines changed

application/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(chapter-00)
12
add_subdirectory(chapter-01)
23
add_subdirectory(chapter-02)
34
add_subdirectory(chapter-03)
@@ -24,4 +25,7 @@ add_subdirectory(chapter-23)
2425
add_subdirectory(chapter-24)
2526
add_subdirectory(chapter-25)
2627
add_subdirectory(chapter-26)
28+
add_subdirectory(chapter-27)
2729
add_subdirectory(chapter-28)
30+
add_subdirectory(chapter-29)
31+
add_subdirectory(chapter-30)

application/chapter-01/main.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,43 @@
22
#include <SFML/Graphics/RenderWindow.hpp>
33
#include <Application.h>
44
#include <Entity.h>
5+
#include <NameHolder.h>
56

67
using namespace std;
78

89
std::vector<EntityPointer> units;
910

10-
class Unit : public Entity
11+
class Unit : public Entity, public NameHolder
1112
{
1213
public:
1314
Unit(const sf::Vector2f &size = sf::Vector2f()):
1415
Entity(size, CornerStyle())
1516
{
1617
setOutlineColor(sf::Color::Yellow);
1718
setOutlineThickness(1.0f);
18-
setBackgroundColor(sf::Color::Green);
19+
setBackgroundColor(sf::Color(rand() % 250, rand() % 250, rand() % 250));
1920
}
2021
private:
2122
void onMouseEnter()
2223
{
23-
setBackgroundColor(sf::Color::Red);
24+
setOutlineColor(sf::Color::Red);
2425
}
2526

2627
void onMouseExit()
2728
{
28-
setBackgroundColor(sf::Color::Green);
29+
setOutlineColor(sf::Color::Green);
2930
}
3031

3132
void onMousePressed(sf::Mouse::Button button)
3233
{
3334
(void)button;
34-
setBackgroundColor(sf::Color::White);
35+
setOutlineColor(sf::Color::White);
3536
}
3637

3738
void onMouseReleased(sf::Mouse::Button button)
3839
{
3940
(void)button;
40-
setBackgroundColor(sf::Color::Black);
41+
setOutlineColor(sf::Color::Black);
4142

4243
for (auto unit : units)
4344
unit->setRotate(unit->getRotate() + 15.0f);
@@ -57,7 +58,7 @@ class Unit : public Entity
5758
int main()
5859
{
5960
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(800, 600), "Chapter-1",
60-
sf::Style::Close);
61+
sf::Style::Close);
6162
window->setVerticalSyncEnabled(true);
6263

6364
auto app = Application::getInstance();
@@ -66,15 +67,15 @@ int main()
6667

6768
auto object = std::make_shared<Object>();
6869

69-
int xoffset = 60;
70-
int yoffset = 10;
70+
int xoffset = 0;
71+
int yoffset = 0;
7172

7273
for (int i = 0; i < 10; i++)
7374
for (int j = 0; j < 18; j++) {
7475
auto item = std::make_shared<Unit>(sf::Vector2f(100, 36));
76+
item->setName(std::to_string(i) + "," + std::to_string(j));
7577
units.push_back(std::dynamic_pointer_cast<Entity>(item));
7678
item->setPosition(xoffset + i * 120, yoffset + j * 40);
77-
item->setRotate(30.0f);
7879
object->addChild(item);
7980
}
8081

application/chapter-03/main.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,38 @@ std::shared_ptr<Text> createText(std::shared_ptr<sf::Font> font)
2020
return text;
2121
}
2222

23+
class MyTileVisitor : public TileVisitor
24+
{
25+
public:
26+
void visit(uint32_t x, uint32_t y, std::shared_ptr<Tile> tile) override
27+
{
28+
(void)x, void(y);
29+
tile->setScale(0.95f, 0.95f);
30+
tile->setFillColor(sf::Color(rand() % 250, rand() % 250, rand() % 250));
31+
tile->setOutlineColor(sf::Color(rand() % 250, rand() % 250, rand() % 250));
32+
}
33+
};
34+
2335
int main()
2436
{
25-
auto size = sf::Vector2f(800, 600);
37+
auto size = sf::Vector2f(960, 720);
2638
auto setting = sf::ContextSettings();
2739
setting.antialiasingLevel = 12;
2840
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-3",
29-
sf::Style::Close, setting);
41+
sf::Style::Close, setting);
3042
window->setVerticalSyncEnabled(true);
3143

3244
auto app = Application::getInstance();
3345
app->setBackgroundColor(sf::Color::Blue);
3446
app->setWindow(window);
3547

3648
auto tileMap = TileMap::createTileMap(TileMapType_Hex);
37-
tileMap->init(23, 15, 24);
49+
tileMap->init(21, 14, 64);
3850
tileMap->setTextVisible(true);
3951

52+
auto visitor = std::make_shared<MyTileVisitor>();
53+
tileMap->accept(visitor.get());
54+
4055
auto list = tileMap->getAdjacentTileByTileIndex(8, 5);
4156
for (auto itr = list.begin(); itr != list.end(); itr++)
4257
tileMap->getTileByIndex(itr->x, itr->y)->setVisible(false);
@@ -46,7 +61,7 @@ int main()
4661

4762
auto text = createText(font);
4863
text->setText(L"六边地图", false);
49-
text->setPosition(80, 30);
64+
text->setPosition(30, 30);
5065
tileMap->addChild(text);
5166

5267
app->execute(tileMap);

application/chapter-04/main.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,27 @@ std::shared_ptr<Text> createText(std::shared_ptr<sf::Font> font)
3636

3737
void clickedTile(int32_t x, int32_t y)
3838
{
39-
auto tile = tileMap->getTileByIndex(x,y);
40-
if(tile)
39+
auto tile = tileMap->getTileByIndex(x, y);
40+
if (tile)
4141
tile->setFillColor(sf::Color::Red);
4242
}
4343

44+
class MyTileVisitor : public TileVisitor
45+
{
46+
public:
47+
void visit(uint32_t x, uint32_t y, std::shared_ptr<Tile> tile) override
48+
{
49+
(void)x, void(y);
50+
tile->setFillColor(sf::Color(rand() % 250, rand() % 250, rand() % 250));
51+
tile->setOutlineColor(sf::Color(rand() % 250, rand() % 250, rand() % 250));
52+
}
53+
};
54+
4455
int main()
4556
{
46-
auto size = sf::Vector2f(800, 600);
57+
auto size = sf::Vector2f(960, 720);
4758
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-4",
48-
sf::Style::Close);
59+
sf::Style::Close);
4960
window->setVerticalSyncEnabled(true);
5061

5162
auto app = Application::getInstance();
@@ -56,10 +67,13 @@ int main()
5667

5768
tileMap = TileMap::createTileMap(TileMapType_Hex);
5869
tileMap->tileClicked.connect(clickedTile);
59-
tileMap->init(48, 36, 32);
70+
tileMap->init(48, 36, 64);
6071
tileMap->setMessageReceived(true);
6172
tileMap->setTextVisible(false);
6273

74+
auto visitor = std::make_shared<MyTileVisitor>();
75+
tileMap->accept(visitor.get());
76+
6377
object->addChild(tileMap);
6478

6579
std::vector<std::string> units = {
@@ -108,7 +122,7 @@ int main()
108122

109123
std::vector<sf::Color> spriteColor = {sf::Color::White, sf::Color::Red, sf::Color::Green, sf::Color::Blue};
110124

111-
for(int i = 0; i < 30; i++) {
125+
for (int i = 0; i < 30; i++) {
112126
auto sprite = createSprite(units.at(rand() % units.size()), rand() % 800, rand() % 600);
113127
int index = rand() % modifers.size();
114128
sprite->addTexture(*Application::getInstance()->loadTexture(modifers[index]));
@@ -123,7 +137,7 @@ int main()
123137

124138
auto text = createText(font);
125139
text->setText(L"精灵", false);
126-
text->setPosition(80, 30);
140+
text->setPosition(30, 30);
127141
object->addChild(text);
128142

129143
app->execute(object);

application/chapter-05/main.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
#include <Scene.h>
77
#include <Sprite.h>
88

9+
auto screenSize = sf::Vector2f(800, 640);
10+
911
using namespace std;
1012

11-
std::shared_ptr<Sprite> createSprite(const std::string &image, float x, float y)
13+
std::shared_ptr<Sprite> createSprite(const std::string &image)
1214
{
1315
auto sprite = std::make_shared<Sprite>();
1416
sprite->setSpriteColor(sf::Color::Yellow);
15-
sprite->setPosition(x, y);
1617
auto texture = Application::getInstance()->loadTexture(image);
1718
sprite->addTexture(*texture);
19+
20+
sf::Vector2f size(texture->getSize().x, texture->getSize().y);
21+
auto position = Entity::adjustPosition(sf::FloatRect(sf::Vector2f(), screenSize), size, HMode_Center, VMode_Center);
22+
sprite->setPosition(position);
1823
return sprite;
1924
}
2025

@@ -73,8 +78,7 @@ std::shared_ptr<Text> createText(std::shared_ptr<sf::Font> font)
7378

7479
int main()
7580
{
76-
auto size = sf::Vector2f(800, 640);
77-
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-5",
81+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(screenSize.x, screenSize.y), "Chapter-5",
7882
sf::Style::Close);
7983
window->setVerticalSyncEnabled(true);
8084

@@ -88,7 +92,7 @@ int main()
8892
auto background = Application::getInstance()->loadTexture("../resource/images/background.png");
8993
scene->setBackground(*background);
9094

91-
auto sprite = createSprite("../resource/images/plane.png", 400, 320);
95+
auto sprite = createSprite("../resource/images/plane.png");
9296
scene->addMessageListener(std::make_shared<SpriteMessageListener>(sprite));
9397
scene->addChild(sprite);
9498

@@ -97,7 +101,7 @@ int main()
97101

98102
auto text = createText(font);
99103
text->setText(L"消息监听", false);
100-
text->setPosition(80, 30);
104+
text->setPosition(30, 30);
101105
scene->addChild(text);
102106

103107
app->execute(scene);

application/chapter-06/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main()
6969

7070
auto text = createText(font);
7171
text->setText(L"动画", false);
72-
text->setPosition(80, 30);
72+
text->setPosition(30, 30);
7373
scene->addChild(text);
7474

7575
auto sceneManager = std::make_shared<SceneManager>();

application/chapter-07/main.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55
#include <Scene.h>
66
#include <Text.h>
77
#include <MovingSprite.h>
8+
#include <Util.h>
89

10+
auto screenSize = sf::Vector2f(960, 640);
911
#define BULLET_SPEED sf::Vector2f(0, -160.0f)
1012

1113
using namespace std;
1214

1315
std::shared_ptr<Scene> scene;
1416

15-
std::shared_ptr<Sprite> createSprite(const std::string &image, float x, float y)
17+
std::shared_ptr<Sprite> createSprite(const std::string &image, VMode vMode)
1618
{
17-
auto sprite = std::make_shared<MovingSprite>();
18-
sprite->setPosition(x, y);
19+
auto sprite = std::make_shared<Sprite>();
1920
auto texture = Application::getInstance()->loadTexture(image);
2021
sprite->addTexture(*texture);
21-
auto size = texture->getSize();
22-
sprite->setSize(size.x, size.y);
22+
23+
sf::Vector2f size(texture->getSize().x, texture->getSize().y);
24+
auto position = Entity::adjustPosition(sf::FloatRect(sf::Vector2f(), screenSize), size, HMode_Center, vMode);
25+
sprite->setPosition(position);
2326
return sprite;
2427
}
2528

@@ -43,18 +46,18 @@ class SpriteMessageListener : public MessageListener
4346
return true;
4447
} else if (event.key.code == sf::Keyboard::Key::D) {
4548
if (sprite->getPosition().x + sprite->getSize().x <
46-
Application::getInstance()->getWindow()->getSize().x - 5)
49+
Application::getInstance()->getWindow()->getSize().x - 5)
4750
sprite->move(5, 0);
4851
return true;
4952
} else if (event.key.code == sf::Keyboard::Key::Space) {
5053
auto texture = Application::getInstance()->loadTexture("../resource/images/bullet.png");
5154
auto bullet = std::make_shared<MovingSprite>();
5255
bullet->addTexture(*texture);
5356

54-
auto position = sprite->getPosition();
57+
auto position = getRectCenter(sprite->getBoundingBox());
5558
position.y -= sprite->getSize().y;
5659

57-
bullet->setPosition(position);
60+
bullet->setCenter(position);
5861
bullet->setVelocity(BULLET_SPEED);
5962
scene->addChild(bullet);
6063
return true;
@@ -69,9 +72,8 @@ class SpriteMessageListener : public MessageListener
6972

7073
int main()
7174
{
72-
auto size = sf::Vector2f(960, 640);
73-
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-7",
74-
sf::Style::Close);
75+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(screenSize.x, screenSize.y), "Chapter-7",
76+
sf::Style::Close);
7577
window->setVerticalSyncEnabled(true);
7678

7779
auto app = Application::getInstance();
@@ -84,19 +86,19 @@ int main()
8486
auto background = Application::getInstance()->loadTexture("../resource/images/background.png");
8587
scene->setBackground(*background);
8688

87-
auto sprite = createSprite("../resource/images/plane.png", size.x * 0.5f, 600);
89+
auto sprite = createSprite("../resource/images/plane.png", VMode_Bottom);
8890
scene->addMessageListener(std::make_shared<SpriteMessageListener>(sprite));
8991
scene->addChild(sprite);
9092

91-
auto enemy = createSprite("../resource/images/enemy1.png", size.x * 0.5f, 40);
93+
auto enemy = createSprite("../resource/images/enemy1.png", VMode_Top);
9294
scene->addChild(enemy);
9395

9496
auto font = std::make_shared<sf::Font>();
9597
font->loadFromFile("../resource/FZYTK.TTF");
9698

9799
auto text = scene->createToastText();
98100
text->setText(L"游戏场景", false);
99-
text->setPosition(80, 30);
101+
text->setPosition(30, 30);
100102
scene->addChild(text);
101103

102104
auto sceneManager = std::make_shared<SceneManager>();

0 commit comments

Comments
 (0)