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
1c234b83
Commit
1c234b83
authored
5 months ago
by
Max Cherris
Browse files
Options
Downloads
Patches
Plain Diff
Implement removal of units on stage if defeated
parent
abb13726
Branches
Branches containing commit
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
+24
-2
24 additions, 2 deletions
src/level.cpp
src/unit.cpp
+9
-28
9 additions, 28 deletions
src/unit.cpp
src/unit.hpp
+5
-2
5 additions, 2 deletions
src/unit.hpp
with
38 additions
and
32 deletions
src/level.cpp
+
24
−
2
View file @
1c234b83
...
...
@@ -8,6 +8,7 @@
#include
<SDL.h>
#include
<iostream>
#include
<string>
#include
<algorithm>
namespace
advanced_wars
{
...
...
@@ -74,6 +75,7 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
switch
(
event
.
type
)
{
case
SDL_MOUSEBUTTONDOWN
:
if
(
event
.
button
.
button
==
SDL_BUTTON_LEFT
)
{
if
(
clickCheck
(
event
.
button
.
x
,
event
.
button
.
y
))
{
if
(
selectedUnit
)
{
...
...
@@ -83,8 +85,28 @@ void Level::handleEvent(Engine &engine, SDL_Event &event) {
if
(
selectedBuilding
)
{
//building stuff
}
}
}
else
if
(
event
.
button
.
button
==
SDL_BUTTON_RIGHT
)
{
if
(
selectedUnit
)
{
int
tileX
=
event
.
button
.
x
/
(
16
*
RENDERING_SCALE
);
int
tileY
=
event
.
button
.
y
/
(
16
*
RENDERING_SCALE
);
for
(
auto
&
unit
:
units
)
{
if
(
unit
.
x
==
tileX
&&
unit
.
y
==
tileY
)
{
//std::cout << "X:" << unit.x << "Y:" << unit.y << std::endl;
selectedUnit
->
attack
(
unit
);
units
.
erase
(
std
::
remove_if
(
units
.
begin
(),
units
.
end
(),
[](
const
Unit
&
unit
)
{
return
unit
.
health
<
0
;
}),
units
.
end
());
}
}
}
}
break
;
default
:
...
...
This diff is collapsed.
Click to expand it.
src/unit.cpp
+
9
−
28
View file @
1c234b83
...
...
@@ -73,35 +73,16 @@ namespace advanced_wars
MatchupTabel
damageMatrix
;
std
::
vector
<
Unit
*>
units
;
void
Unit
::
attack
(
Unit
*
ally
,
Unit
*
enemy
)
{
if
(
ally
->
has_attacked
)
{
// display the unit as not able to attack (maybe greyscale?)
}
void
Unit
::
attack
(
Unit
&
enemy
)
{
// Start Attack: choose the appropriate weapon:
/*
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>(enemy->id)][static_cast<u_int8_t>(ally->id)] * ((enemy->health) / (enemy->max_health));
*/
int
offDamage
=
10
;
int
offDamage
=
50
;
int
defDamage
=
1000
;
enemy
->
health
=
enemy
->
health
-
offDamage
;
if
(
enemy
->
health
>
0
)
{
ally
->
health
=
ally
->
health
-
defDamage
;
std
::
cout
<<
"Health ally:"
<<
ally
->
health
<<
std
::
endl
;
if
(
ally
->
health
<=
0
)
{
ally
->~
Unit
();
}
}
else
{
enemy
->~
Unit
();
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
;
}
}
...
...
@@ -175,7 +156,7 @@ Features:
if
(
attacker
!=
nullptr
&&
defender
!=
nullptr
)
{
attack
(
attacker
,
defender
);
//
attack(attacker, defender);
std
::
cout
<<
"We are fighting!!"
<<
std
::
endl
;
break
;
}
...
...
This diff is collapsed.
Click to expand it.
src/unit.hpp
+
5
−
2
View file @
1c234b83
...
...
@@ -62,6 +62,7 @@ class Unit {
public:
int
x
;
int
y
;
int
health
;
//health equals max_health at construction
Unit
(
int
x
,
int
y
,
UnitFaction
faction
,
UnitId
id
,
UnitState
state
);
...
...
@@ -86,7 +87,8 @@ public:
Will Update the health for both units
Attacker deals damage to the defender first
*/
void
attack
(
Unit
*
ally
,
Unit
*
enemy
);
void
attack
(
Unit
&
enemy
);
/*
...
...
@@ -109,6 +111,7 @@ 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
...
...
@@ -120,7 +123,7 @@ private:
UnitId
id
;
UnitState
state
;
int
health
;
//health equals max_health at construction
int
max_health
;
// max_health required for damage_scaling
int
range
;
int
fuel
;
...
...
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