Skip to content

Commit 9279851

Browse files
committed
lifetime component, door sound emitter component
1 parent e6e378e commit 9279851

File tree

12 files changed

+162
-54
lines changed

12 files changed

+162
-54
lines changed

src/dungeon/door/door.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::dungeon::dungeon::Dungeon;
22
use crate::dungeon::entities::block_appearance::BlockAppearance;
3+
use crate::dungeon::entities::components::Lifetime;
34
use crate::dungeon::entities::moving_block_behaviour::MovingBlockBehaviour;
45
use crate::dungeon::seeded_rng::seeded_rng;
56
use glam::{dvec3, ivec3, IVec3};
@@ -158,11 +159,16 @@ impl Door {
158159
BlockAppearance {
159160
block: self.get_block(),
160161
},
161-
MovingBlockBehaviour {
162-
block: ivec3(x, y, z),
163-
total_ticks: 20,
164-
difference: -0.25,
165-
}
162+
(
163+
MovingBlockBehaviour {
164+
block: ivec3(x, y, z),
165+
remove_block_in_tick: 20,
166+
difference: -0.25,
167+
},
168+
Lifetime {
169+
ticks: 20
170+
}
171+
)
166172
);
167173
world.chunk_grid.set_block_at(Block::Barrier, x, y, z);
168174
}

src/dungeon/door/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod door;
2-
pub(super) mod door_positions;
2+
pub(super) mod door_positions;
3+
pub mod sound_emitter;

src/dungeon/door/sound_emitter.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::dungeon::dungeon::Dungeon;
2+
use bevy_ecs::prelude::Component;
3+
use server::constants::Sound;
4+
use server::entity::components::EntityBehaviour;
5+
use server::entity::entity::MinecraftEntity;
6+
7+
#[derive(Component)]
8+
pub struct DoorSoundEmitter {
9+
pub sound: Sound,
10+
pub volume: f32,
11+
pub pitch: f32,
12+
}
13+
14+
impl EntityBehaviour<Dungeon> for DoorSoundEmitter {
15+
fn tick(entity: &mut MinecraftEntity<Dungeon>, component: &mut Self) {
16+
if entity.ticks_existed.is_multiple_of(5) {
17+
entity.world_mut().play_sound_at(
18+
component.sound,
19+
component.volume,
20+
component.pitch,
21+
entity.position,
22+
);
23+
}
24+
}
25+
}

src/dungeon/entities/block_appearance.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,60 @@ impl EntityAppearance<Dungeon> for BlockAppearance {
9595
packet.entities.push(VarInt(entity.id));
9696
packet.entities.push(VarInt(entity.id + 1));
9797
}
98+
}
99+
100+
// doesn't prevent clientside gravity whatsoever, used for falling blocks and in ice fill
101+
#[derive(Component)]
102+
pub struct FallingBlockAppearance {
103+
pub block: Block
104+
}
105+
106+
impl EntityAppearance<Dungeon> for FallingBlockAppearance {
107+
108+
fn enter_player_view(&self, entity: &MinecraftEntity<Dungeon>, player: &mut Player<DungeonPlayer>) {
109+
let DVec3 { x, y, z } = entity.position;
110+
111+
let object_data = {
112+
let block_state_id = self.block.get_blockstate_id() as i32;
113+
let block_id = block_state_id >> 4;
114+
let metadata = block_state_id & 0b1111;
115+
block_id | (metadata << 12)
116+
};
117+
118+
player.write_packet(&SpawnObject {
119+
entity_id: entity.id,
120+
variant: ObjectVariant::FallingBlock,
121+
x,
122+
y,
123+
z,
124+
pitch: 0.0,
125+
yaw: 0.0,
126+
data: object_data,
127+
velocity_x: 0.0,
128+
velocity_y: 0.0,
129+
velocity_z: 0.0,
130+
});
131+
}
132+
133+
fn leave_player_view(&self, entity: &MinecraftEntity<Dungeon>, player: &mut Player<DungeonPlayer>) {
134+
player.write_packet(&DestroyEntites {
135+
entities: vec![VarInt(entity.id)],
136+
})
137+
}
138+
139+
fn update_position(&self, entity: &MinecraftEntity<Dungeon>, packet_buffer: &mut PacketBuffer) {
140+
packet_buffer.write_packet(&EntityTeleport {
141+
entity_id: entity.id,
142+
pos_x: entity.position.x,
143+
pos_y: entity.position.y,
144+
pos_z: entity.position.z,
145+
yaw: 0.0,
146+
pitch: 0.0,
147+
on_ground: false,
148+
});
149+
}
150+
151+
fn destroy(&self, entity: &MinecraftEntity<Dungeon>, packet: &mut DestroyEntites) {
152+
packet.entities.push(VarInt(entity.id));
153+
}
98154
}

