Exemplo n.º 1
0
        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // todo: helper class to set relative paths
            var dialog = new OpenFileDialog
            {
                Filter           = @"Image Files|*.map",
                Title            = @"Select a File",
                InitialDirectory = @"C:\Source\MapEditor\MapEditor\Map\Saves"
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                MapFile data = null;

                var archive = ZipFile.OpenRead(dialog.FileName);
                var mapdata = archive.GetEntry("map.json");
                if (mapdata != null)
                {
                    using (var stream = mapdata.Open())
                        using (var reader = new StreamReader(stream))
                        {
                            var json = reader.ReadToEnd();
                            data = JsonConvert.DeserializeObject <MapFile>(json);
                        }

                    data.Terrains = data.Terrains.Select(x =>
                    {
                        // todo: customer serializer/deserializer
                        Bitmap tileImage = null;
                        var imageData    = archive.GetEntry($"{x.Key}.png");
                        if (imageData != null)
                        {
                            using (var stream = imageData.Open())
                                using (var image = Image.FromStream(stream))
                                {
                                    tileImage = new Bitmap(image);
                                }
                        }
                        return(new Terrain(x.Value.TerrainType, tileImage, x.Value.Width, x.Value.Height));
                    }).ToDictionary(k => k.Key);
                }

                if (data != null)
                {
                    _mapHandler = new Editor.MapEditor(_messageHub, _session, data.Width, data.Height);
                }
            }
        }
Exemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();

            _messageHub = new MessageHub();
            _session    = new Session(_messageHub);
            _session.Init();

            var graphics = new WinFormGraphics(canvas);

            _scene = new Scene(_session, graphics);

            _mapHandler = new Editor.MapEditor(_messageHub, _session, canvas.Width / 20, canvas.Height / 20);
            // todo: replace with an OnChecked handler
            _mapHandler.ShowGrid(gridChk.Checked);
            _mapHandler.Init();

            _unitHandler = new UnitHandler(_messageHub, _session);
            _unitHandler.Init();

            // todo: is cameraHandler the responsibility of Map class?
            _cameraHandler = new CameraHandler(_messageHub, new Point(0, 0), canvas.Width, canvas.Height);
            _cameraHandler.Init();

            _input            = new Editor.EditorInput(_messageHub, _cameraHandler);
            _mouseState       = new MouseState();
            canvas.MouseMove += (sender, eventArgs) =>
            {
                _mouseState.GetState(eventArgs.Location, eventArgs.Button);
                _input.OnMouseEvent(_mouseState);
            };
            KeyPreview = true;
            KeyPress  += (sender, eventArgs) =>
            {
                _input.OnKeyboardEvent(eventArgs);
            };

            LoadTerrains();
            LoadUnits();

            var timer = new Timer();

            timer.Tick    += Update;
            timer.Interval = 100; // in miliseconds
            timer.Start();
        }