Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bevytest
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
Per Lindgren
bevytest
Commits
fc3c33e2
Commit
fc3c33e2
authored
3 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
refactor
parent
25dbc8c2
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/events.rs
+17
-0
17 additions, 0 deletions
src/events.rs
src/game_pad.rs
+88
-0
88 additions, 0 deletions
src/game_pad.rs
src/main.rs
+6
-101
6 additions, 101 deletions
src/main.rs
with
111 additions
and
101 deletions
src/events.rs
0 → 100644
+
17
−
0
View file @
fc3c33e2
use
bevy
::
prelude
::
*
;
pub
struct
LevelUpEvent
(
Entity
);
// fn player_level_up(mut ev_levelup: EventWriter<LevelUpEvent>, query: Query<(Entity, &PlayerXp)>) {
// for (entity, xp) in query.iter() {
// if xp.0 > 1000 {
// ev_levelup.send(LevelUpEvent(entity));
// }
// }
// }
// fn debug_levelups(mut ev_levelup: EventReader<LevelUpEvent>) {
// for ev in ev_levelup.iter() {
// eprintln!("Entity {:?} leveled up!", ev.0);
// }
// }
This diff is collapsed.
Click to expand it.
src/game_pad.rs
0 → 100644
+
88
−
0
View file @
fc3c33e2
use
bevy
::{
core
::
FixedTimestep
,
prelude
::
*
,
render
::{
camera
::
Camera
,
texture
::
FilterMode
},
};
use
crate
::
events
::
*
;
// Gamepad related
/// Simple resource to store the ID of the connected gamepad.
/// We need to know which gamepad to use for player input.
pub
struct
MyGamepad
(
Gamepad
);
pub
fn
gamepad_connections
(
mut
commands
:
Commands
,
my_gamepad
:
Option
<
Res
<
MyGamepad
>>
,
mut
gamepad_evr
:
EventReader
<
GamepadEvent
>
,
)
{
for
GamepadEvent
(
id
,
kind
)
in
gamepad_evr
.iter
()
{
match
kind
{
GamepadEventType
::
Connected
=>
{
println!
(
"New gamepad connected with ID: {:?}"
,
id
);
// if we don't have any gamepad yet, use this one
if
my_gamepad
.is_none
()
{
commands
.insert_resource
(
MyGamepad
(
*
id
));
}
}
GamepadEventType
::
Disconnected
=>
{
println!
(
"Lost gamepad connection with ID: {:?}"
,
id
);
// if it's the one we previously associated with the player,
// disassociate it:
if
let
Some
(
MyGamepad
(
old_id
))
=
my_gamepad
.as_deref
()
{
if
old_id
==
id
{
commands
.remove_resource
::
<
MyGamepad
>
();
}
}
}
// other events are irrelevant
_
=>
{}
}
}
}
pub
fn
gamepad_input
(
axes
:
Res
<
Axis
<
GamepadAxis
>>
,
buttons
:
Res
<
Input
<
GamepadButton
>>
,
my_gamepad
:
Option
<
Res
<
MyGamepad
>>
,
mut
ev_levelup
:
EventWriter
<
LevelUpEvent
>
,
)
{
let
gamepad
=
if
let
Some
(
gp
)
=
my_gamepad
{
// a gamepad is connected, we have the id
gp
.0
}
else
{
// no gamepad is connected
return
;
};
// The joysticks are represented using a separate axis for X and Y
let
axis_lx
=
GamepadAxis
(
gamepad
,
GamepadAxisType
::
LeftStickX
);
let
axis_ly
=
GamepadAxis
(
gamepad
,
GamepadAxisType
::
LeftStickY
);
if
let
(
Some
(
x
),
Some
(
y
))
=
(
axes
.get
(
axis_lx
),
axes
.get
(
axis_ly
))
{
// combine X and Y into one vector
let
left_stick_pos
=
Vec2
::
new
(
x
,
y
);
// implement a dead-zone to ignore small inputs
if
left_stick_pos
.length
()
>
0.1
{
// do something with the position of the left stick
}
}
let
jump_button
=
GamepadButton
(
gamepad
,
GamepadButtonType
::
South
);
let
heal_button
=
GamepadButton
(
gamepad
,
GamepadButtonType
::
East
);
if
buttons
.just_pressed
(
jump_button
)
{
// button pressed: make the player jump
println!
(
"jump"
);
}
if
buttons
.pressed
(
heal_button
)
{
// button being held down: heal the player
println!
(
"heal"
);
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
6
−
101
View file @
fc3c33e2
...
...
@@ -7,6 +7,12 @@ use bevy::{
use
bevy_ecs_tilemap
::{
prelude
::
*
,
MapQuery
};
use
std
::
f32
::
consts
::
PI
;
mod
game_pad
;
use
game_pad
::
*
;
mod
events
;
use
events
::
*
;
const
PLAYER_SHEET
:
&
str
=
"character_zombie_sheet.png"
;
fn
main
()
{
env_logger
::
Builder
::
from_default_env
()
...
...
@@ -168,28 +174,7 @@ impl Default for Player {
fn
player_movement
(
mut
query
:
Query
<
(
&
mut
Player
,
&
mut
Transform
,
&
mut
TextureAtlasSprite
)
>
,
keyboard_input
:
Res
<
Input
<
KeyCode
>>
,
// mut gilrs_input: ResMut<Gamepad>,
)
{
// while let Some(event) = gilrs_input.gilrs.next_event() {
// match event {
// Event {
// id,
// event: EventType::ButtonPressed(Button::South, _),
// ..
// } => {
// println!("Player {}: jump!", id)
// }
// Event {
// id,
// event: EventType::Disconnected,
// ..
// } => {
// println!("We lost player {}", id)
// }
// _ => (),
// };
// }
// println!("gilrs_input {}", gilrs_input.gilrs);
if
let
Ok
((
mut
player
,
mut
transform
,
mut
sprite
))
=
query
.single_mut
()
{
let
(
index
,
state
,
dir
)
=
if
player
.player_jump
{
// already in jump
...
...
@@ -299,83 +284,3 @@ pub fn camera(
transform
.translation.x
=
player
.player_x
*
(
1.
-
layer
.settings.parallax_x
)
/
MAP_SCALE
;
}
}
// Gamepad related
/// Simple resource to store the ID of the connected gamepad.
/// We need to know which gamepad to use for player input.
struct
MyGamepad
(
Gamepad
);
fn
gamepad_connections
(
mut
commands
:
Commands
,
my_gamepad
:
Option
<
Res
<
MyGamepad
>>
,
mut
gamepad_evr
:
EventReader
<
GamepadEvent
>
,
)
{
for
GamepadEvent
(
id
,
kind
)
in
gamepad_evr
.iter
()
{
match
kind
{
GamepadEventType
::
Connected
=>
{
println!
(
"New gamepad connected with ID: {:?}"
,
id
);
// if we don't have any gamepad yet, use this one
if
my_gamepad
.is_none
()
{
commands
.insert_resource
(
MyGamepad
(
*
id
));
}
}
GamepadEventType
::
Disconnected
=>
{
println!
(
"Lost gamepad connection with ID: {:?}"
,
id
);
// if it's the one we previously associated with the player,
// disassociate it:
if
let
Some
(
MyGamepad
(
old_id
))
=
my_gamepad
.as_deref
()
{
if
old_id
==
id
{
commands
.remove_resource
::
<
MyGamepad
>
();
}
}
}
// other events are irrelevant
_
=>
{}
}
}
}
fn
gamepad_input
(
axes
:
Res
<
Axis
<
GamepadAxis
>>
,
buttons
:
Res
<
Input
<
GamepadButton
>>
,
my_gamepad
:
Option
<
Res
<
MyGamepad
>>
,
)
{
let
gamepad
=
if
let
Some
(
gp
)
=
my_gamepad
{
// a gamepad is connected, we have the id
gp
.0
}
else
{
// no gamepad is connected
return
;
};
// The joysticks are represented using a separate axis for X and Y
let
axis_lx
=
GamepadAxis
(
gamepad
,
GamepadAxisType
::
LeftStickX
);
let
axis_ly
=
GamepadAxis
(
gamepad
,
GamepadAxisType
::
LeftStickY
);
if
let
(
Some
(
x
),
Some
(
y
))
=
(
axes
.get
(
axis_lx
),
axes
.get
(
axis_ly
))
{
// combine X and Y into one vector
let
left_stick_pos
=
Vec2
::
new
(
x
,
y
);
// implement a dead-zone to ignore small inputs
if
left_stick_pos
.length
()
>
0.1
{
// do something with the position of the left stick
}
}
let
jump_button
=
GamepadButton
(
gamepad
,
GamepadButtonType
::
South
);
let
heal_button
=
GamepadButton
(
gamepad
,
GamepadButtonType
::
East
);
if
buttons
.just_pressed
(
jump_button
)
{
// button pressed: make the player jump
println!
(
"jump"
);
}
if
buttons
.pressed
(
heal_button
)
{
// button being held down: heal the player
println!
(
"heal"
);
}
}
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