Skip to content

Commit a40a71d

Browse files
committed
Using SkV2
1 parent 1e3c043 commit a40a71d

File tree

5 files changed

+41
-34
lines changed

5 files changed

+41
-34
lines changed

src/arena/logic/Arena.cpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ namespace arena::logic {
1111
auto princess_tower = entity_data_manager.getEntityDataByName("PrincessTower");
1212
auto king_tower = entity_data_manager.getEntityDataByName("KingTower");
1313

14-
add_arena_tower(princess_tower, "princess", "blue", this->blue_side_tower_skin, 171, 788);
15-
add_arena_tower(princess_tower, "princess", "blue", this->blue_side_tower_skin, 548, 788);
16-
add_arena_tower(king_tower, "king", "blue", this->blue_side_tower_skin, 360, 875);
14+
add_arena_tower(princess_tower, "princess", "blue", this->blue_side_tower_skin, SkV2{171, 788});
15+
add_arena_tower(princess_tower, "princess", "blue", this->blue_side_tower_skin, SkV2{548, 788});
16+
add_arena_tower(king_tower, "king", "blue", this->blue_side_tower_skin, SkV2{360, 875});
1717

18-
add_arena_tower(princess_tower, "princess", "red", this->red_side_tower_skin, 171, 262);
19-
add_arena_tower(princess_tower, "princess", "red", this->red_side_tower_skin, 548, 262);
20-
add_arena_tower(king_tower, "king", "red", this->red_side_tower_skin, 360, 167);
18+
add_arena_tower(princess_tower, "princess", "red", this->red_side_tower_skin, SkV2{171, 262});
19+
add_arena_tower(princess_tower, "princess", "red", this->red_side_tower_skin, SkV2{548, 262});
20+
add_arena_tower(king_tower, "king", "red", this->red_side_tower_skin, SkV2{360, 167});
2121
}
2222

23-
void Arena::add_arena_tower(pEntityData entity_data, std::string character, std::string team_side, TowerSkin tower_skin, int x, int y)
23+
void Arena::add_arena_tower(pEntityData entity_data, std::string character, std::string team_side, TowerSkin tower_skin, SkV2 position)
2424
{
2525
auto result = try_get_arena_tower_path(character, team_side, tower_skin);
2626
if (!result.has_value()) throw std::exception(result.error().c_str());
2727
auto building_instance_result = Entity::create(entity_data, result.value());
2828
if (!building_instance_result.has_value()) throw std::exception(building_instance_result.error().c_str());
2929
pEntity building = std::make_shared<Entity>(building_instance_result.value());
30-
building->setPosition(x, y);
30+
building->setPosition(position);
3131
this->ground_entities.push_back(building);
3232
}
3333

