Skip to content

Commit

Permalink
Merge pull request #99 from jonylu7/lastlastfix
Browse files Browse the repository at this point in the history
Lastlastfix
  • Loading branch information
jonylu7 committed Jun 18, 2024
2 parents 440a237 + 4729a41 commit 4805e33
Show file tree
Hide file tree
Showing 31 changed files with 481 additions and 348 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ set(SRC_FILES

${SRC_DIR}/AI/AI.cpp
${SRC_DIR}/AI/AIScripts.cpp
${SRC_DIR}/AI/AIGroupCommander.cpp

${SRC_DIR}/Structure/Structure.cpp
${SRC_DIR}/Structure/WayPoint.cpp
Expand All @@ -101,7 +102,7 @@ set(SRC_FILES
${SRC_DIR}/Scene/MapScene.cpp
${SRC_DIR}/Scene/TutorialScene.cpp
${SRC_DIR}/Scene/SandBoxScene.cpp

${SRC_DIR}/Avatar/Avatar.cpp
${SRC_DIR}/Avatar/PathUtility.cpp
${SRC_DIR}/Avatar/Moving.cpp
Expand All @@ -121,6 +122,8 @@ set(SRC_FILES
${SRC_DIR}/Mechanics/CursorSelection.cpp
${SRC_DIR}/Mechanics/AvatarManager.cpp
${SRC_DIR}/Mechanics/UnitManager.cpp
${SRC_DIR}/Mechanics/BuiltStructure.cpp
${SRC_DIR}/Mechanics/NemesisManager.cpp

)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand Down Expand Up @@ -228,9 +231,9 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/Scene/SandBoxScene.hpp


${INCLUDE_DIR}/Enemy/AI.hpp
${INCLUDE_DIR}/Enemy/AIScripts.hpp
${INCLUDE_DIR}/Enemy/AIGroupCommander.hpp
${INCLUDE_DIR}/AI/AI.hpp
${INCLUDE_DIR}/AI/AIScripts.hpp
${INCLUDE_DIR}/AI/AIGroupCommander.hpp


${INCLUDE_DIR}/Core/Context.hpp
Expand Down
Binary file modified assets/.DS_Store
Binary file not shown.
Binary file modified assets/map/.DS_Store
Binary file not shown.
Binary file added assets/map/terraint/map.bin
Binary file not shown.
Binary file added assets/map/terraint/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions assets/map/terraint/map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
MapFormat: 12

RequiresMod: ra

Title: terraint2

Author: Your name here

Tileset: TEMPERAT

MapSize: 12,12

Bounds: 1,1,10,10

Visibility: Lobby

Categories: Conquest

Players:
PlayerReference@Neutral:
Name: Neutral
OwnsWorld: True
NonCombatant: True
Faction: england
PlayerReference@Creeps:
Name: Creeps
NonCombatant: True
Faction: england

Actors:
168 changes: 5 additions & 163 deletions include/AI/AIGroupCommander.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,170 +80,12 @@ class AIGroupCommander {
}
updateOffensiveTroopAttackTarget();
}
void updateDefensiveGroup() {
for (int i = 0; i < static_cast<int>(m_defensiveGroup.size()); i++) {
if (m_defensiveGroup[i]->getHealth()->ifDead()) {
m_defensiveGroup.erase(m_defensiveGroup.begin() + i);
i--;
} else {
autoAttack(m_defensiveGroup[i], AUTO_ATTACK_METHOD);
};
}
std::vector<std::shared_ptr<Avatar>> temp =
m_AIAvatarManager->getAvatarArray();
for (int i = static_cast<int>(temp.size()) - 1; i >= 0; i--) {
if (temp[i]->getAIType() == AI_Type::NONE) {
temp[i]->setAIType(AI_Type::DEFENCE);
m_defensiveGroup.push_back(temp[i]);
}
}
}
void addTroopToOffensiveGroup(std::shared_ptr<Avatar> unit) {
auto it =
std::find_if(m_offensiveGroup.begin(), m_offensiveGroup.end(),
[](const std::vector<std::shared_ptr<Avatar>> &group) {
return group.size() < GROUP_SIZE;
});
unit->setAIType(AI_Type::ATTACK);
if (it != m_offensiveGroup.end()) {
it->push_back(unit);
} else {
m_offensiveGroup.push_back(std::vector<std::shared_ptr<Avatar>>());
m_offensiveGroup.back().push_back(unit);
}
}
void autoAttack(std::shared_ptr<Avatar> unit, int method) {
switch (method) {
case 1:
for (auto i :
m_PlayerUnitManager->getAvatarManager()->getAvatarArray()) {
if (i->getDistance(unit->getCurrentLocationInCell()) <=
AUTO_FIND_RANGE) {
// attack
m_AIAvatarManager->assignAttackOrderToAvatar(
unit, i->getCurrentLocationInCell(), HouseType::ENEMY);
return;
}
}
break;
case 2:
glm::vec2 targetCell = m_Map->findEnemyInRange(
AUTO_FIND_RANGE, unit->getCurrentLocationInCell(),
HouseType::ENEMY);
if (targetCell.x < 0) {
return;
}
// attack
m_AIAvatarManager->assignAttackOrderToAvatar(unit, targetCell,
HouseType::ENEMY);
break;
}
}
void updateOffensiveTroopAttackTarget() {
for (int i = 0; i < static_cast<int>(m_offensiveGroup.size()); i++) {
auto &currentGroup = m_offensiveGroup[i];
printf("(updateOffensiveTroopAttackTarget)groupSize : %d\n",
static_cast<int>(currentGroup.size()));
if (!currentGroup.empty()) {
if (!m_AIAvatarManager->ifAvatarHasNemesis(
currentGroup.front())) {
// find new target
glm::vec2 targetCell =
findTargetForOffensiveUnit(currentGroup.front());
if (targetCell.x < 0) {
printf(
"(updateOffensiveTroopAttackTarget) No Target\n");
break;
}
// attack
for (int j = 0; j < static_cast<int>(currentGroup.size());
j++) {
if (currentGroup[j]->getHealth()->ifDead()) {
currentGroup.erase(currentGroup.begin() + j);
j--;
} else {
m_AIAvatarManager->assignAttackOrderToAvatar(
currentGroup[j], targetCell, HouseType::ENEMY);
}
}
} else {
glm::vec2 targetCell =
m_AIAvatarManager->getAvatarNemesisCell(
currentGroup.front());
printf("(updateOffensiveTroopAttackTarget)AvatarHasNemesis "
": {%d,%d}\n",
static_cast<int>(targetCell.x),
static_cast<int>(targetCell.y));
}
} else {
m_offensiveGroup.erase(m_offensiveGroup.begin() + i);
}
}
}
void updateDefensiveGroup();
void addTroopToOffensiveGroup(std::shared_ptr<Avatar> unit);
void autoAttack(std::shared_ptr<Avatar> unit, int method);
void updateOffensiveTroopAttackTarget();

glm::vec2 findTargetForOffensiveUnit(std::shared_ptr<Avatar> unit) {
glm::vec2 targetCell = {-10.f, -10.f};
// issue: empty group should delete before this
if (static_cast<int>(m_offensiveGroup.size()) >=
static_cast<int>(m_PlayerUnitManager->getAvatarManager()
->getAvatarArray()
.size()) &&
!m_PlayerUnitManager->getStructureManager()
->getStructureArray()
->getBuiltStructureArray()
.empty()) {
if (!m_PlayerUnitManager->getStructureManager()
->getStructureArray()
->getBuiltStructureArray()
.empty()) {
targetCell = m_PlayerUnitManager->getStructureManager()
->getStructureArray()
->getBuiltStructureArray()
.front()
->getTopRightCell();
} else {
return targetCell;
}
for (auto i : m_PlayerUnitManager->getStructureManager()
->getStructureArray()
->getBuiltStructureArray()) {
if (unit->getDistance(i->getTopRightCell()) <
unit->getDistance(targetCell)) {
targetCell = i->getTopRightCell();
}
}
} else {
if (!m_PlayerUnitManager->getAvatarManager()
->getAvatarArray()
.empty()) {
targetCell = m_PlayerUnitManager->getAvatarManager()
->getAvatarArray()
.front()
->getCurrentLocationInCell();
}
for (auto i :
m_PlayerUnitManager->getAvatarManager()->getAvatarArray()) {
if (i->getDistance(unit->getCurrentLocationInCell()) <
unit->getDistance(targetCell)) {
// attack
if (m_Map->getTileByCellPosition(targetCell)
->getAvatars()
.size() > 0) {
if (m_AIAvatarManager->ifAvatarHasNemesis(
m_Map->getTileByCellPosition(targetCell)
->getAvatars()
.front())) {
continue;
}
}
targetCell = i->getCurrentLocationInCell();
}
}
}
printf("(findTargetForOffensiveUnit) TargetCell : {%d,%d}\n",
targetCell.x, targetCell.y);
return targetCell;
}
glm::vec2 findTargetForOffensiveUnit(std::shared_ptr<Avatar> unit);
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_AIGROUPCOMMANDER_HPP
13 changes: 5 additions & 8 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "Display/SpriteSheetAnimation.hpp"
#include "Map/MapUtility.hpp"
#include "Mechanics/GameObjectID.hpp"
#include "Selectable.hpp"
#include "Unit/Huntable.hpp"
#include "Unit/Selectable.hpp"
#include "Util/Image.hpp"
enum class AI_Type{NONE,DEFENCE,ATTACK};
enum class AI_Type { NONE, DEFENCE, ATTACK };
class Avatar : public Util::GameObject, public Selectable, public Huntable {

public:
Expand Down Expand Up @@ -62,12 +62,9 @@ class Avatar : public Util::GameObject, public Selectable, public Huntable {

void DrawAvatar();

void setAIType(AI_Type type){
m_aiType=type;
}
AI_Type getAIType(){
return m_aiType;
}
void setAIType(AI_Type type) { m_aiType = type; }
AI_Type getAIType() { return m_aiType; }

public:
GameObjectID getID() { return m_ID; }

Expand Down
1 change: 1 addition & 0 deletions include/Cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CursorClass {

DrawCursorSelectionRegion(&start_pos, &end_pos, ImGuiMouseButton_Left);
Util::Transform trans2;
trans2.scale = {1, 1};
trans2.translation = MapUtil::PositionStickToGrid(
MapUtil::ScreenToGlobalCoord(Util::Input::GetCursorPosition()));
m_testdraw.DrawUsingCamera(trans2, 1);
Expand Down
5 changes: 5 additions & 0 deletions include/Display/SpriteSheet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class SpriteSheet {
m_SpriteSheet[index]->Init();
m_SpriteSheet[index]->DrawUsingCamera(trans, zIndex);
}
void InitSpritByIndex(int index) { m_SpriteSheet[index]->Init(); }

void DrawSpriteWithoutChange(int index, Util::Transform trans, int zIndex) {
m_SpriteSheet[index]->DrawUsingCamera(trans, zIndex);
}

glm::vec2 getSpriteSize() {
return glm::vec2(m_SpriteWidth, m_SpriteHeight);
Expand Down
3 changes: 2 additions & 1 deletion include/Map/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ class MapClass : public Core::Drawable {
return NullPos;
}


int getWidth() { return m_MapWdith; }
int getHeight() { return m_MapHeight; }

protected:
void InitGrid();

private:
const glm::vec2 m_MapTransShift = {10, -10};
const glm::vec2 m_MapTransShift = {20, 20};
std::vector<std::shared_ptr<Util::ImageArray>> m_Images;
std::unordered_map<std::string, std::vector<glm::vec2>> m_Tiles;
unsigned int m_MapWdith = 0;
Expand Down
14 changes: 8 additions & 6 deletions include/Map/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "Avatar/Avatar.hpp"
#include "Mechanics/GameObjectID.hpp"
#include "Selectable.hpp"
#include "Unit/Selectable.hpp"

#include "Display/SpriteSheet.hpp"
#include "Structure/Structure.hpp"
Expand Down Expand Up @@ -52,7 +52,7 @@ class TileClass {
void pushAvatars(std::shared_ptr<Avatar> avatar) {
avatar->getMoving()->setStandingCorner(m_Avatars.size());
m_Avatars.push_back(avatar);
// if (m_Avatars.size() == 4) {
// if (m_Avatars.size() >= 5) {
// setWalkable(false);
//}
}
Expand All @@ -74,10 +74,12 @@ class TileClass {
auto it = m_Avatars.begin() + i;
m_Avatars.erase(it);
i--;
// fix this
if (m_Avatars.size() < 4) {
setWalkable(true);
}


// if (m_Avatars.size() < 4 && m_TerrainBuildable) {
// setWalkable(true);
// }

if (m_Avatars.size() == 0) {
setBuildable(true);
}
Expand Down
Loading

0 comments on commit 4805e33

Please sign in to comment.