src/dungeon/entities/components.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::dungeon::dungeon::Dungeon;
2+
use bevy_ecs::prelude::Component;
3+
use server::entity::components::EntityBehaviour;
4+
use server::entity::entity::MinecraftEntity;
5+
6+
/// entities with this will only live for the provided amount of time
7+
#[derive(Component)]
8+
pub struct Lifetime {
9+
pub ticks: u32
10+
}
11+
12+
impl EntityBehaviour<Dungeon> for Lifetime {
13+
fn tick(entity: &mut MinecraftEntity<Dungeon>, component: &mut Self) {
14+
if entity.ticks_existed > component.ticks {
15+
entity.destroy()
16+
}
17+
}
18+
}

src/dungeon/entities/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod npc;
22
pub mod block_appearance;
3-
pub mod moving_block_behaviour;
3+
pub mod moving_block_behaviour;
4+
pub mod components;

src/dungeon/entities/moving_block_behaviour.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,25 @@ use crate::dungeon::dungeon::Dungeon;
22
use bevy_ecs::prelude::Component;
33
use glam::IVec3;
44
use server::block::Block;
5-
use server::constants::Sound;
65
use server::entity::components::EntityBehaviour;
76
use server::entity::entity::MinecraftEntity;
87

98
#[derive(Component)]
109
pub struct MovingBlockBehaviour {
1110
pub block: IVec3,
12-
pub total_ticks: u32,
11+
pub remove_block_in_tick: u32,
1312
pub difference: f64,
1413
}
1514

1615
impl EntityBehaviour<Dungeon> for MovingBlockBehaviour {
1716
fn tick(entity: &mut MinecraftEntity<Dungeon>, component: &mut Self) {
1817
entity.position.y += component.difference;
1918

20-
if entity.ticks_existed == component.total_ticks {
19+
if entity.ticks_existed == component.remove_block_in_tick {
2120
let world = entity.world_mut();
2221

2322
let IVec3 { x, y, z } = component.block;
2423
world.chunk_grid.set_block_at(Block::Air, x, y, z);
25-
entity.destroy()
26-
}
27-
}
28-
}
29-
30-
// scuffed
31-
#[derive(Component)]
32-
pub struct DoorSoundEmitter {
33-
pub sound: Sound,
34-
pub volume: f32,
35-
pub pitch: f32,
36-
}
37-
38-
impl EntityBehaviour<Dungeon> for DoorSoundEmitter {
39-
fn tick(entity: &mut MinecraftEntity<Dungeon>, component: &mut Self) {
40-
if entity.ticks_existed.is_multiple_of(5) {
41-
entity.world_mut().play_sound_at(
42-
component.sound,
43-
component.volume,
44-
component.pitch,
45-
entity.position,
46-
);
47-
}
48-
if entity.ticks_existed == 50 {
49-
entity.destroy()
5024
}
5125
}
5226
}

src/dungeon/items/ender_pearl.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::dungeon::dungeon::Dungeon;
22
use crate::dungeon::dungeon_player::DungeonPlayer;
3+
use crate::dungeon::entities::components::Lifetime;
34
use crate::dungeon::items::dungeon_items::DungeonItem;
45
use bevy_ecs::prelude::Component;
56
use glam::dvec3;
@@ -35,7 +36,14 @@ impl DungeonItem for EnderPearl {
3536
player.yaw,
3637
player.pitch,
3738
EnderPearlAppearance,
38-
EnderPearlBehaviour { player_id: player.client_id },
39+
(
40+
EnderPearlBehaviour {
41+
player_id: player.client_id
42+
},
43+
Lifetime {
44+
ticks: 200
45+
}
46+
)
3947
);
4048

4149
player.play_sound(Sound::GhastFireball, 0.2, 1.0);
@@ -107,11 +115,6 @@ impl EntityBehaviour<Dungeon> for EnderPearlBehaviour {
107115
}
108116
entity.destroy()
109117
}
110-
111-
// prevent falling into void infinitely or something
112-
if entity.ticks_existed == 100 {
113-
entity.destroy()
114-
}
115118
}
116119
}
117120

