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
645c9bc0
Commit
645c9bc0
authored
6 months ago
by
Max Cherris
Browse files
Options
Downloads
Patches
Plain Diff
Add responsive Unitstates for movement
parent
19e1fdec
No related branches found
No related tags found
1 merge request
!15
Merge units into main
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/level.cpp
+2
-2
2 additions, 2 deletions
src/level.cpp
src/unit.cpp
+47
-78
47 additions, 78 deletions
src/unit.cpp
src/unit.hpp
+2
-1
2 additions, 1 deletion
src/unit.hpp
with
51 additions
and
81 deletions
src/level.cpp
+
2
−
2
View file @
645c9bc0
...
...
@@ -128,8 +128,8 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
if
(
selectedUnit
)
{
int
tileX
=
event
.
button
.
x
;
int
tileY
=
event
.
button
.
y
;
int
tileX
=
event
.
button
.
x
/
(
16
*
RENDERING_SCALE
)
;
int
tileY
=
event
.
button
.
y
/
(
16
*
RENDERING_SCALE
)
;
if
(
click_check_right
(
tileX
,
tileY
))
{
...
...
This diff is collapsed.
Click to expand it.
src/unit.cpp
+
47
−
78
View file @
645c9bc0
...
...
@@ -69,97 +69,66 @@ namespace advanced_wars
}
}
void
Unit
::
attack
(
Unit
*
enemy
)
{
secondary_weapon
=
fill_matchupTable
(
0
);
primary_weapon
=
fill_matchupTable
(
1
);
// Zuerst die Tabel für die Waffen der angreifenden Einheit holen
auto
&
attackerSecondaryWeaponTable
=
secondary_weapon
[
this
->
id
];
auto
&
attackerPrimaryWeaponTable
=
primary_weapon
[
this
->
id
];
void
Unit
::
attack
(
Unit
*
enemy
)
{
// Schadenswert für die angreifende Einheit gegen die verteidigende Einheit berechnen
// Es wird die Waffe genommen die mehr Schaden macht
int
offDamage
=
50
;
int
defDamage
=
50
;
int
attackerDamageValue
=
0
;
if
(
attackerSecondaryWeaponTable
.
find
(
enemy
->
id
)
!=
attackerSecondaryWeaponTable
.
end
())
{
attackerDamageValue
=
attackerSecondaryWeaponTable
[
enemy
->
id
];
}
if
(
attackerPrimaryWeaponTable
.
find
(
enemy
->
id
)
!=
attackerPrimaryWeaponTable
.
end
())
{
if
(
attackerDamageValue
<
attackerPrimaryWeaponTable
[
enemy
->
id
])
{
// Here ammo deduction should happen if applicable
attackerDamageValue
=
attackerPrimaryWeaponTable
[
enemy
->
id
];
enemy
->
health
=
enemy
->
health
-
offDamage
;
std
::
cout
<<
"Enemy health:"
<<
enemy
->
health
<<
std
::
endl
;
if
(
enemy
->
health
>
0
)
{
this
->
health
=
this
->
health
-
defDamage
;
std
::
cout
<<
"Health ally:"
<<
this
->
health
<<
std
::
endl
;
}
}
if
(
attackerDamageValue
==
0
)
void
Unit
::
update_position
(
int
posX
,
int
posY
)
{
std
::
cout
<<
"No damage value found for attack from unit "
<<
static_cast
<
int
>
(
id
)
<<
" against unit "
<<
static_cast
<
int
>
(
enemy
->
id
)
<<
std
::
endl
;
calc_state
(
posX
,
posY
);
this
->
x
=
posX
;
this
->
y
=
posY
;
}
else
{
int
offDamage
=
attackerDamageValue
*
(
static_cast
<
float
>
(
health
)
/
max_health
);
enemy
->
health
-=
offDamage
;
enemy
->
health
=
std
::
max
(
0
,
enemy
->
health
);
// Ensuring health is not negative
std
::
cout
<<
"Enemy health after attack: "
<<
enemy
->
health
<<
std
::
endl
;
void
Unit
::
calc_state
(
int
posX
,
int
posY
)
{
// Prüfen, ob der Gegner noch am Leben ist um zurückzuschlagen
if
(
enemy
->
health
>
0
)
{
// Weapon tables for the defender
auto
&
defenderSecondaryWeaponTable
=
secondary_weapon
[
enemy
->
id
];
auto
&
defenderPrimaryWeaponTable
=
primary_weapon
[
enemy
->
id
];
int
currentX
=
this
->
x
;
int
currentY
=
this
->
y
;
int
defenderDamageValue
=
0
;
// Declare outside for later use
int
deltaX
=
currentX
-
posX
;
int
deltaY
=
currentY
-
posY
;
// Determine the damage value for the defender
if
(
defenderSecondaryWeaponTable
.
find
(
id
)
!=
defenderSecondaryWeaponTable
.
end
())
{
defenderDamageValue
=
defenderSecondaryWeaponTable
[
id
];
}
if
(
defenderPrimaryWeaponTable
.
find
(
id
)
!=
defenderPrimaryWeaponTable
.
end
())
{
if
(
defenderDamageValue
<
defenderPrimaryWeaponTable
[
id
])
{
// Deduct ammo for primary weapon, if applicable
defenderDamageValue
=
defenderPrimaryWeaponTable
[
id
];
if
(
deltaY
==
0
)
{
if
(
deltaX
>
0
)
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTLEFT
;
return
;
}
else
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTRIGHT
;
return
;
}
}
// If a valid damage value was determined for retaliation
if
(
defenderDamageValue
>
0
)
{
int
defDamage
=
static_cast
<
int
>
(
defenderDamageValue
*
static_cast
<
float
>
(
enemy
->
health
)
/
enemy
->
max_health
);
this
->
health
-=
defDamage
;
this
->
health
=
std
::
max
(
0
,
this
->
health
);
// Safeguard against negative health
std
::
cout
<<
"Ally health after retaliation: "
<<
this
->
health
<<
std
::
endl
;
}
}
double
bresen
=
deltaX
/
deltaY
;
if
(
bresen
==
0
)
{
if
(
deltaY
<
0
)
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTDOWN
;
return
;
}
else
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTUP
;
return
;
}
}
MatchupTable
Unit
::
fill_matchupTable
(
int
type
)
{
switch
(
type
)
{
case
0
:
break
;
default:
break
;
}
if
(
0
<
bresen
&&
bresen
<
1
)
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTDOWN
;
return
;
}
else
if
(
-
1
<
bresen
&&
bresen
<
0
)
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTUP
;
return
;
}
void
Unit
::
update_position
(
int
posX
,
int
posY
)
{
this
->
x
=
posX
;
this
->
y
=
posY
;
}
void
Unit
::
on_left_click
(
SDL_Event
event
,
std
::
vector
<
Unit
>
&
unitVector
)
...
...
This diff is collapsed.
Click to expand it.
src/unit.hpp
+
2
−
1
View file @
645c9bc0
...
...
@@ -103,6 +103,7 @@ public:
*/
void
calculate_movement
();
void
calc_state
(
int
posX
,
int
posY
);
/*
This function fills the MatchupTable
...
...
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