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
ab45c86e
Commit
ab45c86e
authored
6 months ago
by
Max Cherris
Browse files
Options
Downloads
Patches
Plain Diff
Add onClick for units and basic attack initialization
parent
7c937293
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
+54
-14
54 additions, 14 deletions
src/unit.cpp
src/unit.hpp
+4
-0
4 additions, 0 deletions
src/unit.hpp
with
58 additions
and
14 deletions
src/unit.cpp
+
54
−
14
View file @
ab45c86e
...
...
@@ -63,33 +63,73 @@ void Unit::render(Engine &engine, int scale) {
}
}
void
Unit
::
attack
(
Unit
&
enemy
)
{
void
Unit
::
attack
(
Unit
&
ally
,
Unit
&
enemy
)
{
//has unit not attacked this turn?
if
(
!
inRange
(
enemy
))
{
//Render enemies as not in Range => use the UNAVAILABLE Texture for that
}
if
(
this
->
has_attacked
)
{
if
(
ally
->
has_attacked
)
{
//display the unit as not able to attack (maybe greyscale?)
}
//Start Attack: choose the appropriate weapon:
int
offDamage
=
damageMatrix
[
this
->
id
][
enemy
->
id
]
*
((
this
->
health
)
/
(
this
->
max_health
));
int
defDamage
=
damageMatrix
[
this
->
id
][
enemy
->
id
]
*
((
enemy
->
health
)
/
(
enemy
->
max_health
));
int
offDamage
=
damageMatrix
[
ally
->
id
][
enemy
->
id
]
*
((
ally
->
health
)
/
(
ally
->
max_health
));
int
defDamage
=
damageMatrix
[
ally
->
id
][
enemy
->
id
]
*
((
enemy
->
health
)
/
(
enemy
->
max_health
));
enemy
->
health
=
enemy
->
health
-
offDamage
;
if
(
enemy
->
health
>
0
)
{
this
->
health
=
this
->
health
-
defDamage
;
if
(
this
->
health
<=
0
)
{
this
->~
Unit
();
ally
->
health
=
ally
->
health
-
defDamage
;
if
(
ally
->
health
<=
0
)
{
ally
->~
Unit
();
}
}
else
{
enemy
->~
Unit
();
}
}
void
Unit
::
inRange
(
Unit
&
enemy
)
{
void
Unit
::
onClick
(
SDL_Event
event
)
{
Unit
&
defender
;
Unit
&
attacker
;
switch
(
event
.
button
.
button
)
{
case
SDL_BUTTON_LEFT
:
this
->
is_selected
=
true
;
for
(
Unit
&
unit
:
units
)
{
if
(
inRange
(
unit
))
{
unit
.
state
=
UNAVAILABLE
;
};
}
break
;
case
SDL_BUTTON_RIGHT
:
this
->
is_targeted
=
true
;
for
(
Unit
&
unit
:
units
)
{
if
(
unit
.
state
=
UNAVAILABLE
)
{
continue
;
}
if
(
unit
.
is_selected
)
{
attacker
=
&
unit
;
}
if
(
unit
.
is_targeted
)
{
defender
=
&
unit
;
}
}
if
(
attacker
&&
defender
)
{
attack
(
attacker
,
defender
);
break
;
}
else
{
stderr
(
"Could not init the attack!"
);
break
;
}
}
}
bool
Unit
::
inRange
(
Unit
&
enemy
)
{
if
(
this
->
x
==
enemy
.
x
)
{
return
abs
(
this
->
y
-
enemy
.
y
)
<=
this
->
range
;
}
else
if
(
this
->
y
==
enemy
.
y
)
{
...
...
@@ -98,7 +138,7 @@ void Unit::inRange(Unit &enemy) {
return
false
;
}
void
loadXML
(
const
char
*
filename
)
{
void
Unit
::
loadXML
(
const
char
*
filename
)
{
tinyxml2
::
XMLDocument
doc
;
if
(
doc
.
LoadFile
(
filename
)
!=
tinyxml2
::
XML_SUCCESS
)
{
...
...
This diff is collapsed.
Click to expand it.
src/unit.hpp
+
4
−
0
View file @
ab45c86e
...
...
@@ -102,6 +102,8 @@ public:
*/
void
loadXML
(
const
char
*
filename
);
void
onClick
(
SDL_EVENT
event
);
private
:
int
x
;
int
y
;
...
...
@@ -118,6 +120,8 @@ private:
bool
has_moved
;
bool
has_attacked
;
bool
is_selected
;
bool
is_targeted
;
// Primary weapon ammo
int
ammo
;
...
...
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