Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ fabric.properties

.vscode/launch.json
.vscode/settings.json
.vscode/tasks.json
*.blend[0-9]
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = []

[dependencies]
amethyst = "0.10.0"#{version = "0.10.0" , features = ["nightly"] }
amethyst_derive = "0.3.0"
log = "0.4.6"
serde = "1.0.82"
serde_derive = "1.0.82"
Expand Down
Binary file added assets/textures/ground_hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/rock_raider/default_hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions resources/color_palette.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"RED": (1.0,0.0,0.0,1.0),
"GREEN": (0.0,1.0,0.0,1.0),
"BLUE": (0.0,0.0,1.0,1.0),
"BEIGE": (0.5, 0.4, 0.0, 1.0),
}
155 changes: 155 additions & 0 deletions src/assetmanagement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use amethyst::{
assets::{AssetStorage, Loader},
config::Config,
renderer::{
Material, Mesh, MeshHandle, ObjFormat, PngFormat, Texture, TextureData, TextureHandle,
TextureMetadata,
},
};
use std::collections::HashMap;
use std::path::Path;

#[derive(Default)]
pub struct MeshManager {
assets: HashMap<String, MeshHandle>,
}

impl MeshManager {
pub fn get_handle_or_load<'a>(
&mut self,
path: &str,
loader: &Loader,
storage: &'a mut AssetStorage<Mesh>,
) -> MeshHandle {
if let Some(handle) = self.assets.get(path).cloned() {
return handle;
}

let handle = loader.load(Self::sanitize_path(&path), ObjFormat, (), (), storage);
self.assets.insert(String::from(path), handle.clone());
handle
}

/// Adds the foldername and file extension to the assets' name
#[inline(always)]
fn sanitize_path(path: &str) -> String {
format!("{}{}{}", "meshes/", path, ".obj")
}

pub fn is_default(&self) -> bool {
self.assets.len() == 0
}
}

impl PartialEq for MeshManager {
fn eq(&self, other: &Self) -> bool {
self.is_default() && other.is_default()
}
}

#[derive(Default)]
pub struct TextureManager {
assets: HashMap<String, TextureHandle>,
colors: HashMap<String, [f32; 4]>,
defaults: Option<Material>,
}

impl TextureManager {
pub fn material_from<'a>(
&mut self,
path: &str,
loader: &Loader,
mut storage: &'a mut AssetStorage<Texture>,
) -> Material {
Material {
albedo: self.get_handle_or_load(path, &loader, &mut storage),
..self.defaults.clone().unwrap()
}
}

pub fn get_handle_or_load<'a>(
&mut self,
path: &str,
loader: &Loader,
storage: &'a AssetStorage<Texture>,
) -> TextureHandle {
if let Some(handle) = self.assets.get(path).cloned() {
return handle;
}

if let Some(color) = self.colors.get(path) {
let handle = loader.load_from_data(TextureData::color(color.clone()), (), &storage);
self.assets.insert(String::from(path), handle.clone());
return handle;
};

let handle = loader.load(
Self::sanitize_path(&path),
PngFormat,
TextureMetadata::srgb(),
(),
storage,
);
self.assets.insert(String::from(path), handle.clone());
handle
}

/// Adds the foldername and file extension to the assets' name
#[inline(always)]
fn sanitize_path(path: &str) -> String {
format!("{}{}{}", "textures/", path, ".png")
}

pub fn initialize_with(&mut self, default: Material) {
self.defaults = Some(default);
// TODO
self.colors = HashMap::<String, [f32; 4]>::load(Path::new(&format!(
"{}/resources/color_palette.ron",
env!("CARGO_MANIFEST_DIR")
)));
}

pub fn is_default(&self) -> bool {
self.assets.len() == 0
}
}

impl PartialEq for TextureManager {
fn eq(&self, other: &Self) -> bool {
self.is_default() && other.is_default()
}
}

pub mod util {
use amethyst::{
assets::{AssetStorage, Loader},
ecs::{Entity, Write, WriteStorage},
renderer::{Material, Mesh, MeshHandle, Texture},
};
use assetmanagement::{MeshManager, TextureManager};

pub type TextureStorages<'a> = (
Write<'a, TextureManager>,
Write<'a, AssetStorage<Texture>>,
WriteStorage<'a, Material>,
);
pub type MeshStorages<'a> = (
Write<'a, MeshManager>,
Write<'a, AssetStorage<Mesh>>,
WriteStorage<'a, MeshHandle>,
);

pub fn attach_assets(
entity: Entity,
path: &str,
loader: &Loader,
(ref mut texture_manager, ref mut texture_storage, ref mut material_storage): &mut TextureStorages,
(ref mut mesh_manager, ref mut mesh_storage, ref mut mesh_handle_storage): &mut MeshStorages,
) {
let mesh = mesh_manager.get_handle_or_load(&path, &loader, mesh_storage);
let material = texture_manager.material_from(path, &loader, texture_storage);

material_storage.insert(entity, material).unwrap();
mesh_handle_storage.insert(entity, mesh).unwrap();
}
}
100 changes: 0 additions & 100 deletions src/assetmanagement/asset_loader.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/assetmanagement/mod.rs

This file was deleted.

91 changes: 0 additions & 91 deletions src/assetmanagement/util.rs

This file was deleted.

Loading