浏览代码

exit button working :)

amethyst
Isabelle L. 5 年前
父节点
当前提交
b0921e44db
共有 3 个文件被更改,包括 48 次插入9 次删除
  1. +1
    -1
      src/states/preload.rs
  2. +46
    -8
      src/states/titlescreen.rs
  3. +1
    -0
      src/systems/mod.rs

+ 1
- 1
src/states/preload.rs 查看文件

@@ -67,7 +67,7 @@ impl SimpleState for PreloadState {
} else if counter.is_complete() { } else if counter.is_complete() {
println!("completed load!"); println!("completed load!");
world.delete_all(); world.delete_all();
Trans::Switch(Box::new(crate::states::titlescreen::TitleScreenState))
Trans::Switch(Box::new(crate::states::titlescreen::TitleScreenState::default()))
} else { } else {
Trans::None Trans::None
} }


+ 46
- 8
src/states/titlescreen.rs 查看文件

@@ -2,9 +2,10 @@
use crate::resources::{font::FontResource, spritesheet::SpriteSheetMap}; use crate::resources::{font::FontResource, spritesheet::SpriteSheetMap};
use amethyst::{ use amethyst::{
core::transform::Transform, core::transform::Transform,
ecs::prelude::*,
prelude::*, prelude::*,
renderer::{Camera, SpriteRender}, renderer::{Camera, SpriteRender},
ui::{Anchor, UiImage, UiText, UiTransform},
ui::{Anchor, Interactable, UiEventType, UiImage, UiText, UiTransform},
window::ScreenDimensions, window::ScreenDimensions,
}; };
use nalgebra::Vector3; use nalgebra::Vector3;
@@ -14,11 +15,17 @@ const DEFAULT_SPRITE_WIDTH: f32 = 256.0;
const DEFAULT_SPRITE_HEIGHT: f32 = 144.0; const DEFAULT_SPRITE_HEIGHT: f32 = 144.0;


/// struct for title screen state /// struct for title screen state
pub struct TitleScreenState;
#[derive(Default)]
pub struct TitleScreenState {
exit_button: Option<Entity>,
settings_button: Option<Entity>,
load_button: Option<Entity>,
new_button: Option<Entity>,
}


impl TitleScreenState { impl TitleScreenState {
/// initialize the titlescreen /// initialize the titlescreen
fn init(&self, world: &mut World, dimensions: ScreenDimensions) {
fn init(&mut self, world: &mut World, dimensions: ScreenDimensions) {
self.init_camera(world, &dimensions); self.init_camera(world, &dimensions);
self.init_ui(world); self.init_ui(world);
self.init_scenery(world, &dimensions); self.init_scenery(world, &dimensions);
@@ -37,7 +44,7 @@ impl TitleScreenState {
} }


// initialize the ui // initialize the ui
fn init_ui(&self, world: &mut World) {
fn init_ui(&mut self, world: &mut World) {
// get the font handle // get the font handle
let font_handle = { let font_handle = {
if let Some(font_rsc) = world.try_fetch::<FontResource>() { if let Some(font_rsc) = world.try_fetch::<FontResource>() {
@@ -69,7 +76,7 @@ impl TitleScreenState {
.build(); .build();


// create the buttons // create the buttons
world
let exit_button = world
.create_entity() .create_entity()
.with(UiTransform::new( .with(UiTransform::new(
"exit_button".into(), "exit_button".into(),
@@ -83,8 +90,9 @@ impl TitleScreenState {
)) ))
.with(UiText::new(font_handle.clone(), "Exit".into(), [1.0, 1.0, 1.0, 1.0], 30.0)) .with(UiText::new(font_handle.clone(), "Exit".into(), [1.0, 1.0, 1.0, 1.0], 30.0))
.with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0])) .with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0]))
.with(Interactable)
.build(); .build();
world
let settings_button = world
.create_entity() .create_entity()
.with(UiTransform::new( .with(UiTransform::new(
"settings_button".into(), "settings_button".into(),
@@ -98,8 +106,9 @@ impl TitleScreenState {
)) ))
.with(UiText::new(font_handle.clone(), "Settings".into(), [1.0, 1.0, 1.0, 1.0], 30.0)) .with(UiText::new(font_handle.clone(), "Settings".into(), [1.0, 1.0, 1.0, 1.0], 30.0))
.with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0])) .with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0]))
.with(Interactable)
.build(); .build();
world
let load_button = world
.create_entity() .create_entity()
.with(UiTransform::new( .with(UiTransform::new(
"load_button".into(), "load_button".into(),
@@ -113,8 +122,9 @@ impl TitleScreenState {
)) ))
.with(UiText::new(font_handle.clone(), "Load Game".into(), [1.0, 1.0, 1.0, 1.0], 30.0)) .with(UiText::new(font_handle.clone(), "Load Game".into(), [1.0, 1.0, 1.0, 1.0], 30.0))
.with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0])) .with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0]))
.with(Interactable)
.build(); .build();
world
let new_button = world
.create_entity() .create_entity()
.with(UiTransform::new( .with(UiTransform::new(
"new_button".into(), "new_button".into(),
@@ -128,7 +138,14 @@ impl TitleScreenState {
)) ))
.with(UiText::new(font_handle, "New Game".into(), [1.0, 1.0, 1.0, 1.0], 30.0)) .with(UiText::new(font_handle, "New Game".into(), [1.0, 1.0, 1.0, 1.0], 30.0))
.with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0])) .with(UiImage::SolidColor([0.0, 0.0, 0.0, 1.0]))
.with(Interactable)
.build(); .build();

// set ui elements in state struct
self.exit_button = Some(exit_button);
self.settings_button = Some(settings_button);
self.load_button = Some(load_button);
self.new_button = Some(new_button);
} }


// initialize the scenery // initialize the scenery
@@ -179,4 +196,25 @@ impl SimpleState for TitleScreenState {


self.init(world, dimensions); self.init(world, dimensions);
} }

fn handle_event(
&mut self,
_data: StateData<'_, GameData<'_, '_>>,
event: StateEvent,
) -> SimpleTrans {
if let StateEvent::Ui(ui_event) = event {
if ui_event.target == self.exit_button.unwrap() {
if ui_event.event_type == UiEventType::Click {
println!("exit requested");
Trans::Quit
} else {
Trans::None
}
} else {
Trans::None
}
} else {
Trans::None
}
}
} }

+ 1
- 0
src/systems/mod.rs 查看文件

@@ -0,0 +1 @@


正在加载...
取消
保存