Menu text & raw character colors

This commit is contained in:
Pascal Engélibert 2022-08-21 23:46:07 +02:00
parent a52a3b84c8
commit 55278858cb
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
2 changed files with 49 additions and 36 deletions

View file

@ -17,7 +17,6 @@ pub struct GamePlugin;
impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_event::<LevelStartupEvent>()
.init_resource::<CharacterMaterials>()
.init_resource::<CharacterMeshes>()
.insert_resource(CurrentLevel(None))
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup))
@ -38,29 +37,6 @@ struct LevelStartupEvent(Entity);
struct CurrentLevel(Option<Entity>);
struct CharacterMaterials {
red: Handle<ColorMaterial>,
green: Handle<ColorMaterial>,
blue: Handle<ColorMaterial>,
yellow: Handle<ColorMaterial>,
magenta: Handle<ColorMaterial>,
cyan: Handle<ColorMaterial>,
}
impl FromWorld for CharacterMaterials {
fn from_world(world: &mut World) -> Self {
let mut materials = world.get_resource_mut::<Assets<ColorMaterial>>().unwrap();
Self {
red: materials.add(ColorMaterial::from(Color::rgba(1.0, 0., 0., 1.))),
green: materials.add(ColorMaterial::from(Color::rgba(0., 1.0, 0., 1.))),
blue: materials.add(ColorMaterial::from(Color::rgba(0., 0., 1.0, 1.))),
yellow: materials.add(ColorMaterial::from(Color::rgba(1.0, 1.0, 0., 1.))),
magenta: materials.add(ColorMaterial::from(Color::rgba(1.0, 0., 1.0, 1.))),
cyan: materials.add(ColorMaterial::from(Color::rgba(0., 1.0, 1.0, 1.))),
}
}
}
struct CharacterMeshes {
square: Mesh2dHandle,
}
@ -118,9 +94,9 @@ fn setup(
// This is a bad design, but it's the only way I found
fn post_setup_level(
mut commands: Commands,
character_materials: Res<CharacterMaterials>,
character_meshes: Res<CharacterMeshes>,
mut effects: ResMut<Assets<EffectAsset>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
mut level_startup_event: EventReader<LevelStartupEvent>,
) {
@ -132,28 +108,31 @@ fn post_setup_level(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
character_materials.red.clone(),
Transform::from_xyz(-128., -64., 0.),
Color::RED,
);
spawn_character(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
character_materials.green.clone(),
Transform::from_xyz(0., -64., 0.),
Color::GREEN,
);
spawn_character(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
character_materials.blue.clone(),
Transform::from_xyz(128., -64., 0.),
Color::BLUE,
);
}
}
@ -163,10 +142,11 @@ fn spawn_character(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>,
material: Handle<ColorMaterial>,
transform: Transform,
color: Color,
) {
let character_id = CharacterId(
character_id_list
@ -178,13 +158,25 @@ fn spawn_character(
character_id_list.0.insert(character_id);
let mut gradient = Gradient::new();
gradient.add_key(0.0, Vec4::new(1.0, 0.1, 0.1, 0.0));
gradient.add_key(0.2, Vec4::new(1.0, 0.1, 0.1, 1.0));
gradient.add_key(1.0, Vec4::new(1.0, 0.1, 0.1, 0.0));
gradient.add_key(
0.0,
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
);
gradient.add_key(
0.2,
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 1.)),
);
gradient.add_key(
1.0,
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
);
commands
.spawn_bundle(MaterialMesh2dBundle {
mesh: character_meshes.square.clone(),
material,
material: materials.add(ColorMaterial::from(color)),
transform,
..default()
})
@ -204,7 +196,7 @@ fn spawn_character(
effect: ParticleEffect::new(
effects.add(
EffectAsset {
name: "Red particles".into(),
name: "Particles".into(),
capacity: 4096,
spawner: Spawner::rate(30.0.into())
.with_active(selected_character_id.0.is_none()),
@ -236,8 +228,10 @@ fn spawn_character(
fn collision_event_system(
mut commands: Commands,
//materials: ResMut<Assets<ColorMaterial>>,
current_level: Res<CurrentLevel>,
mut collision_events: EventReader<CollisionEvent>,
//character_query: Query<(&CharacterId, &Handle<ColorMaterial>)>,
character_query: Query<&CharacterId>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
) {
@ -245,9 +239,12 @@ fn collision_event_system(
for collision_event in collision_events.iter() {
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
if flags.is_empty() {
//if let (Ok((c1_id, c1_material)), Ok((c2_id, c2_material))) =
if let (Ok(c1_id), Ok(c2_id)) =
(character_query.get(*e1), character_query.get(*e2))
{
//c1_material.color = (Vec4::from(c1_material.color) + Vec4::from(c2_material.color)).into();
let (mut selected_character_id, mut character_id_list) =
level_query.get_mut(level_entity).unwrap();
character_id_list.0.remove(c2_id);

View file

@ -19,13 +19,29 @@ impl Plugin for MenuPlugin {
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.get_handle("Cantarell-VF.otf");
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"BEVYJAM",
TextStyle {
font: font.clone(),
font_size: 48.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
transform: Transform::from_xyz(0., -128.0, 0.),
..Default::default()
})
.insert(Menu());
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"Press ENTER",
TextStyle {
font: asset_server.get_handle("Cantarell-VF.otf"),
font_size: 64.0,
font,
font_size: 32.0,
color: Color::WHITE,
},
)