rust/lib-hedgewars-engine/src/instance.rs
author Wuzzy <Wuzzy2@mail.ru>
Fri, 11 Oct 2019 18:59:48 +0200
changeset 15493 b56c04fac6f9
parent 15305 ae8e14d14596
child 15831 c3971b38bbfa
permissions -rw-r--r--
Fix closing bracket
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     1
use hedgewars_engine_messages::messages::{
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     2
    ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*,
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
     3
    UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *,
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     4
};
15305
ae8e14d14596 Move messages queue to hedgewars-engine-messages lib
unc0rr
parents: 15286
diff changeset
     5
use hedgewars_engine_messages::queue::*;
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     6
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
     7
use integral_geometry::{Point, Rect, Size};
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
     8
use landgen::outline_template::OutlineTemplate;
14314
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14293
diff changeset
     9
15286
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    10
use super::{ipc::*, world::World};
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    11
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    12
pub struct EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    13
    pub world: World,
15286
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    14
    pub ipc_channel: Channel,
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    15
    ipc_queue: MessagesQueue,
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    16
}
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    17
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    18
impl EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    19
    pub fn new() -> Self {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    20
        let mut world = World::new();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    21
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    22
        fn template() -> OutlineTemplate {
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    23
            let mut template = OutlineTemplate::new(Size::new(4096 * 1, 2048 * 1));
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    24
            template.islands = vec![vec![
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    25
                Rect::from_size_coords(100, 2050, 1, 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    26
                Rect::from_size_coords(100, 500, 400, 1200),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    27
                Rect::from_size_coords(3600, 500, 400, 1200),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    28
                Rect::from_size_coords(3900, 2050, 1, 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    29
            ]];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    30
            template.fill_points = vec![Point::new(1, 0)];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    31
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    32
            template
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    33
        }
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    34
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14315
diff changeset
    35
        world.init(template());
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14725
diff changeset
    36
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    37
        Self {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    38
            world,
15286
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    39
            ipc_channel: Channel::new(),
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    40
            ipc_queue: MessagesQueue::new(QueueChatStrategy::LocalGame),
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    41
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    42
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    43
14725
12db7e435ea6 move camera into engine
alfadur
parents: 14723
diff changeset
    44
    pub fn render(&mut self) {
12db7e435ea6 move camera into engine
alfadur
parents: 14723
diff changeset
    45
        self.world.render();
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    46
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    47
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    48
    fn process_unordered_message(&mut self, message: &UnorderedEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    49
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    50
            Pong => println!("Pong!"),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    51
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    52
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    53
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    54
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    55
    fn process_config_message(&mut self, message: &ConfigEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    56
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    57
            SetSeed(seed) => self.world.set_seed(seed.as_bytes()),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    58
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    59
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    60
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    61
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    62
    pub fn process_ipc_queue(&mut self) {
15286
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    63
        for message in self.ipc_channel.iter() {
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    64
            self.ipc_queue.push(message);
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    65
        }
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    66
07e909ba4203 Implement ipc queue which takes care of message ordering and timestamps
unC0Rr
parents: 14726
diff changeset
    67
        let messages: Vec<EngineMessage> = self.ipc_queue.iter(0).collect();
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    68
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    69
        for message in messages {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    70
            println!("Processing message: {:?}", message);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    71
            match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    72
                Unknown => println!("Unknown message"),
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    73
                Empty => println!("Empty message"),
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    74
                Synced(_, _) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    75
                Unsynced(_) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    76
                Unordered(unordered_message) => self.process_unordered_message(&unordered_message),
14315
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14314
diff changeset
    77
                Config(config_message) => self.process_config_message(&config_message),
14293
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    78
            }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    79
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    80
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    81
}