src/dungeon/room/puzzles/ice_fill.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use crate::dungeon::door::sound_emitter::DoorSoundEmitter;
12
use crate::dungeon::dungeon::Dungeon;
23
use crate::dungeon::dungeon_player::DungeonPlayer;
3-
use crate::dungeon::entities::block_appearance::BlockAppearance;
4-
use crate::dungeon::entities::moving_block_behaviour::{DoorSoundEmitter, MovingBlockBehaviour};
4+
use crate::dungeon::entities::block_appearance::{BlockAppearance, FallingBlockAppearance};
5+
use crate::dungeon::entities::components::Lifetime;
6+
use crate::dungeon::entities::moving_block_behaviour::MovingBlockBehaviour;
57
use crate::dungeon::room::room::{Room, RoomStatus};
68
use crate::dungeon::room::room_implementation::RoomImplementation;
79
use crate::dungeon::seeded_rng::seeded_rng;
@@ -142,6 +144,21 @@ impl RoomImplementation for IceFillPuzzle {
142144
for position in obstacles.iter() {
143145
let IVec3 { x, y, z } = *position;
144146
world.chunk_grid.set_block_at(Block::Air, x, y, z);
147+
let mut position = position.as_dvec3();
148+
position.x += 0.5;
149+
position.z += 0.5;
150+
151+
world.spawn_entity(
152+
position,
153+
0.0,
154+
0.0,
155+
FallingBlockAppearance {
156+
block: Block::PolishedAndesite,
157+
},
158+
Lifetime {
159+
ticks: 15,
160+
}
161+
);
145162
}
146163
self.layer = Layer::Respawning { in_ticks: 60 };
147164
}
@@ -174,12 +191,16 @@ impl IceFillPuzzle {
174191
sound_emitter,
175192
0.0,
176193
0.0,
177-
NoAppearance,
178-
DoorSoundEmitter {
179-
sound: Sound::RandomWoodClick,
180-
volume: 2.0,
181-
pitch: 0.5,
182-
}
194+
NoAppearance, (
195+
DoorSoundEmitter {
196+
sound: Sound::RandomWoodClick,
197+
volume: 2.0,
198+
pitch: 0.5,
199+
},
200+
Lifetime {
201+
ticks: 50
202+
}
203+
)
183204
);
184205

185206
for x in 13..=17 {
@@ -196,7 +217,7 @@ impl IceFillPuzzle {
196217
},
197218
MovingBlockBehaviour {
198219
block,
199-
total_ticks: 40,
220+
remove_block_in_tick: 20,
200221
difference: 0.2,
201222
}
202223
);

src/dungeon/room/puzzles/teleport_maze.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl Default for TeleportMazePuzzle {
126126

127127
remaining.shuffle(rng);
128128

129+
// this broke bru
129130
let mut index = 0;
130131
while index + 1 < remaining.len() {
131132
if remaining[index].0 == remaining[index + 1].0 {

0 commit comments

Comments
 (0)