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
07726a73
Commit
07726a73
authored
5 months ago
by
Lorenz Martin Diel
Browse files
Options
Downloads
Patches
Plain Diff
Adjusted and refactored the function calc_state
parent
6692c6dd
No related branches found
No related tags found
1 merge request
!15
Merge units into main
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/unit.cpp
+177
-197
177 additions, 197 deletions
src/unit.cpp
with
177 additions
and
197 deletions
src/unit.cpp
+
177
−
197
View file @
07726a73
#include
"unit.hpp"
#include
<iostream>
namespace
advanced_wars
{
namespace
advanced_wars
{
Unit
::
Unit
(
int
x
,
int
y
,
UnitFaction
faction
,
UnitId
id
,
UnitState
state
)
:
x
(
x
),
y
(
y
),
faction
(
faction
),
id
(
id
),
state
(
state
),
max_health
(
100
)
{
:
x
(
x
),
y
(
y
),
faction
(
faction
),
id
(
id
),
state
(
state
),
max_health
(
100
)
{
// das ist nur für Testzwecke
if
(
id
==
UnitId
::
INFANTERY
)
{
if
(
id
==
UnitId
::
INFANTERY
)
{
secondary_weapon
=
Weapon
(
"Machine-Gun"
,
{{
UnitId
::
INFANTERY
,
55
}});
}
health
=
max_health
;
};
void
Unit
::
render
(
Engine
&
engine
,
int
scale
)
{
void
Unit
::
render
(
Engine
&
engine
,
int
scale
)
{
Spritesheet
*
spritesheet
=
engine
.
get_spritesheet
();
int
step
=
engine
.
get_stage
()
%
spritesheet
->
get_unit_textures
()
...
...
@@ -25,8 +21,7 @@ namespace advanced_wars
.
at
(
static_cast
<
int
>
(
state
))
.
second
;
if
(
state
==
UnitState
::
IDLE
||
state
==
UnitState
::
UNAVAILABLE
)
{
if
(
state
==
UnitState
::
IDLE
||
state
==
UnitState
::
UNAVAILABLE
)
{
SDL_Rect
src
;
src
.
x
=
step
*
spritesheet
->
get_unit_width
();
...
...
@@ -47,9 +42,7 @@ namespace advanced_wars
.
at
(
static_cast
<
int
>
(
state
))
.
first
,
&
src
,
&
dst
,
0
,
NULL
,
SDL_FLIP_NONE
);
}
else
{
}
else
{
// The moving states have a resolution of 24x24 instead of 16x16 and need to
// be handled separately
SDL_Rect
src
;
...
...
@@ -74,79 +67,79 @@ namespace advanced_wars
}
}
void
Unit
::
attack
(
Unit
*
enemy
)
{
// Angenommen, primary_weapon und secondary_weapon wurden bereits korrekt
initialisiert
void
Unit
::
attack
(
Unit
*
enemy
)
{
// Angenommen, primary_weapon und secondary_weapon wurden bereits korrekt
//
initialisiert
auto
primary_weapon_damage_it
=
primary_weapon
.
damage
.
find
(
enemy
->
id
);
auto
secondary_weapon_damage_it
=
secondary_weapon
.
damage
.
find
(
enemy
->
id
);
int
attacker_damage_value
=
0
;
// Die Waffe mit dem höchsten Schaden wählen
if
(
secondary_weapon_damage_it
!=
secondary_weapon
.
damage
.
end
())
{
if
(
secondary_weapon_damage_it
!=
secondary_weapon
.
damage
.
end
())
{
attacker_damage_value
=
secondary_weapon_damage_it
->
second
;
}
if
(
primary_weapon_damage_it
!=
primary_weapon
.
damage
.
end
())
{
if
(
primary_weapon_damage_it
->
second
>
attacker_damage_value
)
{
if
(
primary_weapon_damage_it
!=
primary_weapon
.
damage
.
end
())
{
if
(
primary_weapon_damage_it
->
second
>
attacker_damage_value
)
{
// Munitionsabzug sollte hier erfolgen, falls zutreffend
attacker_damage_value
=
primary_weapon_damage_it
->
second
;
}
}
if
(
attacker_damage_value
==
0
)
{
std
::
cout
<<
"No damage value found for attack from unit "
<<
static_cast
<
int
>
(
id
)
<<
" against unit "
<<
static_cast
<
int
>
(
enemy
->
id
)
<<
std
::
endl
;
}
else
{
int
off_damage
=
attacker_damage_value
*
(
static_cast
<
float
>
(
health
)
/
max_health
);
if
(
attacker_damage_value
==
0
)
{
std
::
cout
<<
"No damage value found for attack from unit "
<<
static_cast
<
int
>
(
id
)
<<
" against unit "
<<
static_cast
<
int
>
(
enemy
->
id
)
<<
std
::
endl
;
}
else
{
int
off_damage
=
attacker_damage_value
*
(
static_cast
<
float
>
(
health
)
/
max_health
);
enemy
->
health
-=
off_damage
;
enemy
->
health
=
std
::
max
(
0
,
enemy
->
health
);
// Sicherstellen, dass die Gesundheit nicht negativ wird
enemy
->
health
=
std
::
max
(
0
,
enemy
->
health
);
// Sicherstellen, dass die Gesundheit nicht negativ wird
std
::
cout
<<
"Enemy health after attack: "
<<
enemy
->
health
<<
std
::
endl
;
// Prüfen, ob der Gegner noch am Leben ist um zurückzuschlagen
if
(
enemy
->
health
>
0
)
{
if
(
enemy
->
health
>
0
)
{
// Weapon tables for the defender
auto
defender_primary_weapon_damage_it
=
enemy
->
primary_weapon
.
damage
.
find
(
id
);
auto
defender_secondary_weapon_damage_it
=
enemy
->
secondary_weapon
.
damage
.
find
(
id
);
auto
defender_primary_weapon_damage_it
=
enemy
->
primary_weapon
.
damage
.
find
(
id
);
auto
defender_secondary_weapon_damage_it
=
enemy
->
secondary_weapon
.
damage
.
find
(
id
);
int
defender_damage_value
=
0
;
// Declare outside for later use
// Determine the damage value for the defender
if
(
defender_secondary_weapon_damage_it
!=
enemy
->
secondary_weapon
.
damage
.
end
())
{
if
(
defender_secondary_weapon_damage_it
!=
enemy
->
secondary_weapon
.
damage
.
end
())
{
defender_damage_value
=
defender_secondary_weapon_damage_it
->
second
;
}
if
(
defender_primary_weapon_damage_it
!=
enemy
->
primary_weapon
.
damage
.
end
())
{
if
(
defender_primary_weapon_damage_it
->
second
>
defender_damage_value
)
{
if
(
defender_primary_weapon_damage_it
!=
enemy
->
primary_weapon
.
damage
.
end
())
{
if
(
defender_primary_weapon_damage_it
->
second
>
defender_damage_value
)
{
// Munitionsabzug für primäre Waffe, falls zutreffend
defender_damage_value
=
defender_primary_weapon_damage_it
->
second
;
}
}
// If a valid damage value was determined for retaliation
if
(
defender_damage_value
>
0
)
{
int
def_damage
=
static_cast
<
int
>
(
defender_damage_value
*
static_cast
<
float
>
(
enemy
->
health
)
/
enemy
->
max_health
);
if
(
defender_damage_value
>
0
)
{
int
def_damage
=
static_cast
<
int
>
(
defender_damage_value
*
static_cast
<
float
>
(
enemy
->
health
)
/
enemy
->
max_health
);
this
->
health
-=
def_damage
;
this
->
health
=
std
::
max
(
0
,
this
->
health
);
// Safeguard against negative health
std
::
cout
<<
"Ally health after retaliation: "
<<
this
->
health
<<
std
::
endl
;
this
->
health
=
std
::
max
(
0
,
this
->
health
);
// Safeguard against negative health
std
::
cout
<<
"Ally health after retaliation: "
<<
this
->
health
<<
std
::
endl
;
}
}
}
}
void
Unit
::
update_position
(
int
posX
,
int
posY
)
{
void
Unit
::
update_position
(
int
posX
,
int
posY
)
{
calc_state
(
posX
,
posY
);
this
->
x
=
posX
;
...
...
@@ -154,51 +147,38 @@ namespace advanced_wars
}
void
Unit
::
calc_state
(
int
posX
,
int
posY
)
{
int
deltaX
=
this
->
x
-
posX
;
int
deltaY
=
this
->
y
-
posY
;
int
currentX
=
this
->
x
;
int
currentY
=
this
->
y
;
int
deltaX
=
currentX
-
posX
;
int
deltaY
=
currentY
-
posY
;
if
(
deltaX
==
0
&&
deltaY
==
0
)
{
// Unit is already at the target position
return
;
}
if
(
delta
Y
==
0
)
{
if
(
abs
(
delta
X
)
>=
abs
(
deltaY
)
)
{
if
(
deltaX
>
0
)
{
this
->
state
=
UnitState
::
MOVEMENTLEFT
;
return
;
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTLEFT
;
}
else
{
this
->
state
=
UnitState
::
MOVEMENTRIGHT
;
return
;
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTRIGHT
;
}
}
double
bresen
=
deltaX
/
deltaY
;
if
(
bresen
==
0
)
{
if
(
deltaY
<
0
)
{
this
->
state
=
UnitState
::
MOVEMENTDOWN
;
return
;
}
else
{
this
->
state
=
UnitState
::
MOVEMENTUP
;
return
;
if
(
deltaY
>
0
)
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTUP
;
}
else
{
this
->
state
=
advanced_wars
::
UnitState
::
MOVEMENTDOWN
;
}
}
}
void
Unit
::
on_left_click
(
SDL_Event
event
,
std
::
vector
<
Unit
>
&
unitVector
)
{
void
Unit
::
on_left_click
(
SDL_Event
event
,
std
::
vector
<
Unit
>
&
unitVector
)
{
std
::
cout
<<
"Left-button pressed on unit: "
<<
this
->
health
<<
std
::
endl
;
}
bool
Unit
::
inRange
(
Unit
*
enemy
)
{
if
(
this
->
x
==
enemy
->
x
)
{
bool
Unit
::
inRange
(
Unit
*
enemy
)
{
if
(
this
->
x
==
enemy
->
x
)
{
return
abs
(
this
->
y
-
enemy
->
y
)
<=
this
->
range
;
}
else
if
(
this
->
y
==
enemy
->
y
)
{
}
else
if
(
this
->
y
==
enemy
->
y
)
{
return
abs
(
this
->
x
-
enemy
->
x
)
<=
this
->
range
;
}
return
false
;
...
...
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