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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Objects to be drawn to the screen.

#[cfg(feature = "client")]
mod appearance;
#[cfg(feature = "client")]
mod color;
#[cfg(feature = "client")]
pub use appearance::*;
#[cfg(feature = "client")]
pub use color::Color;

#[cfg(feature = "labels")]
pub mod labels;

#[cfg(feature = "physics")]
pub mod physics;
pub mod scenes;

use crate::prelude::*;

use anyhow::{Error, Result};
use scenes::Layer;

use derive_builder::Builder;
use glam::f32::{vec2, Vec2};
use parking_lot::Mutex;

use std::{
    collections::HashMap,
    sync::{Arc, Weak},
};

#[cfg(feature = "physics")]
type RigidBodyParent = Option<Option<Weak<Mutex<Node<Object>>>>>;
type ObjectsMap = HashMap<usize, NObject>;
pub(crate) type NObject = Arc<Mutex<Node<Object>>>;
type WeakObject = Weak<Mutex<Node<Object>>>;

/// Holds position size and rotation of an object.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform {
    pub position: Vec2,
    pub size: Vec2,
    pub rotation: f32,
}
impl Eq for Transform {}
impl Transform {
    /// Combines two Transforms with each other. It adds position, multiplies size and adds rotation.
    pub fn combine(self, rhs: Self) -> Self {
        Self {
            position: self.position + rhs.position.rotate(Vec2::from_angle(self.rotation)),
            size: self.size * rhs.size,
            rotation: self.rotation + rhs.rotation,
        }
    }

    /// Sets the position of this transform and returns itself.
    pub fn position(mut self, position: Vec2) -> Self {
        self.position = position;
        self
    }

    /// Sets the size of this transform and returns itself.
    pub fn size(mut self, size: Vec2) -> Self {
        self.size = size;
        self
    }

    /// Sets the rotation of this transform and returns itself.
    pub fn rotation(mut self, rotation: f32) -> Self {
        self.rotation = rotation;
        self
    }
}

impl Default for Transform {
    fn default() -> Self {
        Self {
            position: vec2(0.0, 0.0),
            size: vec2(1.0, 1.0),
            rotation: 0.0,
        }
    }
}

#[derive(Clone)]
#[cfg(feature = "client")]
pub(crate) struct VisualObject {
    pub transform: Transform,
    pub appearance: Appearance,
}
#[cfg(feature = "client")]
impl VisualObject {
    /// Combines the object position data.
    pub fn combined(object: &Object, other: &Object) -> Self {
        let transform = object.transform.combine(other.transform);
        let appearance = other.appearance().clone();
        Self {
            transform,
            appearance,
        }
    }
}

/// Node structure for the layer.
pub(crate) struct Node<T> {
    pub object: T,
    pub parent: Option<Weak<Mutex<Node<T>>>>,
    #[cfg(feature = "physics")]
    pub rigid_body_parent: RigidBodyParent,
    pub children: Vec<Arc<Mutex<Node<T>>>>,
}
impl PartialEq for Node<Object> {
    fn eq(&self, other: &Self) -> bool {
        self.object == other.object
    }
}

impl Node<Object> {
    /// Takes a vector of every object transform and appearance and fills it with the right client order based on the root node inserted.
    #[cfg(feature = "client")]
    pub(crate) fn order_position(order: &mut Vec<VisualObject>, objects: &Self) {
        for child in objects.children.iter() {
            let child = child.lock();
            let object = VisualObject::combined(&objects.object, &child.object);
            order.push(object.clone());
            for child in child.children.iter() {
                let child = child.lock();
                order.push(VisualObject {
                    transform: object.transform.combine(child.object.transform),
                    appearance: child.object.appearance().clone(),
                });
                Self::order_position(order, &child);
            }
        }
    }

    /// Iterates to the last child to update all public position held by the Node.
    pub fn update_children_position(&mut self, parent_pos: Transform) {
        self.object.set_parent_transform(parent_pos);
        for child in self.children.iter() {
            child
                .lock()
                .update_children_position(self.object.public_transform());
        }
    }

    /// Searches for the given object to be removed from the list of children.
    ///
    /// In case there is no child it will return an error.
    pub fn remove_child(&mut self, object: &NObject) -> Result<()> {
        let index = self
            .children
            .clone()
            .into_iter()
            .position(|x| Arc::ptr_eq(&x, object))
            .ok_or(Error::msg("No child found"))?;
        self.children.remove(index);
        Ok(())
    }

    /// Removes all children and their children from the layer.
    pub fn remove_children(
        &mut self,
        objects: &mut ObjectsMap,
        #[cfg(feature = "physics")] rigid_bodies: &mut ObjectsMap,
    ) {
        #[cfg(feature = "physics")]
        {
            let layer = self.object.layer().clone();
            self.object.physics.remove(layer.physics());
        }
        for child in self.children.iter() {
            child.clone().lock().remove_children(
                objects,
                #[cfg(feature = "physics")]
                rigid_bodies,
            );
        }
        objects.remove(&self.object.id());
        #[cfg(feature = "physics")]
        rigid_bodies.remove(&self.object.id());
        self.children = vec![];
    }
}

/// Object to be initialized to the layer.
#[derive(Default, Clone, Builder, PartialEq, Debug)]
pub struct NewObject {
    #[builder(setter(into))]
    pub transform: Transform,
    #[builder(setter(into))]
    #[cfg(feature = "client")]
    pub appearance: Appearance,
    #[builder(setter(skip))]
    #[cfg(feature = "physics")]
    pub(crate) physics: ObjectPhysics,
}

/// An initialized object that gets rendered on the screen.
#[derive(Clone)]
pub struct Object {
    pub transform: Transform,
    parent_transform: Transform,
    #[cfg(feature = "client")]
    pub appearance: Appearance,
    id: usize,
    node: Option<WeakObject>,
    #[cfg(feature = "physics")]
    pub(crate) physics: ObjectPhysics,
    layer: Option<Arc<Layer>>,
}
impl std::fmt::Debug for Object {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Object")
            .field("id", &self.id)
            .field("transform", &self.transform)
            .field("parent_transform", &self.parent_transform)
            .finish()
    }
}

impl Eq for NewObject {}
impl PartialEq for Object {
    fn eq(&self, other: &Self) -> bool {
        #[cfg(not(feature = "client"))]
        {
            self.transform == other.transform
                && self.parent_transform == other.parent_transform
                && self.id == other.id
                && self.layer == other.layer
        }
        #[cfg(feature = "client")]
        {
            self.transform == other.transform
                && self.parent_transform == other.parent_transform
                && self.appearance == other.appearance
                && self.id == other.id
                && self.layer == other.layer
        }
    }
}
impl Eq for Object {}

/// New
impl NewObject {
    /// Returns a default object
    pub fn new() -> Self {
        Self::default()
    }

    /// Initializes the object into a layer.
    pub fn init(self, layer: &Arc<Layer>) -> Result<Object> {
        self.init_with_optional_parent(layer, None)
    }

    /// Initializes the object into a layer with a parent object.
    pub fn init_with_parent(self, layer: &Arc<Layer>, parent: &Object) -> Result<Object> {
        self.init_with_optional_parent(layer, Some(parent))
    }

    /// Initializes the object into a layer with an optional parent object.
    #[allow(unused_mut)]
    pub fn init_with_optional_parent(
        mut self,
        layer: &Arc<Layer>,
        parent: Option<&Object>,
    ) -> Result<Object> {
        // Init ID of this object.
        let id = layer.increment_id();

        #[cfg(feature = "physics")]
        let mut rigid_body_parent;
        let parent: NObject = if let Some(parent) = parent {
            let parent = parent.as_node();
            #[cfg(feature = "physics")]
            {
                rigid_body_parent = parent.lock().rigid_body_parent.clone();
            }
            parent.clone()
        } else {
            #[cfg(feature = "physics")]
            {
                rigid_body_parent = None;
            }
            layer.root.clone()
        };
        // Updates the physics side and returns the parent position.
        #[cfg(feature = "physics")]
        let parent_transform = self
            .physics
            .update(
                &self.transform,
                &parent,
                &mut rigid_body_parent,
                id as u128,
                layer.physics(),
            )
            .ok_or(Error::msg(
                "Could not update the physics side of this object.",
            ))?;
        #[cfg(not(feature = "physics"))]
        let parent_transform = parent.lock().object.public_transform();

        let mut initialized = Object {
            transform: self.transform,
            parent_transform,
            #[cfg(feature = "client")]
            appearance: self.appearance,
            id,
            node: None,
            #[cfg(feature = "physics")]
            physics: self.physics,
            layer: Some(layer.clone()),
        };

        // Make yourself to a node.
        let node: NObject = std::sync::Arc::new(Mutex::new(Node {
            object: initialized.clone(),
            parent: Some(std::sync::Arc::downgrade(&parent)),
            #[cfg(feature = "physics")]
            rigid_body_parent: rigid_body_parent.clone(),
            children: vec![],
        }));

        // set reference to own node to manipulate.
        let reference = Some(std::sync::Arc::downgrade(&node));
        node.lock().object.node = reference.clone();
        initialized.node = reference;

        // In case there is no rigid body roots make yourself one.
        #[cfg(feature = "physics")]
        if let Some(value) = &rigid_body_parent {
            if value.is_none() && initialized.physics.rigid_body.is_some() {
                layer.rigid_body_roots().lock().insert(id, node.clone());
            }
        }

        // Add yourself to the objects map.
        layer.add_object(id, &node);

        // Add yourself to the list of children of the parent.
        parent.lock().children.push(node.clone());
        Ok(initialized)
    }
}

/// Setters
impl NewObject {
    /// Sets the position and rotation of an object.
    pub fn set_isometry(&mut self, position: Vec2, rotation: f32) {
        self.transform.position = position;
        self.transform.rotation = rotation;
    }
    /// Returns a reference to the appearance of the object.
    #[cfg(feature = "client")]
    pub fn appearance(&self) -> &Appearance {
        &self.appearance
    }
}

/// Physics
#[cfg(feature = "physics")]
impl NewObject {
    /// Returns the collider of the object in case it has one.
    #[cfg(feature = "physics")]
    pub fn collider(&self) -> Option<&Collider> {
        self.physics.collider.as_ref()
    }
    /// Sets the collider of the object.
    #[cfg(feature = "physics")]
    pub fn set_collider(&mut self, collider: Option<Collider>) {
        self.physics.collider = collider;
    }
    /// Returns a mutable reference to the collider.
    #[cfg(feature = "physics")]
    pub fn collider_mut(&mut self) -> Option<&mut Collider> {
        self.physics.collider.as_mut()
    }
    /// Returns the rigid bodyh of the object in case it has one.
    #[cfg(feature = "physics")]
    pub fn rigid_body(&self) -> Option<&RigidBody> {
        self.physics.rigid_body.as_ref()
    }
    /// Sets the rigid body of the object.
    #[cfg(feature = "physics")]
    pub fn set_rigid_body(&mut self, rigid_body: Option<RigidBody>) {
        self.physics.rigid_body = rigid_body;
    }
    /// Returns a mutable reference to the rigid body.
    #[cfg(feature = "physics")]
    pub fn rigid_body_mut(&mut self) -> Option<&mut RigidBody> {
        self.physics.rigid_body.as_mut()
    }
    /// Returns the local position of the collider.
    #[cfg(feature = "physics")]
    pub fn local_collider_position(&self) -> Vec2 {
        self.physics.local_collider_position
    }
    /// Sets the local position of the collider of this object in case it has one.
    #[cfg(feature = "physics")]
    pub fn set_local_collider_position(&mut self, pos: Vec2) {
        self.physics.local_collider_position = pos;
    }
}

impl Object {
    pub(crate) fn root() -> Self {
        Self {
            transform: Transform::default(),
            parent_transform: Transform::default(),
            #[cfg(feature = "client")]
            appearance: Appearance::default(),
            id: 0,
            node: None,
            #[cfg(feature = "physics")]
            physics: ObjectPhysics::default(),
            layer: None,
        }
    }

    pub fn layer(&self) -> &Arc<Layer> {
        self.layer.as_ref().unwrap()
    }

    /// Removes the object from it's layer.
    #[allow(unused_mut)]
    pub fn remove(mut self) -> NewObject {
        let layer = self.layer.unwrap();
        let mut map = layer.objects_map.lock();
        #[cfg(feature = "physics")]
        let mut rigid_bodies = layer.rigid_body_roots().lock();
        let node = map.remove(&self.id).unwrap();

        #[cfg(feature = "physics")]
        {
            rigid_bodies.remove(&self.id);
            // Remove self from the physics side.
            self.physics.remove(layer.physics());
        }

        let mut object = node.lock();
        object.remove_children(
            &mut map,
            #[cfg(feature = "physics")]
            &mut rigid_bodies,
        );

        let parent = object.parent.clone().unwrap().upgrade().unwrap();
        let mut parent = parent.lock();
        parent.remove_child(&node).unwrap();

        NewObject {
            transform: self.transform,
            #[cfg(feature = "client")]
            appearance: self.appearance,
            #[cfg(feature = "physics")]
            physics: self.physics,
        }
    }

