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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Provides access to spirits.

use crate::{
    prelude::*, CanFrom, Destructible, DestructibleID, LivingDestructible, LivingDestructibleID,
};
use js_sys::{Array, Float64Array, Object};
use std::convert::TryFrom;
use wasm_bindgen::prelude::*;

// Spirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`Spirit`].
    #[wasm_bindgen(extends = DestructibleID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type SpiritID;

    /// A spirit.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = Destructible, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type Spirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &Spirit) -> SpiritID;

    #[wasm_bindgen(method, getter)]
    pub fn merged(this: &Spirit) -> Vec<DeadSpiritID>;

    #[wasm_bindgen(method, getter)]
    pub fn move_speed(this: &Spirit) -> f64;

    #[wasm_bindgen(method, getter)]
    pub fn mark(this: &Spirit) -> String;
}

impl CanFrom<Destructible> for Spirit {
    #[inline]
    fn can_from(value: &Destructible) -> bool {
        !<Base as CanFrom<Destructible>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Destructible>, Error = Base for Spirit);

// FriendlySpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`FriendlySpirit`].
    #[wasm_bindgen(extends = SpiritID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type FriendlySpiritID;

    /// A spirit that belongs to you.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = Spirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type FriendlySpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &FriendlySpirit) -> FriendlySpiritID;

    #[wasm_bindgen(method, getter)]
    pub fn merged(this: &FriendlySpirit) -> Vec<DeadFriendlySpiritID>;
}

impl CanFrom<Spirit> for FriendlySpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        &value.player_id() == this_player_id.as_ref()
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = EnemySpirit for FriendlySpirit);

// EnemySpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of an [`EnemySpirit`].
    #[wasm_bindgen(extends = SpiritID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type EnemySpiritID;

    /// A spirit that does not belong to you.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = Spirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type EnemySpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &EnemySpirit) -> EnemySpiritID;

    #[wasm_bindgen(method, getter)]
    pub fn merged(this: &EnemySpirit) -> Vec<DeadEnemySpiritID>;
}

impl CanFrom<Spirit> for EnemySpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        !<FriendlySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = FriendlySpirit for EnemySpirit);

// LivingSpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`LivingSpirit`].
    #[wasm_bindgen(extends = SpiritID, extends = LivingDestructibleID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type LivingSpiritID;

    /// A spirit that has positive HP.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = Spirit, extends = LivingDestructible, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type LivingSpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &LivingSpirit) -> LivingSpiritID;
}

impl CanFrom<Spirit> for LivingSpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        value.hp() > 0
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = DeadSpirit for LivingSpirit);

impl CanFrom<LivingDestructible> for LivingSpirit {
    #[inline]
    fn can_from(value: &LivingDestructible) -> bool {
        !<Base as CanFrom<LivingDestructible>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<LivingDestructible>, Error = Base for LivingSpirit);

// DeadSpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`DeadSpirit`].
    #[wasm_bindgen(extends = SpiritID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type DeadSpiritID;

    /// A spirit that has positive HP.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = Spirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type DeadSpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &DeadSpirit) -> DeadSpiritID;
}

impl CanFrom<Spirit> for DeadSpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        !<LivingSpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = LivingSpirit for DeadSpirit);

// OperableSpirit
#[cfg(any(
    doc,
    not(any(feature = "circles", feature = "squares", feature = "triangles"))
))]
#[wasm_bindgen]
extern "C" {
    /// A [`Spirit`](crate::spirit::Spirit) that is "operable", meaning that you can call methods on it.
    /// A spirit is "operable" if and only if it belongs to you, and it has an [`hp`](Destructible::hp) of 1.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = FriendlySpirit, extends = LivingSpirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type OperableSpirit;
}

#[cfg(all(feature = "circles", not(doc)))]
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends = FriendlySpirit, extends = LivingSpirit, typescript_type = "CircleSpirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type OperableSpirit;
}

#[cfg(all(feature = "squares", not(any(doc, feature = "circles"))))]
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends = FriendlySpirit, extends = LivingSpirit, typescript_type = "SquareSpirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type OperableSpirit;
}

#[cfg(all(
    feature = "triangles",
    not(any(doc, feature = "circles", feature = "squares"))
))]
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends = FriendlySpirit, extends = LivingSpirit, typescript_type = "SquareSpirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type OperableSpirit;
}

