-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41e8c67
commit 7487cf6
Showing
4 changed files
with
91 additions
and
20 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::map::Position; | ||
|
||
const TILE_SIZE: u32 = 64; | ||
|
||
#[derive(Resource)] | ||
pub struct Material { | ||
texture: Handle<Image>, | ||
layout: Handle<TextureAtlasLayout>, | ||
} | ||
|
||
impl FromWorld for Material { | ||
fn from_world(world: &mut World) -> Self { | ||
let texture: Handle<Image> = world | ||
.get_resource::<AssetServer>() | ||
.unwrap() | ||
.load("pot/sprite.png"); | ||
|
||
let layout = world | ||
.get_resource_mut::<Assets<TextureAtlasLayout>>() | ||
.unwrap() | ||
.add(TextureAtlasLayout::from_grid( | ||
Vec2::new(TILE_SIZE as f32, TILE_SIZE as f32), | ||
7, | ||
1, | ||
None, | ||
None, | ||
)); | ||
|
||
Self { texture, layout } | ||
} | ||
} | ||
|
||
#[derive(Bundle)] | ||
pub struct Pot { | ||
sprite_sheet: SpriteSheetBundle, | ||
position: Position, | ||
} | ||
|
||
impl Pot { | ||
pub fn new(position: Position, material: &Material) -> Self { | ||
Self { | ||
sprite_sheet: SpriteSheetBundle { | ||
atlas: TextureAtlas { | ||
layout: material.layout.clone(), | ||
index: 0, | ||
}, | ||
transform: Transform { | ||
scale: Vec3::new(0.8, 0.8, 1.0), | ||
..Default::default() | ||
}, | ||
texture: material.texture.clone(), | ||
..Default::default() | ||
}, | ||
position, | ||
} | ||
} | ||
} | ||
|
||
fn render(mut query: Query<(&mut Transform, &Position)>) { | ||
for (mut t, p) in query.iter_mut() { | ||
t.translation = Vec3::new( | ||
(p.x * TILE_SIZE as i32) as f32, | ||
(p.y * TILE_SIZE as i32) as f32, | ||
1.0, | ||
); | ||
} | ||
} | ||
|
||
pub struct PotPlugin; | ||
|
||
impl Plugin for PotPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.init_resource::<Material>().add_systems(Update, render); | ||
} | ||
} |