Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Maul
cpp-project
Commits
58a84720
Commit
58a84720
authored
5 months ago
by
Frederik
Browse files
Options
Downloads
Patches
Plain Diff
Fix EventHandler for Movement and Attack range that got broken by merge
parent
dfc7bff5
No related branches found
No related tags found
1 merge request
!23
Unit movement
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/game/Engine.cpp
+5
-0
5 additions, 0 deletions
src/game/Engine.cpp
src/game/Level.cpp
+98
-31
98 additions, 31 deletions
src/game/Level.cpp
with
103 additions
and
31 deletions
src/game/Engine.cpp
+
5
−
0
View file @
58a84720
...
...
@@ -120,6 +120,11 @@ Spritesheet* Engine::getSpritesheet()
return
m_spritesheet
.
value
();
}
Config
&
Engine
::
getUnitConfig
()
{
return
m_unitConfig
;
}
SDL_Renderer
*
Engine
::
renderer
()
{
return
this
->
m_SDLRenderer
;
...
...
This diff is collapsed.
Click to expand it.
src/game/Level.cpp
+
98
−
31
View file @
58a84720
...
...
@@ -371,7 +371,8 @@ Effect Level::removeEffect(int id)
return
value
;
}
void
Level
::
handleRecruitingEvent
(
Engine
&
engine
,
SDL_Event
&
event
)
{
void
Level
::
handleRecruitingEvent
(
Engine
&
engine
,
SDL_Event
&
event
)
{
switch
(
event
.
type
)
{
...
...
@@ -390,15 +391,19 @@ void Level::handleRecruitingEvent(Engine& engine, SDL_Event& event) {
UnitFaction
u_faction
=
static_cast
<
UnitFaction
>
(
b
.
m_faction
);
UnitId
unit_id
=
m_recruitingMenu
.
getSelectedOption
();
if
(
b
.
check_money
(
500
))
{
if
(
b
.
check_spawn
(
m_units
)){
addUnit
(
Unit
(
b
.
m_x
,
b
.
m_y
,
u_faction
,
unit_id
,
UnitState
::
IDLE
));
if
(
b
.
check_money
(
500
))
{
if
(
b
.
check_spawn
(
m_units
))
{
addUnit
(
Unit
(
b
.
m_x
,
b
.
m_y
,
u_faction
,
unit_id
,
UnitState
::
IDLE
,
engine
.
getUnitConfig
()));
m_state
=
LevelState
::
SELECTING_STATE
;
m_selectedBuilding
=
-
1
;
}
}
}
}}
}
}
//*******************helper functions for event Handling*************************************
...
...
@@ -415,6 +420,9 @@ void Level::handleAttack(std::pair<int, int> tilePos)
Unit
&
attacking
=
m_units
.
at
(
m_selectedUnit
);
Unit
&
defending
=
m_units
.
at
(
targetedUnit
);
if
(
m_attackableUnitIds
.
find
(
targetedUnit
)
!=
m_attackableUnitIds
.
end
())
{
attacking
.
attack
(
defending
);
if
(
attacking
.
m_health
<=
0
)
{
...
...
@@ -425,9 +433,16 @@ void Level::handleAttack(std::pair<int, int> tilePos)
removeUnit
(
targetedUnit
);
}
m_selectedUnit
=
-
1
;
m_showAttackableTiles
=
false
;
m_showReachableTiles
=
false
;
m_state
=
LevelState
::
SELECTING_STATE
;
}
else
{
std
::
cout
<<
"No target in range clicked!"
<<
std
::
endl
;
}
}
else
{
std
::
cout
<<
"No valid target clicked"
<<
std
::
endl
;
}
...
...
@@ -444,10 +459,31 @@ void Level::handleMovement(std::pair<int, int> tilePos)
return
;
}
}
bool
isReachable
=
false
;
for
(
auto
&
pos
:
m_reachableTiles
)
{
if
(
pos
==
tilePos
)
{
isReachable
=
true
;
break
;
}
}
if
(
isReachable
)
{
m_units
.
at
(
m_selectedUnit
).
updatePosition
(
tilePos
.
first
,
tilePos
.
second
);
m_selectedUnit
=
-
1
;
m_showAttackableTiles
=
false
;
m_showReachableTiles
=
false
;
m_state
=
LevelState
::
SELECTING_STATE
;
}
else
{
std
::
cout
<<
"Unglültige Bewegunsposition!"
<<
std
::
endl
;
}
}
void
Level
::
handlePositionMarker
(
Engine
&
engine
,
SDL_Event
&
event
)
{
...
...
@@ -486,6 +522,40 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
(
tilePos
.
second
*
16
+
15
)
*
RENDERING_SCALE
);
if
(
m_selectedUnit
>=
0
)
{
m_reachableTiles
=
calculateMovementRange
(
m_units
.
at
(
m_selectedUnit
));
m_units
.
at
(
m_selectedUnit
).
on_left_click
(
event
);
m_showReachableTiles
=
true
;
std
::
vector
<
Unit
*>
allUnits
;
for
(
auto
&
[
id
,
unit
]
:
m_units
)
{
allUnits
.
push_back
(
&
unit
);
}
std
::
vector
<
Unit
*>
attackableTargets
=
m_units
.
at
(
m_selectedUnit
).
getUnitsInRangeWithDamagePotential
(
allUnits
);
m_attackableTiles
.
clear
();
m_showAttackableTiles
=
true
;
m_attackableUnitIds
.
clear
();
for
(
Unit
*
target
:
attackableTargets
)
{
// Füge die Position jedes angreifbaren Ziels hinzu
m_attackableTiles
.
emplace_back
(
target
->
m_x
,
target
->
m_y
);
// Angreifbaren Einheits-ID setzen
for
(
auto
&
[
id
,
unit
]
:
m_units
)
{
if
(
&
unit
==
target
)
{
m_attackableUnitIds
.
insert
(
id
);
break
;
}
}
}
m_contextMenu
.
setOptions
({
"Move"
,
"Attack"
,
"Info"
,
"Wait"
});
}
else
...
...
@@ -519,6 +589,8 @@ void Level::handleSelectingEvents(Engine& engine, SDL_Event& event)
}
else
{
m_showReachableTiles
=
false
;
m_showAttackableTiles
=
false
;
m_state
=
LevelState
::
SELECTING_STATE
;
}
}
...
...
@@ -537,6 +609,8 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
m_selectedUnit
=
-
1
;
m_selectedBuilding
=
-
1
;
m_state
=
LevelState
::
SELECTING_STATE
;
m_showAttackableTiles
=
false
;
m_showReachableTiles
=
false
;
}
if
(
event
.
key
.
keysym
.
sym
==
SDLK_UP
||
event
.
key
.
keysym
.
sym
==
SDLK_DOWN
)
{
...
...
@@ -580,17 +654,10 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
m_recruitingMenu
.
update
(
(
tilePos
.
first
*
16
+
15
)
*
RENDERING_SCALE
,
(
tilePos
.
second
*
16
+
15
)
*
RENDERING_SCALE
);
m_recruitingMenu
.
setOptions
({
UnitId
::
INFANTERY
,
UnitId
::
MECHANIZED_INFANTERY
,
UnitId
::
RECON
,
UnitId
::
APC
,
UnitId
::
ARTILLERY
,
UnitId
::
ANTI_AIR_TANK
,
UnitId
::
ANTI_AIR_MISSILE_LAUNCHER
,
UnitId
::
ROCKET_ARTILLERY
,
UnitId
::
MEDIUM_TANK
,
UnitId
::
NEO_TANK
,
m_recruitingMenu
.
setOptions
(
{
UnitId
::
INFANTERY
,
UnitId
::
MECHANIZED_INFANTERY
,
UnitId
::
RECON
,
UnitId
::
APC
,
UnitId
::
ARTILLERY
,
UnitId
::
ANTI_AIR_TANK
,
UnitId
::
ANTI_AIR_MISSILE_LAUNCHER
,
UnitId
::
ROCKET_ARTILLERY
,
UnitId
::
MEDIUM_TANK
,
UnitId
::
NEO_TANK
,
UnitId
::
HEAVY_TANK
});
std
::
cout
<<
"no training here"
<<
std
::
endl
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment