rust/lib-hedgewars-engine/src/instance.rs
author alfadur
Fri, 22 Mar 2019 19:09:28 +0300
changeset 14703 81030dcbd2d8
parent 14702 29dbe9ce8b7d
child 14704 12db7e435ea6
permissions -rw-r--r--
update module structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14272
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::*,
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
     3
    UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *,
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     4
};
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     5
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
     6
use landgen::outline_template::OutlineTemplate;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
     7
use integral_geometry::{Point, Rect, Size};
14293
b8871dd33ec4 add gfx setup ffi
alfadur
parents: 14272
diff changeset
     8
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
     9
use super::{ipc::IPC, world::World};
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    10
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    11
pub struct EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    12
    pub world: World,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    13
    pub ipc: IPC,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    14
}
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    15
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    16
impl EngineInstance {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    17
    pub fn new() -> Self {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    18
        let mut world = World::new();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    19
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    20
        fn template() -> OutlineTemplate {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    21
            let mut template = OutlineTemplate::new(Size::new(4096*1, 2048*1));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    22
            template.islands = vec![vec![
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    23
                Rect::from_size_coords(100, 2050, 1, 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    24
                Rect::from_size_coords(100, 500, 400, 1200),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    25
                Rect::from_size_coords(3600, 500, 400, 1200),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    26
                Rect::from_size_coords(3900, 2050, 1, 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    27
            ]];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    28
            template.fill_points = vec![Point::new(1, 0)];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    29
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    30
            template
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    31
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    32
        
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    33
        world.init(template());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    34
        
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    35
        Self {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    36
            world,
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    37
            ipc: IPC::new(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    38
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    39
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    40
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    41
    pub fn render(&mut self, x: f32, y: f32, w: f32, h: f32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14294
diff changeset
    42
        self.world.render(x, y, w, h);
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    43
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    44
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    45
    fn process_unordered_message(&mut self, message: &UnorderedEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    46
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    47
            Pong => println!("Pong!"),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    48
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    49
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    50
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    51
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    52
    fn process_config_message(&mut self, message: &ConfigEngineMessage) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    53
        match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    54
            SetSeed(seed) => self.world.set_seed(seed.as_bytes()),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    55
            _ => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    56
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    57
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    58
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    59
    pub fn process_ipc_queue(&mut self) {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    60
        let messages: Vec<EngineMessage> = self.ipc.iter().collect();
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    61
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    62
        for message in messages {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    63
            println!("Processing message: {:?}", message);
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    64
            match message {
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    65
                Unknown => println!("Unknown message"),
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    66
                Empty => println!("Empty message"),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    67
                Synced(_, _) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    68
                Unsynced(_) => unimplemented!(),
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    69
                Unordered(unordered_message) => self.process_unordered_message(&unordered_message),
14294
21be7838a127 Add advance_simulation() function to engine lib, some WIP on frontend
unc0rr
parents: 14293
diff changeset
    70
                Config(config_message) => self.process_config_message(&config_message),
14272
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    71
            }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    72
        }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    73
    }
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents:
diff changeset
    74
}