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
159e0b5f
Commit
159e0b5f
authored
6 months ago
by
Max Cherris
Browse files
Options
Downloads
Patches
Plain Diff
Update with move position and format unit.cpp
parent
85ba7225
No related branches found
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
+163
-128
163 additions, 128 deletions
src/unit.cpp
src/unit.hpp
+7
-1
7 additions, 1 deletion
src/unit.hpp
with
170 additions
and
129 deletions
src/unit.cpp
+
163
−
128
View file @
159e0b5f
#include
"unit.hpp"
#include
<tinyxml2.h>
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
)
{
:
x
(
x
),
y
(
y
),
faction
(
faction
),
id
(
id
),
state
(
state
)
{
health
=
max_health
;
};
void
Unit
::
render
(
Engine
&
engine
,
int
scale
)
{
void
Unit
::
render
(
Engine
&
engine
,
int
scale
)
{
Spritesheet
*
spritesheet
=
engine
.
get_spritesheet
();
while
(
engine
.
events
not
empty
)
{
handle_event
(
events
.
pop_front
());
}
int
step
=
engine
.
get_stage
()
%
spritesheet
->
get_unit_textures
()
...
...
@@ -23,7 +25,8 @@ void Unit::render(Engine &engine, int scale) {
.
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
();
...
...
@@ -44,7 +47,9 @@ void Unit::render(Engine &engine, int scale) {
.
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
;
...
...
@@ -69,9 +74,11 @@ void Unit::render(Engine &engine, int scale) {
}
}
void
Unit
::
attack
(
Unit
&
ally
,
Unit
&
enemy
)
{
void
Unit
::
attack
(
Unit
&
ally
,
Unit
&
enemy
)
{
if
(
ally
->
has_attacked
)
{
if
(
ally
->
has_attacked
)
{
// display the unit as not able to attack (maybe greyscale?)
}
...
...
@@ -80,29 +87,43 @@ void Unit::attack(Unit &ally, Unit &enemy) {
int
defDamage
=
damageMatrix
[
ally
->
id
][
enemy
->
id
]
*
((
enemy
->
health
)
/
(
enemy
->
max_health
));
enemy
->
health
=
enemy
->
health
-
offDamage
;
if
(
enemy
->
health
>
0
)
{
if
(
enemy
->
health
>
0
)
{
ally
->
health
=
ally
->
health
-
defDamage
;
if
(
ally
->
health
<=
0
)
{
if
(
ally
->
health
<=
0
)
{
ally
->~
Unit
();
}
}
else
{
}
else
{
enemy
->~
Unit
();
}
}
void
Unit
::
onClick
(
SDL_Event
event
)
{
void
Unit
::
update_position
(
int
posX
,
int
posY
)
{
this
->
x
=
posX
;
this
->
y
=
posY
;
}
void
Unit
::
onClick
(
SDL_EVENT
event
)
{
Unit
&
defender
;
Unit
&
attacker
;
switch
(
event
.
button
.
button
)
{
switch
(
event
.
button
.
button
)
{
case
SDL_BUTTON_LEFT
:
// we have to re-initialize the unit.state (probably to idle)
this
->
is_selected
=
true
;
for
(
Unit
&
unit
:
units
)
{
if
(
inRange
(
unit
))
{
for
(
Unit
&
unit
:
units
)
{
if
(
inRange
(
unit
))
{
unit
.
state
=
UNAVAILABLE
;
};
}
...
...
@@ -111,44 +132,56 @@ void Unit::onClick(SDL_Event event) {
this
->
is_targeted
=
true
;
for
(
Unit
&
unit
:
units
)
{
if
(
unit
.
state
=
UNAVAILABLE
)
{
for
(
Unit
&
unit
:
units
)
{
if
(
unit
.
state
=
UNAVAILABLE
)
{
continue
;
}
if
(
unit
.
is_selected
)
{
if
(
unit
.
is_selected
)
{
attacker
=
&
unit
;
}
if
(
unit
.
is_targeted
)
{
if
(
unit
.
is_targeted
)
{
defender
=
&
unit
;
}
}
if
(
attacker
&&
defender
)
{
if
(
attacker
&&
defender
)
{
attack
(
attacker
,
defender
);
break
;
}
else
{
}
else
{
stderr
(
"Could not init the attack!"
);
break
;
}
}
}
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
;
}
void
Unit
::
loadXML
(
const
char
*
filename
)
{
void
Unit
::
loadXML
(
const
char
*
filename
)
{
tinyxml2
::
XMLDocument
doc
;
if
(
doc
.
LoadFile
(
filename
)
!=
tinyxml2
::
XML_SUCCESS
)
{
if
(
doc
.
LoadFile
(
filename
)
!=
tinyxml2
::
XML_SUCCESS
)
{
std
::
cerr
<<
"Fehler beim Laden der XML-Datei!"
<<
std
::
endl
;
return
;
}
...
...
@@ -157,7 +190,8 @@ void Unit::loadXML(const char* filename) {
tinyxml2
::
XMLElement
*
unitElement
=
doc
.
FirstChildElement
(
"Units"
)
->
FirstChildElement
(
"Unit"
);
// get all Units
while
(
unitElement
)
{
while
(
unitElement
)
{
const
u_int8_t
UnitId
=
unitElement
->
Attribute
(
"id"
);
...
...
@@ -165,11 +199,13 @@ void Unit::loadXML(const char* filename) {
tinyxml2
::
XMLElement
*
attackElement
=
unitElement
->
FirstChildElement
(
"Attack"
);
// get all attack-values
while
(
attackElement
)
{
while
(
attackElement
)
{
tinyxml2
::
XMLElement
*
attackTypeElement
=
attackElement
->
FirstChildElement
();
while
(
attackTypeElement
)
{
while
(
attackTypeElement
)
{
UnitId
Unit_Id
=
static_cast
<
UnitId
>
attackTypeElement
->
Name
();
// wenn das geht kauf ich maggus sein kochbuch
int
attackValue
=
attackTypeElement
->
IntText
();
...
...
@@ -183,6 +219,5 @@ void Unit::loadXML(const char* filename) {
damageMatrix
[
unitElement
][
attackValues
];
unitElement
=
unitElement
->
NextSiblingElement
(
"Unit"
);
}
}
}
// namespace advanced_wars
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/unit.hpp
+
7
−
1
View file @
159e0b5f
...
...
@@ -81,11 +81,12 @@ public:
Will Update the health for both units
Attacker deals damage to the defender first
*/
void
attack
(
Unit
&
enemy
);
void
attack
(
Unit
&
ally
,
Unit
&
enemy
);
/*
@params Takes the desired position of the unit and updates its values
This will teleport the unit, there is no smooth transition between tiles
*/
void
update_position
(
int
posX
,
int
posY
);
...
...
@@ -102,6 +103,11 @@ public:
*/
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_EVENT
event
);
private
:
...
...
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