-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
combat.rs
186 lines (158 loc) · 4.73 KB
/
combat.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#![allow(clippy::type_complexity)]
use bevy_ecs::query::QueryData;
use rand::Rng;
use valence::entity::EntityStatuses;
use valence::math::Vec3Swizzles;
use valence::prelude::*;
const SPAWN_Y: i32 = 64;
const ARENA_RADIUS: i32 = 32;
/// Attached to every client.
#[derive(Component, Default)]
struct CombatState {
/// The tick the client was last attacked.
last_attacked_tick: i64,
has_bonus_knockback: bool,
}
pub fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(EventLoopUpdate, handle_combat_events)
.add_systems(
Update,
(
init_clients,
despawn_disconnected_clients,
teleport_oob_clients,
),
)
.run();
}
fn setup(
mut commands: Commands,
server: Res<Server>,
dimensions: Res<DimensionTypeRegistry>,
biomes: Res<BiomeRegistry>,
) {
let mut layer = LayerBundle::new(ident!("overworld"), &dimensions, &biomes, &server);
for z in -5..5 {
for x in -5..5 {
layer.chunk.insert_chunk([x, z], UnloadedChunk::new());
}
}
let mut rng = rand::thread_rng();
// Create circular arena.
for z in -ARENA_RADIUS..ARENA_RADIUS {
for x in -ARENA_RADIUS..ARENA_RADIUS {
let dist = f64::hypot(f64::from(x), f64::from(z)) / f64::from(ARENA_RADIUS);
if dist > 1.0 {
continue;
}
let block = if rng.gen::<f64>() < dist {
BlockState::STONE
} else {
BlockState::DEEPSLATE
};
for y in 0..SPAWN_Y {
layer.chunk.set_block([x, y, z], block);
}
}
}
commands.spawn(layer);
}
fn init_clients(
mut clients: Query<
(
Entity,
&mut EntityLayerId,
&mut VisibleChunkLayer,
&mut VisibleEntityLayers,
&mut Position,
&mut GameMode,
),
Added<Client>,
>,
layers: Query<Entity, (With<ChunkLayer>, With<EntityLayer>)>,
mut commands: Commands,
) {
for (
entity,
mut layer_id,
mut visible_chunk_layer,
mut visible_entity_layers,
mut pos,
mut game_mode,
) in &mut clients
{
let layer = layers.single();
layer_id.0 = layer;
visible_chunk_layer.0 = layer;
visible_entity_layers.0.insert(layer);
pos.set([0.0, f64::from(SPAWN_Y) 1.0, 0.0]);
*game_mode = GameMode::Creative;
commands.entity(entity).insert(CombatState::default());
}
}
#[derive(QueryData)]
#[query_data(mutable)]
struct CombatQuery {
client: &'static mut Client,
pos: &'static Position,
state: &'static mut CombatState,
statuses: &'static mut EntityStatuses,
}
fn handle_combat_events(
server: Res<Server>,
mut clients: Query<CombatQuery>,
mut sprinting: EventReader<SprintEvent>,
mut interact_entity: EventReader<InteractEntityEvent>,
) {
for &SprintEvent { client, state } in sprinting.read() {
if let Ok(mut client) = clients.get_mut(client) {
client.state.has_bonus_knockback = state == SprintState::Start;
}
}
for &InteractEntityEvent {
client: attacker_client,
entity: victim_client,
..
} in interact_entity.read()
{
let Ok([mut attacker, mut victim]) = clients.get_many_mut([attacker_client, victim_client])
else {
// Victim or attacker does not exist, or the attacker is attacking itself.
continue;
};
if server.current_tick() - victim.state.last_attacked_tick < 10 {
// Victim is still on attack cooldown.
continue;
}
victim.state.last_attacked_tick = server.current_tick();
let victim_pos = victim.pos.0.xz();
let attacker_pos = attacker.pos.0.xz();
let dir = (victim_pos - attacker_pos).normalize().as_vec2();
let knockback_xz = if attacker.state.has_bonus_knockback {
18.0
} else {
8.0
};
let knockback_y = if attacker.state.has_bonus_knockback {
8.432
} else {
6.432
};
victim
.client
.set_velocity([dir.x * knockback_xz, knockback_y, dir.y * knockback_xz]);
attacker.state.has_bonus_knockback = false;
victim.client.trigger_status(EntityStatus::PlayAttackSound);
victim.statuses.trigger(EntityStatus::PlayAttackSound);
}
}
fn teleport_oob_clients(mut clients: Query<&mut Position, With<Client>>) {
for mut pos in &mut clients {
if pos.0.y < 0.0 {
pos.set([0.0, f64::from(SPAWN_Y), 0.0]);
}
}
}