-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
172 lines (160 loc) · 4.9 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use bevy::asset::AssetMetaCheck;
use bevy::{prelude::*, sprite::Anchor};
use constants::*;
use piece::*;
use stats::*;
mod constants;
mod piece;
mod quant;
mod stats;
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
pub enum GameState {
#[default]
Playing,
Lost,
}
#[derive(Resource, PartialEq, Eq, Clone, Copy)]
pub enum Objective {
Measure0,
Measure1,
MeasurePhi,
MeasurePsi,
}
#[derive(Component)]
pub struct ObjectiveLabel;
#[derive(Resource)]
pub struct DropSound(Handle<AudioSource>);
#[derive(Resource)]
pub struct ClearSound(Handle<AudioSource>);
#[derive(Resource)]
pub struct QuadrupleClearSound(Handle<AudioSource>);
#[derive(Resource)]
pub struct MeasureImage(Handle<Image>);
fn main() {
App::new()
.insert_resource(ClearColor(Color::WHITE))
.insert_resource(PieceInfo {
last_drop: 0.,
shape: Shape::I,
rotation: 0,
pieces_since_objective: 0,
})
.insert_resource(Score { score: 0 })
.insert_resource(Objective::Measure0)
.insert_resource(AssetMetaCheck::Never)
.add_state::<GameState>()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
fit_canvas_to_parent: true,
..default()
}),
..default()
}))
.add_systems(Startup, (setup_camera, setup_background))
.add_systems(
Update,
(
check_over,
generate_new_piece.after(check_over),
check_measurment,
falling_piece,
move_piece,
rotate_piece,
clear_columns,
drop_piece,
move_empty_lines,
edit_objective_label,
edit_scoreboard,
)
.run_if(in_state(GameState::Playing)),
)
.add_systems(Update, check_game_restart)
.add_systems(OnEnter(GameState::Lost), show_lose_screen)
.add_systems(
PostUpdate,
(
update_block_transforms,
hide_outside_blocks,
move_control_wires,
),
)
.run();
}
fn setup_camera(mut commands: Commands) {
let mut camera = Camera2dBundle::default();
camera.projection.scaling_mode = bevy::render::camera::ScalingMode::AutoMin {
min_width: REFERENCE_SCREEN_WIDTH,
min_height: REFERENCE_SCREEN_HEIGHT,
};
commands.spawn(camera);
}
pub fn setup_background(mut commands: Commands, asset_server: Res<AssetServer>) {
for y in 1..Y_COUNT + 1 {
commands.spawn(SpriteBundle {
sprite: Sprite {
color: Color::BLACK,
custom_size: Some(Vec2::new(X_GAPS * (X_COUNT as f32 - 1.), WIRE_WIDTH)),
..default()
},
transform: Transform::from_xyz(
0.,
y as f32 * Y_GAPS - REFERENCE_SCREEN_HEIGHT / 2.,
0.,
),
..default()
});
commands.spawn(SpriteBundle {
texture: asset_server.load("0.png"),
transform: Transform::from_xyz(
-REFERENCE_SCREEN_WIDTH / 2. + INITIAL_STATE_DISTANCE_FROM_RIGHT,
y as f32 * Y_GAPS - REFERENCE_SCREEN_HEIGHT / 2.,
1.,
),
..default()
});
}
commands.spawn((
Text2dBundle {
text: Text::from_section(
"",
TextStyle {
font_size: OBJECTIVE_FONT_SIZE,
color: Color::BLACK,
..default()
},
),
transform: Transform::from_xyz(0., -REFERENCE_SCREEN_HEIGHT / 2. + OBJECTIVE_GAP, 3.),
text_anchor: Anchor::BottomCenter,
..default()
},
ObjectiveLabel,
));
commands.spawn((
Text2dBundle {
text: Text::from_section(
"",
TextStyle {
font_size: SCORE_FONT_SIZE,
color: Color::BLACK,
..default()
},
),
transform: Transform::from_xyz(
-REFERENCE_SCREEN_WIDTH / 2. + SCORE_GAP,
REFERENCE_SCREEN_HEIGHT / 2. - SCORE_GAP,
1.,
),
text_anchor: Anchor::TopLeft,
..default()
},
Scoreboard,
));
commands.spawn(AudioBundle {
source: asset_server.load("music.ogg"),
settings: PlaybackSettings::LOOP,
});
commands.insert_resource(DropSound(asset_server.load("drop.ogg")));
commands.insert_resource(ClearSound(asset_server.load("clear.ogg")));
commands.insert_resource(QuadrupleClearSound(asset_server.load("quadclear.ogg")));
commands.insert_resource(MeasureImage(asset_server.load("measure.png")));
}