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
c3aeb3d7
Commit
c3aeb3d7
authored
9 months ago
by
Max Cherris
Browse files
Options
Downloads
Patches
Plain Diff
Fix all possible bugs
Had to switch from references to pointers for now; should be refactored in the future
parent
159e0b5f
Branches
Branches containing commit
No related tags found
1 merge request
!15
Merge units into main
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/unit.cpp
+26
-26
26 additions, 26 deletions
src/unit.cpp
src/unit.hpp
+6
-4
6 additions, 4 deletions
src/unit.hpp
with
32 additions
and
30 deletions
src/unit.cpp
+
26
−
26
View file @
c3aeb3d7
#include
"unit.hpp"
#include
<tinyxml2.h>
#include
<iostream>
namespace
advanced_wars
{
...
...
@@ -14,11 +15,6 @@ namespace advanced_wars
{
Spritesheet
*
spritesheet
=
engine
.
get_spritesheet
();
while
(
engine
.
events
not
empty
)
{
handle_event
(
events
.
pop_front
());
}
int
step
=
engine
.
get_stage
()
%
spritesheet
->
get_unit_textures
()
.
at
(
static_cast
<
int
>
(
faction
))
.
at
(
static_cast
<
int
>
(
id
))
...
...
@@ -74,7 +70,10 @@ namespace advanced_wars
}
}
void
Unit
::
attack
(
Unit
&
ally
,
Unit
&
enemy
)
MatchupTabel
damageMatrix
;
std
::
vector
<
Unit
*>
units
;
void
Unit
::
attack
(
Unit
*
ally
,
Unit
*
enemy
)
{
if
(
ally
->
has_attacked
)
...
...
@@ -83,8 +82,8 @@ namespace advanced_wars
}
// Start Attack: choose the appropriate weapon:
int
offDamage
=
damageMatrix
[
ally
->
id
][
enemy
->
id
]
*
((
ally
->
health
)
/
(
ally
->
max_health
));
int
defDamage
=
damageMatrix
[
ally
->
id
][
enemy
->
id
]
*
((
enemy
->
health
)
/
(
enemy
->
max_health
));
int
offDamage
=
damageMatrix
[
static_cast
<
u_int8_t
>
(
ally
->
id
)
][
static_cast
<
u_int8_t
>
(
enemy
->
id
)
]
*
((
ally
->
health
)
/
(
ally
->
max_health
));
int
defDamage
=
damageMatrix
[
static_cast
<
u_int8_t
>
(
ally
->
id
)
][
static_cast
<
u_int8_t
>
(
enemy
->
id
)
]
*
((
enemy
->
health
)
/
(
enemy
->
max_health
));
enemy
->
health
=
enemy
->
health
-
offDamage
;
if
(
enemy
->
health
>
0
)
...
...
@@ -107,11 +106,11 @@ namespace advanced_wars
this
->
y
=
posY
;
}
void
Unit
::
onClick
(
SDL_E
VENT
event
)
void
Unit
::
onClick
(
SDL_E
vent
event
)
{
Unit
&
defender
;
Unit
&
attacker
;
Unit
*
defender
=
nullptr
;
Unit
*
attacker
=
nullptr
;
switch
(
event
.
button
.
button
)
{
...
...
@@ -120,11 +119,11 @@ namespace advanced_wars
// we have to re-initialize the unit.state (probably to idle)
this
->
is_selected
=
true
;
for
(
Unit
&
unit
:
units
)
for
(
Unit
*
unit
:
units
)
{
if
(
inRange
(
unit
))
{
unit
.
state
=
UNAVAILABLE
;
unit
->
state
=
advanced_wars
::
UnitState
::
UNAVAILABLE
;
};
}
break
;
...
...
@@ -132,21 +131,21 @@ namespace advanced_wars
this
->
is_targeted
=
true
;
for
(
Unit
&
unit
:
units
)
for
(
Unit
*
unit
:
units
)
{
if
(
unit
.
state
=
UNAVAILABLE
)
if
(
unit
->
state
=
=
advanced_wars
::
UnitState
::
UNAVAILABLE
)
{
continue
;
}
if
(
unit
.
is_selected
)
if
(
unit
->
is_selected
)
{
attacker
=
&
unit
;
attacker
=
unit
;
}
if
(
unit
.
is_targeted
)
if
(
unit
->
is_targeted
)
{
defender
=
&
unit
;
defender
=
unit
;
}
}
...
...
@@ -157,25 +156,25 @@ namespace advanced_wars
}
else
{
std
err
(
"Could not init the attack!"
)
;
std
::
cerr
<<
"Fehler beim Laden der XML-Datei!"
<<
std
::
endl
;
break
;
}
}
}
bool
Unit
::
inRange
(
Unit
&
enemy
)
bool
Unit
::
inRange
(
Unit
*
enemy
)
{
if
(
this
->
x
==
enemy
.
x
)
if
(
this
->
x
==
enemy
->
x
)
{
return
abs
(
this
->
y
-
enemy
.
y
)
<=
this
->
range
;
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
abs
(
this
->
x
-
enemy
->
x
)
<=
this
->
range
;
}
return
false
;
}
/*
void Unit::loadXML(const char *filename)
{
...
...
@@ -220,4 +219,5 @@ namespace advanced_wars
unitElement = unitElement->NextSiblingElement("Unit");
}
}
*/
}
// namespace advanced_wars
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/unit.hpp
+
6
−
4
View file @
c3aeb3d7
#pragma once
#include
<unordered_map>
#include
"engine.hpp"
#include
"weapon.hpp"
#include
<optional>
...
...
@@ -72,7 +73,7 @@ public:
If a unit is selected, it should call inRange on all other enemy units on the field
*/
bool
inRange
(
Unit
&
enemy
);
bool
inRange
(
Unit
*
enemy
);
/*
The attacker will move towards the defender and thus initiate combat
...
...
@@ -81,7 +82,7 @@ public:
Will Update the health for both units
Attacker deals damage to the defender first
*/
void
attack
(
Unit
&
ally
,
Unit
&
enemy
);
void
attack
(
Unit
*
ally
,
Unit
*
enemy
);
/*
...
...
@@ -108,7 +109,7 @@ void loadXML(const char* filename);
This function will be called by an external event-handler, eventually.
Currently, it should be called if a Unit is interacted with and the resulting SDL_EVENT is passed through, and then decided upon
*/
void
onClick
(
SDL_E
VENT
event
);
void
onClick
(
SDL_E
vent
event
);
private
:
int
x
;
...
...
@@ -131,9 +132,10 @@ private:
// Primary weapon ammo
int
ammo
;
/*
std::optional<Weapon> primary;
std::optional<Weapon> secondary;
*/
};
}
// namespace advanced_wars
\ No newline at end of file
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
sign in
to comment