forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.rs
153 lines (135 loc) · 4.75 KB
/
animation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
render::texture::ImageSettings,
};
use bevy_ecs_tilemap::prelude::*;
use bevy_ecs_tilemap::tiles::{AnimatedTile, TileBundle, TilePos, TileStorage, TileTexture};
use bevy_ecs_tilemap::{TilemapBundle, TilemapPlugin};
use rand::seq::IteratorRandom;
use rand::thread_rng;
mod helpers;
struct TilemapMetadata {
size: TilemapSize,
tile_size: TilemapTileSize,
grid_size: TilemapGridSize,
}
const BACKGROUND: &'static str = "tiles.png";
const BACKGROUND_METADATA: TilemapMetadata = TilemapMetadata {
size: TilemapSize { x: 20, y: 20 },
tile_size: TilemapTileSize { x: 16.0, y: 16.0 },
grid_size: TilemapGridSize { x: 16.0, y: 16.0 },
};
const FLOWERS: &'static str = "flower_sheet.png";
const FLOWERS_METADATA: TilemapMetadata = TilemapMetadata {
size: TilemapSize { x: 10, y: 10 },
tile_size: TilemapTileSize { x: 32.0, y: 32.0 },
grid_size: TilemapGridSize { x: 16.0, y: 16.0 },
};
fn create_background(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture_handle: Handle<Image> = asset_server.load(BACKGROUND);
let tilemap_entity = commands.spawn().id();
let TilemapMetadata {
size,
grid_size,
tile_size,
} = BACKGROUND_METADATA;
let mut tilemap_storage = TileStorage::empty(size);
for x in 0..size.x {
for y in 0..size.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn()
.insert_bundle(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
..Default::default()
})
.id();
// Here we let the tile storage component know what tiles we have.
tilemap_storage.set(&tile_pos, Some(tile_entity));
}
}
commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
size,
grid_size,
tile_size,
storage: tilemap_storage,
texture: TilemapTexture(texture_handle.clone()),
transform: bevy_ecs_tilemap::helpers::get_centered_transform_2d(&size, &tile_size, 0.0),
..Default::default()
});
}
fn create_animated_flowers(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture_handle: Handle<Image> = asset_server.load(FLOWERS);
let TilemapMetadata {
size,
grid_size,
tile_size,
} = FLOWERS_METADATA;
let mut tilemap_storage = TileStorage::empty(size);
let tilemap_entity = commands.spawn().id();
// Choose 10 random tiles to contain flowers.
let mut rng = thread_rng();
let mut indices: Vec<(u32, u32)> = Vec::with_capacity((size.x * size.y) as usize);
for x in 0..size.x {
for y in 0..size.y {
indices.push((x, y));
}
}
for (x, y) in indices.into_iter().choose_multiple(&mut rng, 10) {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn()
.insert_bundle(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture: TileTexture(0),
..Default::default()
})
.id();
tilemap_storage.set(&tile_pos, Some(tile_entity));
// To enable animation, we must insert the `AnimatedTile` component on
// each tile that is to be animated.
commands.entity(tile_entity).insert(AnimatedTile {
start: 0,
end: 13,
speed: 0.95,
});
}
commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size,
size,
storage: tilemap_storage,
texture: TilemapTexture(texture_handle.clone()),
tile_size,
transform: bevy_ecs_tilemap::helpers::get_centered_transform_2d(&size, &tile_size, 1.0),
..Default::default()
});
}
fn startup(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default());
}
fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Animated Map Example"),
..Default::default()
})
.insert_resource(ImageSettings::default_nearest())
.add_plugins(DefaultPlugins)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(TilemapPlugin)
.add_startup_system(startup)
.add_startup_system(create_background)
.add_startup_system(create_animated_flowers)
.add_system(helpers::camera::movement)
.run();
}