main.rs (2825B)
1 use crate::plugins::world::{SPRITE_HEIGHT, SPRITE_WIDTH}; 2 use crate::plugins::{ 3 player::PLAYER_START, 4 world::{MainCamera, WorldState}, 5 PlayerPlugin, WorldPlugin, 6 }; 7 use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; 8 use bevy::render::camera::{OrthographicProjection, ScalingMode}; 9 use bevy::{prelude::*, window::WindowMode}; 10 use std::collections::HashMap; 11 12 mod entities; 13 mod plugins; 14 15 #[derive(Default)] 16 pub struct AssetsLoading(Vec<HandleUntyped>); 17 18 #[derive(Default, Clone)] 19 pub struct GameState { 20 pub spawned: bool, 21 pub world_state: WorldState, 22 pub asset_map: HashMap<String, Handle<Texture>>, 23 } 24 25 fn main() { 26 App::build() 27 .insert_resource(WindowDescriptor { 28 title: "Random test game".to_string(), 29 width: 1024., 30 height: 720., 31 vsync: false, 32 resizable: true, 33 mode: WindowMode::Windowed, 34 ..Default::default() 35 }) 36 .init_resource::<GameState>() 37 .init_resource::<AssetsLoading>() 38 .add_plugins(DefaultPlugins) 39 .add_plugin(FrameTimeDiagnosticsPlugin::default()) 40 .add_plugin(LogDiagnosticsPlugin::default()) 41 .add_startup_system_to_stage(StartupStage::PreStartup, setup.system()) 42 .add_plugin(WorldPlugin) 43 .add_plugin(PlayerPlugin) 44 .add_system(bevy::input::system::exit_on_esc_system.system()) 45 .run(); 46 } 47 48 fn setup( 49 mut commands: Commands, 50 mut loading: ResMut<AssetsLoading>, 51 asset_server: Res<AssetServer>, 52 mut game_state: ResMut<GameState>, 53 ) { 54 commands 55 .spawn_bundle(OrthographicCameraBundle { 56 orthographic_projection: OrthographicProjection { 57 far: 1024.0, // This gives us 1024 layers, 58 scale: 200.0, // How many pixels high in the game 59 scaling_mode: ScalingMode::FixedVertical, 60 ..Default::default() 61 }, 62 transform: Transform::from_translation(Vec3::new( 63 SPRITE_WIDTH * PLAYER_START.0 as f32, 64 SPRITE_HEIGHT * PLAYER_START.1 as f32, 65 0.0, 66 )), 67 ..OrthographicCameraBundle::new_2d() 68 }) 69 .insert(PLAYER_START) 70 .insert(MainCamera); 71 72 let sunny_texture_handle = asset_server.load("tilesets/SunnyLand_by_Ansimuz-extended.png"); 73 loading.0.push(sunny_texture_handle.clone_untyped()); 74 game_state 75 .asset_map 76 .insert("Sunnyland".into(), sunny_texture_handle); 77 let dungeon_tileset_texture_handle = 78 asset_server.load("tilesets/0x72_DungeonTilesetII_v1.3.png"); 79 loading 80 .0 81 .push(dungeon_tileset_texture_handle.clone_untyped()); 82 game_state 83 .asset_map 84 .insert("DungeonTileset".into(), dungeon_tileset_texture_handle); 85 }