#[wasm_bindgen]
extern "C" {
    /// The ID of an [`OperableSpirit`].
    #[wasm_bindgen(extends = FriendlySpiritID, extends = LivingSpiritID, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type OperableSpiritID;

    #[wasm_bindgen(method, js_name = "move")]
    fn r#move(this: &OperableSpirit, target: &Array);

    #[wasm_bindgen(method)]
    pub fn energize(this: &OperableSpirit, target: &Entity);

    /// Requires the `"circles"` crate feature
    #[cfg(feature = "circles")]
    #[wasm_bindgen(method)]
    pub fn merge(this: &OperableSpirit, target: &Spirit);

    /// Requires the `"circles"` crate feature
    #[cfg(feature = "circles")]
    #[wasm_bindgen(method)]
    pub fn divide(this: &OperableSpirit);

    /// Requires the `"squares"` crate feature
    #[cfg(feature = "squares")]
    #[wasm_bindgen(method, js_name = "jump")]
    fn _jump(this: &OperableSpirit, target: Array);

    /// Requires the `"triangles"` crate feature
    #[cfg(feature = "triangles")]
    #[wasm_bindgen(method)]
    pub fn explode(this: &OperableSpirit, target: Position);

    #[wasm_bindgen(method)]
    pub fn shout(this: &OperableSpirit, message: &str);

    #[wasm_bindgen(method)]
    pub fn set_mark(this: &OperableSpirit, label: &str);
}

pub type LivingFriendlySpirit = OperableSpirit;
pub type LivingFriendlySpiritID = OperableSpiritID;

impl OperableSpirit {
    /// `move` method
    pub fn move_to_pos(&self, pos: Position) {
        self.r#move(&pos.into());
    }

    #[cfg(feature = "squares")]
    pub fn jump(&self, pos: Position) {
        self._jump(pos.into());
    }
}

impl CanFrom<LivingSpirit> for LivingFriendlySpirit {
    #[inline]
    fn can_from(value: &LivingSpirit) -> bool {
        <FriendlySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<LivingSpirit>, Error = LivingEnemySpirit for LivingFriendlySpirit);

impl CanFrom<FriendlySpirit> for LivingFriendlySpirit {
    #[inline]
    fn can_from(value: &FriendlySpirit) -> bool {
        <LivingSpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<FriendlySpirit>, Error = DeadFriendlySpirit for LivingFriendlySpirit);

impl CanFrom<Spirit> for LivingFriendlySpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        <LivingSpirit as CanFrom<Spirit>>::can_from(value)
            && <FriendlySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = Spirit for LivingFriendlySpirit);

impl AsRef<Spirit> for LivingFriendlySpirit {
    #[inline]
    fn as_ref(&self) -> &Spirit {
        <LivingFriendlySpirit as AsRef<FriendlySpirit>>::as_ref(self).as_ref()
    }
}

// DeadFriendlySpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`DeadFriendlySpirit`].
    #[wasm_bindgen(extends = FriendlySpiritID, extends = DeadSpiritID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type DeadFriendlySpiritID;

    /// A friendly spirit that has zero HP.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = FriendlySpirit, extends = DeadSpirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type DeadFriendlySpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &DeadFriendlySpirit) -> DeadFriendlySpiritID;
}

impl CanFrom<FriendlySpirit> for DeadFriendlySpirit {
    #[inline]
    fn can_from(value: &FriendlySpirit) -> bool {
        <DeadSpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<FriendlySpirit>, Error = LivingFriendlySpirit for DeadFriendlySpirit);

impl CanFrom<DeadSpirit> for DeadFriendlySpirit {
    #[inline]
    fn can_from(value: &DeadSpirit) -> bool {
        <FriendlySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<DeadSpirit>, Error = DeadEnemySpirit for DeadFriendlySpirit);

impl CanFrom<Spirit> for DeadFriendlySpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        <DeadSpirit as CanFrom<Spirit>>::can_from(value)
            && <FriendlySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = Spirit for DeadFriendlySpirit);

impl AsRef<Spirit> for DeadFriendlySpirit {
    #[inline]
    fn as_ref(&self) -> &Spirit {
        <DeadFriendlySpirit as AsRef<FriendlySpirit>>::as_ref(self).as_ref()
    }
}

// LivingEnemySpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`LivingEnemySpirit`].
    #[wasm_bindgen(extends = EnemySpiritID, extends = LivingSpiritID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type LivingEnemySpiritID;

    /// An enemy spirit that has positive HP.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = EnemySpirit, extends = LivingSpirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type LivingEnemySpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &LivingEnemySpirit) -> LivingEnemySpiritID;
}

