Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pokeprofs
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
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
william.ho
Pokeprofs
Commits
86b3bb12
Commit
86b3bb12
authored
1 year ago
by
antoine.giles
Browse files
Options
Downloads
Patches
Plain Diff
added hp bar
parent
d3456bef
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
game/src/combat.rs
+137
-66
137 additions, 66 deletions
game/src/combat.rs
game/src/main.rs
+1
-0
1 addition, 0 deletions
game/src/main.rs
with
138 additions
and
66 deletions
game/src/combat.rs
+
137
−
66
View file @
86b3bb12
...
@@ -14,14 +14,12 @@ impl Plugin for CombatPlugin {
...
@@ -14,14 +14,12 @@ impl Plugin for CombatPlugin {
app
.add_systems
(
Update
,
button_system
);
app
.add_systems
(
Update
,
button_system
);
app
.add_state
::
<
CombatState
>
();
app
.add_state
::
<
CombatState
>
();
app
.add_systems
(
OnEnter
(
CombatState
::
ETurn
),
enemy_attack
);
app
.add_systems
(
OnEnter
(
CombatState
::
ETurn
),
enemy_attack
);
app
.add_systems
(
OnEnter
(
CombatState
::
PTurn
),
player_attack
);
app
.add_systems
(
Update
,
combat_outcome
);
app
.add_systems
(
Update
,
combat_outcome
);
app
.add_systems
(
OnExit
(
GameState
::
Combat
),
exit_combat
);
app
.add_systems
(
OnExit
(
GameState
::
Combat
),
exit_combat
);
app
.add_systems
(
OnEnter
(
CombatState
::
PrepareAttack
),
update_hp
);
}
}
}
}
static
ATTACKNO
:
AtomicUsize
=
AtomicUsize
::
new
(
0
);
#[derive(Clone,
Copy,
Default,
Eq,
PartialEq,
Debug,
Hash,
States)]
#[derive(Clone,
Copy,
Default,
Eq,
PartialEq,
Debug,
Hash,
States)]
pub
enum
CombatState
{
pub
enum
CombatState
{
#[default]
#[default]
...
@@ -46,13 +44,17 @@ const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
...
@@ -46,13 +44,17 @@ const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
#[derive(Component)]
#[derive(Component)]
struct
OnMainCombat
;
struct
OnMainCombat
;
#[derive(Component)]
struct
CombatText
;
pub
fn
spawn_combat
(
pub
fn
spawn_combat
(
mut
commands
:
Commands
,
mut
commands
:
Commands
,
asset_server
:
Res
<
AssetServer
>
,
asset_server
:
Res
<
AssetServer
>
,
window_query
:
Query
<&
mut
Window
,
With
<
PrimaryWindow
>>
,
window_query
:
Query
<&
mut
Window
,
With
<
PrimaryWindow
>>
,
player_query
:
Query
<
(
&
mut
Transform
,
&
Player
)
>
,
player_query
:
Query
<
(
&
Player
)
>
,
enemy_query
:
Query
<&
Enemy
>
,
)
{
)
{
let
player
=
player_query
.get_single
()
.unwrap
()
;
let
player
s
=
player_query
.get_single
();
let
button_style
=
Style
{
let
button_style
=
Style
{
width
:
Val
::
Px
(
150.0
),
width
:
Val
::
Px
(
150.0
),
height
:
Val
::
Px
(
65.0
),
height
:
Val
::
Px
(
65.0
),
...
@@ -65,14 +67,9 @@ pub fn spawn_combat(
...
@@ -65,14 +67,9 @@ pub fn spawn_combat(
};
};
let
window
=
window_query
.get_single
()
.unwrap
();
let
window
=
window_query
.get_single
()
.unwrap
();
//spawn combat assets
commands
.spawn
((
commands
.spawn
((
SpriteBundle
{
SpriteBundle
{
// sprite: Sprite {
// color: Color::rgb(0.0, 0.0, 0.0),
// custom_size: Some(Vec2::new(window.width(), window.height())),
// ..default()
// },
transform
:
Transform
{
transform
:
Transform
{
translation
:
Vec3
{
translation
:
Vec3
{
z
:
3.0
,
z
:
3.0
,
...
@@ -115,7 +112,7 @@ pub fn spawn_combat(
...
@@ -115,7 +112,7 @@ pub fn spawn_combat(
},
},
OnMainCombat
,
OnMainCombat
,
));
));
//spawn combat ui
commands
commands
.spawn
((
.spawn
((
NodeBundle
{
NodeBundle
{
...
@@ -149,7 +146,8 @@ pub fn spawn_combat(
...
@@ -149,7 +146,8 @@ pub fn spawn_combat(
..
default
()
..
default
()
})
})
.with_children
(|
parent
|
{
.with_children
(|
parent
|
{
for
capacite
in
&
player
.1
.moveset
{
if
let
Ok
(
player
)
=
players
{
for
capacite
in
&
player
.moveset
{
parent
parent
.spawn
((
.spawn
((
ButtonBundle
{
ButtonBundle
{
...
@@ -167,6 +165,53 @@ pub fn spawn_combat(
...
@@ -167,6 +165,53 @@ pub fn spawn_combat(
});
});
i
+=
1
;
i
+=
1
;
}
}
}
})
.with_children
(|
parent
|
{
if
let
Ok
(
player
)
=
players
{
parent
.spawn
((
TextBundle
{
text
:
Text
{
sections
:
vec!
[
TextSection
{
value
:
format!
(
"HP Player {}"
,
player
.hp
),
style
:
TextStyle
{
color
:
Color
::
GREEN
,
font_size
:
20.0
,
..
default
()
},
..
default
()
}],
..
default
()
},
..
default
()
},
CombatText
,
));
}
})
.with_children
(|
parent
|
{
for
enemy
in
&
enemy_query
{
if
enemy
.tag
==
true
{
parent
.spawn
((
TextBundle
{
text
:
Text
{
sections
:
vec!
[
TextSection
{
value
:
format!
(
"HP Enemy {}"
,
enemy
.hp
),
style
:
TextStyle
{
color
:
Color
::
RED
,
font_size
:
20.0
,
..
default
()
},
..
default
()
}],
..
default
()
},
..
default
()
},
CombatText
,
));
}
}
});
});
});
});
}
}
...
@@ -184,28 +229,41 @@ fn button_system(
...
@@ -184,28 +229,41 @@ fn button_system(
>
,
>
,
mut
text_query
:
Query
<&
mut
Text
>
,
mut
text_query
:
Query
<&
mut
Text
>
,
mut
combat_state
:
ResMut
<
NextState
<
CombatState
>>
,
mut
combat_state
:
ResMut
<
NextState
<
CombatState
>>
,
mut
players
:
Query
<&
mut
Player
>
,
mut
enemies
:
Query
<&
mut
Enemy
>
,
)
{
)
{
for
(
interaction
,
mut
color
,
mut
border_color
,
children
,
btnaction
)
in
&
mut
interaction_query
{
for
(
interaction
,
mut
color
,
mut
border_color
,
children
,
btnaction
)
in
&
mut
interaction_query
{
let
mut
text
=
text_query
.get_mut
(
children
[
0
])
.unwrap
();
let
mut
text
=
text_query
.get_mut
(
children
[
0
])
.unwrap
();
match
*
interaction
{
match
*
interaction
{
Interaction
::
Pressed
=>
{
Interaction
::
Pressed
=>
{
for
mut
enemy
in
&
mut
enemies
{
println!
(
"enemy tag is{}"
,
enemy
.tag
);
if
enemy
.tag
==
true
{
for
player
in
&
players
{
match
btnaction
{
match
btnaction
{
BtnAction
::
Cap1
=>
{
BtnAction
::
Cap1
=>
{
ATTACKNO
.store
(
0
,
Ordering
::
Relaxed
);
println!
(
"button cap1 pressed"
);
println!
(
"button cap1 pressed"
);
combat_state
.set
(
CombatState
::
PTurn
);
enemy
.hp
-=
player
.moveset
[
0
]
.degat
;
println!
(
"enemi prend {} degat"
,
player
.moveset
[
0
]
.degat
);
}
}
BtnAction
::
Cap2
=>
{
BtnAction
::
Cap2
=>
{
ATTACKNO
.store
(
1
,
Ordering
::
Relaxed
);
println!
(
"button cap1 pressed"
);
combat_state
.set
(
CombatState
::
PTurn
);
enemy
.hp
-=
player
.moveset
[
1
]
.degat
;
println!
(
"enemi prend {} degat"
,
player
.moveset
[
1
]
.degat
);
}
}
BtnAction
::
Cap3
=>
{
BtnAction
::
Cap3
=>
{
ATTACKNO
.store
(
2
,
Ordering
::
Relaxed
);
println!
(
"button cap1 pressed"
);
combat_state
.set
(
CombatState
::
PTurn
);
enemy
.hp
-=
player
.moveset
[
2
]
.degat
;
println!
(
"enemi prend {} degat"
,
player
.moveset
[
2
]
.degat
);
}
}
BtnAction
::
Cap4
=>
{
BtnAction
::
Cap4
=>
{
ATTACKNO
.store
(
3
,
Ordering
::
Relaxed
);
println!
(
"button cap1 pressed"
);
combat_state
.set
(
CombatState
::
PTurn
);
enemy
.hp
-=
player
.moveset
[
3
]
.degat
;
println!
(
"enemi prend {} degat"
,
player
.moveset
[
0
]
.degat
);
}
}
}
combat_state
.set
(
CombatState
::
ETurn
);
}
}
}
}
*
color
=
PRESSED_BUTTON
.into
();
*
color
=
PRESSED_BUTTON
.into
();
...
@@ -223,41 +281,49 @@ fn button_system(
...
@@ -223,41 +281,49 @@ fn button_system(
}
}
}
}
fn
player
_attack
(
fn
enemy
_attack
(
mut
enemies
:
Query
<
(
&
mut
Enemy
)
>
,
mut
enemies
:
Query
<
(
&
Enemy
)
>
,
mut
players
:
Query
<
(
&
Player
)
>
,
mut
players
:
Query
<
(
&
mut
Player
)
>
,
mut
combat_state
:
ResMut
<
NextState
<
CombatState
>>
,
mut
combat_state
:
ResMut
<
NextState
<
CombatState
>>
,
)
{
)
{
println!
(
"entered player_attack()"
);
for
(
mut
enemy
)
in
&
enemies
{
for
mut
enemy
in
&
mut
enemies
{
println!
(
"enemy tag is{}"
,
enemy
.tag
);
if
enemy
.tag
==
true
{
if
enemy
.tag
==
true
{
for
player
in
&
players
{
for
(
mut
player
)
in
&
mut
players
{
enemy
.hp
-=
player
.moveset
[
ATTACKNO
.load
(
Ordering
::
Relaxed
)]
.
degat
;
player
.hp
-=
enemy
.nb
degat
;
println!
(
println!
(
"
enemi
prend {} degat"
,
"
joueur
prend {} degat
s, joueur a {} hp
"
,
player
.moveset
[
ATTACKNO
.load
(
Ordering
::
Relaxed
)]
.degat
enemy
.nbdegat
,
player
.hp
);
);
}
}
}
}
}
}
combat_state
.set
(
CombatState
::
ETurn
);
combat_state
.set
(
CombatState
::
PrepareAttack
);
}
}
fn
enemy_attack
(
fn
update_hp
(
mut
enemies
:
Query
<
(
&
Enemy
)
>
,
enemies
:
Query
<&
Enemy
>
,
mut
players
:
Query
<
(
&
mut
Player
)
>
,
players
:
Query
<
&
Player
>
,
mut
combat_state
:
ResMut
<
NextState
<
CombatState
>
>
,
mut
entity_hp
:
Query
<
(
&
mut
Text
,
&
CombatText
)
>
,
)
{
)
{
for
(
mut
enemy
)
in
&
enemies
{
for
(
mut
ehp
,
et
)
in
&
mut
entity_hp
{
for
player
in
&
players
{
for
enemy
in
&
enemies
{
if
enemy
.tag
==
true
{
if
enemy
.tag
==
true
{
for
(
mut
player
)
in
&
mut
players
{
if
ehp
.sections
[
0
]
.style.color
==
Color
::
RED
{
player
.hp
-=
enemy
.nbdegat
;
ehp
.sections
[
0
]
.value
=
format!
(
"Enemy HP: {}"
,
enemy
.hp
);
println!
(
"joueur prend {} degats"
,
enemy
.nbdegat
);
}
else
{
ehp
.sections
[
0
]
.value
=
format!
(
"Player HP: {}"
,
player
.hp
);
}
println!
(
"enemy hp is {}"
,
enemy
.hp
);
}
}
}
}
}
}
combat_state
.set
(
CombatState
::
PrepareAttack
);
}
//for (mut php, pt) in &mut player_hp {
// for player in &players {
// php.sections[0].value = format!("Player HP: {}", player.hp);
// }
//}
}
}
fn
combat_outcome
(
fn
combat_outcome
(
...
@@ -284,8 +350,13 @@ fn combat_outcome(
...
@@ -284,8 +350,13 @@ fn combat_outcome(
}
}
}
}
fn
exit_combat
(
mut
combat_ui
:
Query
<
(
Entity
,
&
Transform
,
&
OnMainCombat
)
>
,
mut
commands
:
Commands
)
{
fn
exit_combat
(
mut
combat_ui
:
Query
<
(
Entity
,
&
Transform
,
&
OnMainCombat
)
>
,
mut
commands
:
Commands
,
mut
game_state
:
ResMut
<
NextState
<
GameState
>>
,
)
{
for
(
ui_id
,
_transform
,
_onmaincombat
)
in
&
combat_ui
{
for
(
ui_id
,
_transform
,
_onmaincombat
)
in
&
combat_ui
{
commands
.entity
(
ui_id
)
.despawn_recursive
();
commands
.entity
(
ui_id
)
.despawn_recursive
();
}
}
game_state
.set
(
GameState
::
GameOver
);
}
}
This diff is collapsed.
Click to expand it.
game/src/main.rs
+
1
−
0
View file @
86b3bb12
...
@@ -33,6 +33,7 @@ pub enum GameState {
...
@@ -33,6 +33,7 @@ pub enum GameState {
#[default]
#[default]
Overworld
,
Overworld
,
Combat
,
Combat
,
GameOver
,
}
}
fn
setup
(
mut
commands
:
Commands
)
{
fn
setup
(
mut
commands
:
Commands
)
{
commands
.spawn
(
Camera2dBundle
::
default
());
commands
.spawn
(
Camera2dBundle
::
default
());
...
...
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