@@ -71,15 +71,18 @@ namespace arena::logic {
7171

7272
bool Arena::try_add_character(pEntity character)
7373
{
74+
SkV2 character_position = character->position;
7475
for (auto entity : this->ground_entities) {
75-
double distance = sqrt(pow(entity->x - character->x, 2) + pow(entity->y - character->y, 2)) * 32;
76+
SkV2 entity_position = entity->position;
77+
double distance = sqrt(pow(entity_position.x - character_position.x, 2) + pow(entity_position.y - character_position.y, 2)) * 32;
7678
bool inside_bounds = distance <= (entity->entity_data->getCollisionRadius() * Global::scale) + (character->entity_data->getCollisionRadius() * Global::scale);
7779
spdlog::debug("CurrentName:{}|Name:{}|Distance:{}|Inside_Bounds:{}", character->entity_data->getName(), entity->entity_data->getName(), distance, inside_bounds);
7880
if (inside_bounds)
7981
return false;
8082
}
8183
for (auto entity : this->air_entities) {
82-
double distance = sqrt(pow(entity->x - character->x, 2) + pow(entity->y - character->y, 2)) * 32;
84+
SkV2 entity_position = entity->position;
85+
double distance = sqrt(pow(entity_position.x - character_position.x, 2) + pow(entity_position.y - character_position.y, 2)) * 32;
8386
bool inside_bounds = distance <= (entity->entity_data->getCollisionRadius() * 4 * Global::scale) + (character->entity_data->getCollisionRadius() * Global::scale);
8487
spdlog::debug("CurrentName:{}|Name:{}|Distance:{}|Inside_Bounds:{}", character->entity_data->getName(), entity->entity_data->getName(), distance, inside_bounds);
8588
if (inside_bounds)
@@ -98,12 +101,12 @@ namespace arena::logic {
98101
{
99102
std::sort(this->ground_entities.begin(), this->ground_entities.end(), [](const pEntity& entity_1, const pEntity& entity_2) -> bool
100103
{
101-
return entity_1->y < entity_2->y;
104+
return entity_1->position.y < entity_2->position.y;
102105
}
103106
);
104107
std::sort(this->air_entities.begin(), this->air_entities.end(), [](const pEntity& entity_1, const pEntity& entity_2) -> bool
105108
{
106-
return entity_1->y < entity_2->y;
109+
return entity_1->position.y < entity_2->position.y;
107110
}
108111
);
109112
for (auto& entity : this->ground_entities) {

src/arena/logic/Arena.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace arena::logic {
4242
int get_height() { return this->canvas.get_height(); }
4343
private:
4444
Arena(ArenaType arena_type, TowerSkin blue_side, TowerSkin red_side, Canvas canvas);
45-
void add_arena_tower(pEntityData entity_data, std::string character, std::string team_side, TowerSkin tower_skin, int x, int y);
45+
void add_arena_tower(pEntityData entity_data, std::string character, std::string team_side, TowerSkin tower_skin, SkV2 position);
4646
tl::expected<Image, std::string> try_get_arena_tower_path(std::string character, std::string team_side, TowerSkin tower_skin);
4747

4848
std::vector<pEntity> air_entities;

src/arena/logic/Entity.cpp

+17-13
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ namespace arena::logic {
3131
this->rect = SkRect::MakeXYWH(0, 0, this->size.x, this->size.y);
3232
}
3333

34-
void Entity::setPosition(int x, int y)
34+
void Entity::setPosition(SkV2 position)
3535
{
36-
this->x = x;
37-
this->y = y;
38-
this->rect = SkRect::MakeXYWH(this->x - (this->size.x / 2), this->y - (this->size.y / 2), this->size.x, this->size.y);
36+
this->position = position;
37+
this->rect = SkRect::MakeXYWH(position.x - (this->size.x / 2), position.y - (this->size.y / 2), this->size.x, this->size.y);
3938

4039
int entity_scale = this->entity_data->getScale();
4140

@@ -51,16 +50,21 @@ namespace arena::logic {
5150

5251
Random& random = Random::get_instance();
5352
for (auto& spawn_entity : this->spawn_entities) {
54-
spawn_entity->setPosition(
55-
random.random_int_from_interval(
56-
this->rect.fLeft + (entity_image_width / 2),
57-
this->rect.fRight - (entity_image_width / 2)
53+
SkV2 spawn_position = SkV2{
54+
static_cast<float>(
55+
random.random_int_from_interval(
56+
this->rect.fLeft + (entity_image_width / 2),
57+
this->rect.fRight - (entity_image_width / 2)
58+
)
5859
),
59-
random.random_int_from_interval(
60-
this->rect.fTop + (entity_image_height / 2),
61-
this->rect.fBottom - (entity_image_height / 2)
60+
static_cast<float>(
61+
random.random_int_from_interval(
62+
this->rect.fTop + (entity_image_height / 2),
63+
this->rect.fBottom - (entity_image_height / 2)
64+
)
6265
)
63-
);
66+
};
67+
spawn_entity->setPosition(spawn_position);
6468
}
6569
}
6670

@@ -79,7 +83,7 @@ namespace arena::logic {
7983
entity_canvas.draw_image(this->image, SkRect{0, 0, this->size.x, this->size.y}, & normal);
8084

8185
canvas.draw_canvas(entity_canvas, this->rect);
82-
86+
8387
for (auto& spawn_entity : this->spawn_entities) {
8488
canvas.draw(*spawn_entity);
8589
}

src/arena/logic/Entity.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ namespace arena::logic {
2323
{
2424
public:
2525
static tl::expected<Entity, std::string> create(pEntityData entity_data, Image image);
26-
void setPosition(int x, int y);
26+
void setPosition(SkV2 position);
2727
void draw(Canvas& canvas);
2828
void draw_shadow(Canvas& canvas);
2929
~Entity();
3030

3131
pEntityData entity_data;
32-
int x;
33-
int y;
34-
32+
SkV2 position;
3533
SkV2 size;
34+
3635
SkRect rect;
3736

3837
Image image;

src/main.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ std::pair<std::vector<json>, json> generate_battle(int image_id, int character_c
216216
);
217217
if (maybeCharacter.has_value()) {
218218
auto character = std::make_shared<arena::logic::Entity>(maybeCharacter.value());
219-
character->setPosition(
220-
Random::get_instance().random_int_from_interval(64, 664),
221-
Random::get_instance().random_int_from_interval(128, 954)
222-
);
219+
SkV2 position = SkV2{
220+
static_cast<float>(Random::get_instance().random_int_from_interval(64, 664)),
221+
static_cast<float>(Random::get_instance().random_int_from_interval(128, 954))
222+
};
223+
character->setPosition(position);
223224
if (arena.try_add_character(character)) {
224225
json character_coco_object = {
225226
{"id", total_character_count + character_id},

0 commit comments

Comments
 (0)