コード例 #1
0
ファイル: UniverseTest.cs プロジェクト: hieplenet/GameOfLife
 public void UniverseAddDuplicatedLivingCells()
 {
     Universe universe = new Universe();
     universe.Add(new Cell(1, 1));
     universe.Add(new Cell(1, 1));
     List<Cell> cells = universe.GetAllLivingCells();
     Assert.IsTrue(cells.Count == 1);
 }
コード例 #2
0
        public EntityServiceImpl()
        {
            universe = new Universe();
            universe.Add(new BehaviorProcessor());

            runner          = new Runner();
            runner.Universe = universe;

            running           = runner.Run();
            universe.Stepped += (s, e) =>
            {
                var se = new ServerEvent();

                foreach (var entity in universe.Entities)
                {
                    var pc = entity.GetComponent <PhysicsComponent>();
                    var ec = entity.GetComponent <EntityComponent>();
                    se.Entities.Add(new Proto.Entity
                    {
                        Id       = entity.Identifier.ToString(),
                        Position = ProtoConverter.Convert(pc.Body.LinearPosition),
                        Velocity = ProtoConverter.Convert(pc.Body.LinearVelocity),
                        Rotation = ProtoConverter.Convert(pc.Body.AngularPosition),
                        Class    = ec?.Class ?? ""
                    });
                }

                // Console.WriteLine("Emitting " + se.Entities.Count);


                foreach (var writer in writers)
                {
                    writer.Value.WriteAsync(se);
                }
            };

            for (int i = 0; i < 10; i++)
            {
                var bloid = new SimulationEntity();
                bloid.Components.Add(new TransformComponent
                {
                    Scaling = new Vector3(0.2f, 0.2f, 0.2f)
                });
                bloid.Components.Add(new BehaviorComponent());
                bloid.Components.Add(new PhysicsComponent(new Body
                {
                    LinearPosition  = Functions.Random.NextVector3(-10, 10),
                    LinearVelocity  = Functions.Random.NextVector3(-10, 10),
                    AngularPosition = Functions.Random.NextQuaternion()
                }));
                bloid.Components.Add(new EntityComponent {
                    Class = "ai"
                });

                universe.Add(bloid);
            }
        }
コード例 #3
0
ファイル: UniverseTest.cs プロジェクト: hieplenet/GameOfLife
 public void TestGetManyNeiborghs()
 {
     Universe universe = new Universe();
     universe.Add(new Cell(1, 1));
     universe.Add(new Cell(2, 1));
     universe.Add(new Cell(1, 2));
     IEnumerable<Cell> expected = universe.GetNeighbors(new Cell(1, 1));
     Assert.IsTrue(expected.Count() == 2);
 }
コード例 #4
0
ファイル: UniverseTest.cs プロジェクト: hieplenet/GameOfLife
 public void TestDyingCellBecomeALive()
 {
     Universe universe = new Universe();
     universe.Add(new Cell(1, 1));
     universe.Add(new Cell(2, 1));
     universe.Add(new Cell(1, 2));
     universe.Tick();
     Cell expected = universe.GetCell(new Cell(2, 2));
     Assert.IsTrue(expected != null);
 }
コード例 #5
0
ファイル: RocketGame.cs プロジェクト: pokornym/rocket
        protected override void OnInitialize()
        {
            SphereModel sphere = new SphereModel(_coder, SPHERE_BREAKUPS);

            GlProgram wProgram = new GlProgram(
                new Shader(ShaderTypes.Vertex, File.ReadAllText(Path.Combine(SHADERS_DIR, WORLD_SHADERS, "vertex.glsl"))),
                new Shader(ShaderTypes.Fragment, File.ReadAllText(Path.Combine(SHADERS_DIR, WORLD_SHADERS, "fragment.glsl")))
                );

            GlProgram gProgram = new GlProgram(
                new Shader(ShaderTypes.Vertex, File.ReadAllText(Path.Combine(SHADERS_DIR, GUI_SHADERS, "vertex.glsl"))),
                new Shader(ShaderTypes.Fragment, File.ReadAllText(Path.Combine(SHADERS_DIR, GUI_SHADERS, "fragment.glsl")))
                );

            _universe.Add(new SpaceObject(2, 100000, sphere)
            {
                Position = new Vector3(0, 0, 0)
            });
            _universe.Add(new SpaceObject(8, 50, sphere)
            {
                Position = new Vector3(-500, -500, 0), Velocity = new Vector3(30f, 0, -5f)
            });
            _universe.Add(new SpaceObject(0, 250, sphere)
            {
                Position = new Vector3(500, -500, 0), Velocity = new Vector3(30f, 0, 5f)
            });
            _universe.Add(_rocket = new RocketObject(55, new RocketModel(_coder))
            {
                Position = new Vector3(500, 500, 500), Velocity = new Vector3(-30f, 0f, 0f)
            });

            RenderHandle handle = new RenderHandle(this, wProgram);

            _layer = new SceneLayer(_universe, handle);
            AddLayer(_layer);
            _cam = new OrbitalCamera(_layer.Camera)
            {
                Distance = 250
            };
            Font fnt = new Font(Path.Combine(ASSETS_DIR, FONT_DIR, FONT_FILE));

            AddLayer(new GuiFuelLayer(_rocket, this, gProgram, new GuiVertexCoder(), fnt));
            GuiLayer tutorial = new GuiLayer(this, gProgram, new GuiVertexCoder(), fnt);

            tutorial.Add(_tutorialLbl);
            AddLayer(tutorial);
            Background = new Color(50, 50, 50, 255);
            base.OnInitialize();
        }