impl CanFrom<EnemySpirit> for LivingEnemySpirit {
    #[inline]
    fn can_from(value: &EnemySpirit) -> bool {
        <LivingSpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<EnemySpirit>, Error = DeadEnemySpirit for LivingEnemySpirit);

impl CanFrom<LivingSpirit> for LivingEnemySpirit {
    #[inline]
    fn can_from(value: &LivingSpirit) -> bool {
        <EnemySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<LivingSpirit>, Error = LivingFriendlySpirit for LivingEnemySpirit);

impl CanFrom<Spirit> for LivingEnemySpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        <LivingSpirit as CanFrom<Spirit>>::can_from(value)
            && <EnemySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = Spirit for LivingEnemySpirit);

impl AsRef<Spirit> for LivingEnemySpirit {
    #[inline]
    fn as_ref(&self) -> &Spirit {
        <LivingEnemySpirit as AsRef<EnemySpirit>>::as_ref(self).as_ref()
    }
}

// DeadEnemySpirit
#[wasm_bindgen]
extern "C" {
    /// The ID of a [`DeadEnemySpirit`].
    #[wasm_bindgen(extends = EnemySpiritID, extends = DeadSpiritID, typescript_type = "SpiritID")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type DeadEnemySpiritID;

    /// An enemy spirit that has zero HP.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(extends = EnemySpirit, extends = DeadSpirit, typescript_type = "Spirit")]
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub type DeadEnemySpirit;

    #[wasm_bindgen(method, getter)]
    pub fn id(this: &DeadEnemySpirit) -> DeadEnemySpiritID;
}

impl CanFrom<EnemySpirit> for DeadEnemySpirit {
    #[inline]
    fn can_from(value: &EnemySpirit) -> bool {
        <DeadSpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<EnemySpirit>, Error = LivingEnemySpirit for DeadEnemySpirit);

impl CanFrom<DeadSpirit> for DeadEnemySpirit {
    #[inline]
    fn can_from(value: &DeadSpirit) -> bool {
        <EnemySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<DeadSpirit>, Error = DeadFriendlySpirit for DeadEnemySpirit);

impl CanFrom<Spirit> for DeadEnemySpirit {
    #[inline]
    fn can_from(value: &Spirit) -> bool {
        <DeadSpirit as CanFrom<Spirit>>::can_from(value)
            && <EnemySpirit as CanFrom<Spirit>>::can_from(value)
    }
}

try_can_from!(impl TryFrom<Spirit>, Error = Spirit for DeadEnemySpirit);

impl AsRef<Spirit> for DeadEnemySpirit {
    #[inline]
    fn as_ref(&self) -> &Spirit {
        <DeadEnemySpirit as AsRef<EnemySpirit>>::as_ref(self).as_ref()
    }
}

// `spirits`
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends = Object, typescript_type = "(typeof spirits)")]
    #[derive(Clone, Debug)]
    pub type Spirits;

    /// `spirits`. Use the [`GetByID`] trait to retrieve individual spirits.
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen]
    pub static spirits: Spirits;
}

impl TryGetByID<EntityID, Spirit> for Spirits {}
impl TryGetByID<DestructibleID, Spirit> for Spirits {}
impl TryGetByID<LivingDestructibleID, LivingSpirit> for Spirits {}
impl GetByID<SpiritID, Spirit> for Spirits {}
impl EnumerateByID<SpiritID, Spirit> for Spirits {}
impl GetByID<LivingSpiritID, LivingSpirit> for Spirits {}
impl GetByID<DeadSpiritID, DeadSpirit> for Spirits {}
impl GetByID<FriendlySpiritID, FriendlySpirit> for Spirits {}
impl GetByID<EnemySpiritID, EnemySpirit> for Spirits {}
impl GetByID<OperableSpiritID, OperableSpirit> for Spirits {}
impl GetByID<DeadFriendlySpiritID, DeadFriendlySpirit> for Spirits {}
impl GetByID<LivingEnemySpiritID, LivingEnemySpirit> for Spirits {}
impl GetByID<DeadEnemySpiritID, DeadEnemySpirit> for Spirits {}

// `my_spirits`
#[wasm_bindgen]
extern "C" {
    /// `my_spirits`, as a [`Vec`].
    ///
    /// [Yare.io Documentation](https://yare.io/documentation#doc_spirit)
    #[wasm_bindgen(method, getter)]
    pub static my_spirits: Vec<FriendlySpirit>;
}