    /// Makes a new object from this object.
    pub fn to_new(&self) -> NewObject {
        NewObject {
            transform: self.transform,
            #[cfg(feature = "client")]
            appearance: self.appearance.clone(),
            #[cfg(feature = "physics")]
            physics: self.physics.clone(),
        }
    }

    /// Copies the data from a `NewObject` into itself.
    pub fn copy_new(&mut self, object: NewObject) {
        self.transform = object.transform;
        #[cfg(feature = "physics")]
        {
            self.physics = object.physics;
        }
        #[cfg(feature = "client")]
        {
            self.appearance = object.appearance;
        }
    }

    /// Sets the position and rotation of an object.
    pub fn set_isometry(&mut self, position: Vec2, rotation: f32) {
        self.transform.position = position;
        self.transform.rotation = rotation;
    }

    /// Returns the public position where the object is going to be rendered.
    pub fn public_transform(&self) -> Transform {
        self.transform.combine(self.parent_transform)
    }

    pub(crate) fn set_parent_transform(&mut self, transform: Transform) {
        self.parent_transform = transform;
    }

    /// Returns a reference to the appearance of the object.
    #[cfg(feature = "client")]
    pub fn appearance(&self) -> &Appearance {
        &self.appearance
    }

    /// Returns the identification number of the object specific the layer it is inside right now.
    ///
    /// Returns 0 in case it is not initialized to a layer yet.
    pub fn id(&self) -> usize {
        self.id
    }

    pub(crate) fn as_node(&self) -> NObject {
        self.node.as_ref().unwrap().upgrade().unwrap()
    }

    /// Updates the object to match the object information located inside the system of the layer. Useful when having physics.
    pub fn update(&mut self) {
        // receive
        let node = self.as_node();
        let object = &node.lock().object;
        self.transform = object.transform;
        #[cfg(feature = "client")]
        {
            self.appearance = object.appearance().clone();
        }
    }

    /// Updates the object inside the layer system to match with this one. Useful when doing anything to the object and submitting it with this function.
    pub fn sync(&mut self) {
        // send
        // update public position of all children recursively
        let node = self.as_node().clone();
        #[cfg(feature = "physics")]
        {
            let mut node = node.lock();
            let layer = self.layer().clone();
            self.parent_transform = self
                .physics
                .update(
                    &self.transform,
                    &node.parent.clone().unwrap().upgrade().unwrap(),
                    &mut node.rigid_body_parent,
                    self.id as u128,
                    layer.physics(),
                )
                .unwrap();
        }
        node.lock()
            .update_children_position(self.public_transform());
        let arc = self.as_node();
        let mut object = arc.lock();
        object.object = self.clone();
    }
}

/// Physics
#[cfg(feature = "physics")]
impl Object {
    pub(crate) fn rigidbody_handle(&self) -> Option<rapier2d::dynamics::RigidBodyHandle> {
        self.physics.rigid_body_handle
    }

    /// Returns the collider of the object in case it has one.
    pub fn collider(&self) -> Option<&Collider> {
        self.physics.collider.as_ref()
    }

    /// Sets the collider of the object.
    pub fn set_collider(&mut self, collider: Option<Collider>) {
        self.physics.collider = collider;
    }

    /// Returns a mutable reference to the collider.
    pub fn collider_mut(&mut self) -> Option<&mut Collider> {
        self.physics.collider.as_mut()
    }

    /// Returns the rigid bodyh of the object in case it has one.
    pub fn rigid_body(&self) -> Option<&RigidBody> {
        self.physics.rigid_body.as_ref()
    }

    /// Sets the rigid body of the object.
    pub fn set_rigid_body(&mut self, rigid_body: Option<RigidBody>) {
        self.physics.rigid_body = rigid_body;
    }

    /// Returns a mutable reference to the rigid body.
    pub fn rigid_body_mut(&mut self) -> Option<&mut RigidBody> {
        self.physics.rigid_body.as_mut()
    }

    /// Returns the local position of the collider.
    pub fn local_collider_position(&self) -> Vec2 {
        self.physics.local_collider_position
    }

    /// Sets the local position of the collider of this object in case it has one.
    pub fn set_local_collider_position(&mut self, pos: Vec2) {
        self.physics.local_collider_position = pos;
    }
}