コード例 #6
0
        public override async Task Stream(
            IAsyncStreamReader <ClientEvent> requestStream,
            IServerStreamWriter <ServerEvent> responseStream,
            ServerCallContext context)
        {
            var guid = Guid.NewGuid();

            writers.TryAdd(guid, responseStream);
            SimulationEntity entity = null;

            while (await requestStream.MoveNext())
            {
                var current = requestStream.Current;

                var id = Guid.Parse(current.Entity.Id);
                if (!universe.TryGetEntity(id, out entity))
                {
                    entity = new SimulationEntity(id);
                    entity.Components.Add(new EntityComponent());
                    entity.Components.Add(new PhysicsComponent(new Body()));
                    universe.Add(entity);
                }

                var pc = entity.GetComponent <PhysicsComponent>();
                pc.Body.LinearPosition  = ProtoConverter.Convert(current.Entity.Position);
                pc.Body.LinearVelocity  = ProtoConverter.Convert(current.Entity.Velocity);
                pc.Body.AngularPosition = ProtoConverter.Convert(current.Entity.Rotation);
            }

            if (entity != null)
            {
                universe.Remove(entity);
            }
            writers.TryRemove(guid, out var unused);
        }
コード例 #7
0
        private void MainRectangle_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            double  width = MainCanvas.ActualWidth;
            double  height = MainCanvas.ActualHeight;
            double  mouseX, mouseY, bodyX, bodyY;
            Ellipse body;

            if (_currentItem == SelectedItem.Body)
            {
                mouseX = e.GetPosition(sender as IInputElement).X;
                mouseY = e.GetPosition(sender as IInputElement).Y;
                bodyX  = (mouseX - width / 2) / width * (width / Math.Max(width, height) * _universe.CameraFOV);
                bodyY  = (mouseY - height / 2) / height * (height / Math.Max(width, height) * _universe.CameraFOV);

                _universe.Add(new MaterialPoint(new Vector2(bodyX, bodyY), 1e30, new Vector2(0, 0)));

                body = new Ellipse()
                {
                    Width = 10, Height = 10, Fill = Brushes.DarkGray, Stroke = Brushes.Black
                };
                body.PreviewMouseDown += BodyEllipse_MouseDown;
                _bodies.Add(body);
                Canvas.SetLeft(body, mouseX - 5);
                Canvas.SetTop(body, mouseY - 5);
                MainCanvas.Children.Add(body);
            }
        }
コード例 #8
0
        private void ShowSelectedItemInfo(object sender, RoutedEventArgs e)
        {
            if (ListView.SelectedItem == null)
            {
                return;
            }

            SpaceObject oldObj = ListView.SelectedItem as SpaceObject;
            SpaceObject newObj = oldObj.GetCopy();

            OpenInfoWindow(newObj, delegate() {
                _universe.Remove(oldObj);
                _universe.Add(newObj);
                sky.ItemsSource = _universe;
            });
        }
コード例 #9
0
        public void Universe_Contains_Succeeds(int x, int y, bool result)
        {
            var universe = new Universe();

            universe.Add(42, 1337);

            universe.Contains(x, y).Should().Be(result);
        }
コード例 #10
0
ファイル: UniverseTest.cs プロジェクト: hieplenet/GameOfLife
 public void GetCellInUniverse()
 {
     Universe universe = new Universe();
     Cell cell = new Cell(1, 1);
     universe.Add(cell);
     Cell expected = universe.GetCell(cell);
     Assert.IsTrue(expected.Equals(cell));
 }
コード例 #11
0
ファイル: UniverseTest.cs プロジェクト: hieplenet/GameOfLife
 public void TestGetEmptyNeighbor()
 {
     Universe universe = new Universe();
     Cell cell = new Cell(1, 1);
     universe.Add(cell);
     IEnumerable<Cell> expected = universe.GetNeighbors(cell);
     Assert.IsTrue(!expected.Any());
 }
コード例 #12
0
        public void Universe_IsCellAlive_Succeeds(int x, int y, bool result)
        {
            var universe = new Universe();

            universe.Add(42, 1337);

            universe.IsCellAlive(x, y).Should().Be(result);
        }
コード例 #13
0
ファイル: UniverseTest.cs プロジェクト: hieplenet/GameOfLife
 public void TestLivingCellDie()
 {
     Universe universe = new Universe();
     Cell cell = new Cell(1, 1);
     universe.Add(cell);
     universe.Tick();
     Cell expected = universe.GetCell(cell);
     Assert.IsTrue(expected == null);
 }
コード例 #14
0
        public void Universe_IsEmpty_Succeeds()
        {
            var universe = new Universe();

            universe.IsEmpty.Should().BeTrue();

            universe.Add(42, 1337);
            universe.IsEmpty.Should().BeFalse();
        }
コード例 #15
0
        public void it_should_work()
        {
            var universe = new Universe();

            universe.Add(new Vector(3, 3, 6));

            var vectors = universe.Find(new Vector(3, 3, 6));

            Assert.AreEqual(1, vectors.Count);
        }
コード例 #16
0
        public void Add_GivenCoordinates_AddsCellAndReturnsTrue()
        {
            // act
            bool addWorked = universe.Add(12, 23);

            // assert
            Assert.IsTrue(
                addWorked,
                message: "The add operation failed.");

            Assert.AreEqual(
                expected: 1,
                actual: universe.Population,
                message: "The universe population did not increase to exactly 1.");

            Assert.IsTrue(
                universe.HasCell(12, 23),
                message: "No cell found at coordinates.");
        }
コード例 #17
0
        public void Test()
        {
            var universe    = new Universe();
            var componentId = Universe.COMPONENT_ID;

            Assert.Equal(typeof(Component), universe.GetStruct <Component>(componentId)?.Type);
            Assert.Equal(nameof(Component), universe.GetStruct <Identifier>(componentId));

            Assert.Equal(2, universe.Entities[componentId].Archetype.Count);

            var testId = new EcsId(0x100);

            universe.Add(componentId, testId);
            Assert.Equal(new [] { componentId, Universe.IDENTIFIER_ID, testId },
                         universe.Entities[componentId].Type);

            Assert.Equal(1, universe.Entities[componentId].Archetype.Count);
        }
コード例 #18
0
        public Universe Create(int w, int h)
        {
            var universe = new Universe();

            universe.Width  = w;
            universe.Height = h;
            for (var i = 0; i < w; i++)
            {
                for (var j = 0; j < h; j++)
                {
                    universe.Add(new Cell()
                    {
                        X = i, Y = j, IsAlive = false, IsAliveTomorrow = false
                    });
                }
            }
            return(universe);
        }
コード例 #19
0
        private void InitializeGui()
        {
            _universe = new Universe(Content)
            {
                AutoHandleInput = true
            };
            Components.Add(new UniverseComponent(this, _universe));

            var exitButton = new Button
            {
                BackgroundColor = Color.Black,
                Content         = new Label {
                    Font      = _font,
                    Text      = "Exit",
                    TextColor = Color.White
                },
                Location = PointF.Empty
            };

            exitButton.Content.SizeToFit();
            exitButton.SizeToFit();
            exitButton.Tapped += (sender, e) => {
                Exit();
            };

            _labelEndShowKeyboardInput = new Label
            {
                Frame     = new RectangleF(20, 60, 320, 20),
                Font      = _font,
                TextColor = Color.White
            };

            _labelShowKeyboardInputCallback = new Label
            {
                Frame = new RectangleF(
                    _labelEndShowKeyboardInput.Frame.Left,
                    _labelEndShowKeyboardInput.Frame.Bottom + 10,
                    _labelEndShowKeyboardInput.Frame.Width,
                    _labelEndShowKeyboardInput.Frame.Height),
                Font      = _font,
                TextColor = Color.White
            };

            var buttonShowKeyboardInput = new Button
            {
                BackgroundColor = Color.Lavender,
                Content         = new Label
                {
                    Font      = _font,
                    Text      = "Show Keyboard Input",
                    TextColor = Color.Black
                },
                Location = new PointF(20, 200)
            };

            buttonShowKeyboardInput.Content.SizeToFit();
            buttonShowKeyboardInput.SizeToFit();
            buttonShowKeyboardInput.Tapped += (sender, e) => {
                TestShowKeyboardInput(
                    "Some normal title",
                    "And a perfectly ordinary description",
                    "the default");
            };

            var buttonShowKeyboardInputLong = new Button
            {
                BackgroundColor = Color.Lavender,
                Content         = new Label
                {
                    Font      = _font,
                    Text      = "Show Keyboard Input (long)",
                    TextColor = Color.Black
                },
                Location = new PointF(
                    buttonShowKeyboardInput.Frame.Left,
                    buttonShowKeyboardInput.Frame.Bottom + 10)
            };

            buttonShowKeyboardInputLong.Content.SizeToFit();
            buttonShowKeyboardInputLong.SizeToFit();
            buttonShowKeyboardInputLong.Tapped += (sender, e) => {
                TestShowKeyboardInput(
                    "This is the title that never ends, yes it goes on and on my friends.  One " +
                    "day some people started writing it, etc",
                    "And here is a super-duper description that rambles on a bit about, you " +
                    "know, whatever.  And then finally ends over here at about this point.",
                    "surprisingly terse default");
            };

            _universe.Add(exitButton);
            _universe.Add(_labelEndShowKeyboardInput);
            _universe.Add(_labelShowKeyboardInputCallback);
            _universe.Add(buttonShowKeyboardInput);
            _universe.Add(buttonShowKeyboardInputLong);
        }