private void FireBullet() { Debug.Assert(Entity != null, nameof(Entity) + " != null"); Debug.Assert(Entity.Scene != null, "Entity.Scene != null"); var entity = new Entity(); Entity.Scene.AddEntity(entity); var transform = TransformHierarchy.Calculate2DTransformationMatrix(Entity); entity.AddComponent(new Transform2DComponent { Translation = (transform * new Vector2(0, 20).Homogeneous).ToVector2(), Rotation = 0, Scale = Vector2.One }); Debug.Assert(Random != null, nameof(Random) + " != null"); entity.AddComponent(new EllipseRendererComponent { Color = Color.FromArgb(1, Random.NextDouble(), Random.NextDouble(), Random.NextDouble()), Radius = 5, FillInterior = true, OrderInLayer = 2 }); entity.AddComponent(new BulletBehaviorComponent { Velocity = _cannonRotorTransform.VectorY * 200 }); }
public void Calculate2DTransformationMatrix_ShouldReturnTransformOfEntity_GivenEntityIsRoot() { // Arrange var rootTransform2DComponent = CreateRandomTransform2DComponent(); var expected = rootTransform2DComponent.ToMatrix(); var rootEntity = new Entity(); rootEntity.AddComponent(rootTransform2DComponent); // Act var transformationMatrix = TransformHierarchy.Calculate2DTransformationMatrix(rootEntity); // Assert Assert.That(transformationMatrix, Is.EqualTo(expected).Using(Matrix3x3Comparer)); }
public void ProcessPhysics(Scene scene) { var entities = scene.AllEntities.Where(e => e.HasComponent <Transform2DComponent>() && e.HasComponent <Collider2DComponent>()).ToArray(); _colliders.Clear(); _transforms.Clear(); foreach (var entity in entities) { var collider2D = entity.GetComponent <Collider2DComponent>(); _transforms.Add(TransformHierarchy.Calculate2DTransformationMatrix(entity)); _colliders.Add(collider2D); collider2D.ClearCollidingEntities(); } for (var i = 0; i < entities.Length; i++) { var entity1 = entities[i]; var collider1 = _colliders[i]; var transform1 = _transforms[i]; var shape1 = CreateShapeForCollider(collider1, transform1); for (var j = i + 1; j < entities.Length; j++) { var entity2 = entities[j]; var collider2 = _colliders[j]; var transform2 = _transforms[j]; IShape shape2 = CreateShapeForCollider(collider2, transform2); if (shape1.Overlaps(shape2)) { collider1.AddCollidingEntity(entity2); collider2.AddCollidingEntity(entity1); } } } }
public void Calculate2DTransformationMatrix_ShouldReturnProductOfTransformOfEntityAndTransformOfParentEntity_GivenEntityHasParent() { // Arrange var level0Transform2DComponent = CreateRandomTransform2DComponent(); var level1Transform2DComponent = CreateRandomTransform2DComponent(); var expected = level0Transform2DComponent.ToMatrix() * level1Transform2DComponent.ToMatrix(); var level0Entity = new Entity(); level0Entity.AddComponent(level0Transform2DComponent); var level1Entity = new Entity(); level1Entity.AddComponent(level1Transform2DComponent); level0Entity.AddChild(level1Entity); // Act var transformationMatrix = TransformHierarchy.Calculate2DTransformationMatrix(level1Entity); // Assert Assert.That(transformationMatrix, Is.EqualTo(expected).Using(Matrix3x3Comparer)); }
private void RenderEntities(Matrix3x3 cameraTransformationMatrix) { foreach (var entity in _renderList) { var transformationMatrix = TransformHierarchy.Calculate2DTransformationMatrix(entity); transformationMatrix = cameraTransformationMatrix * transformationMatrix; if (entity.HasComponent <SpriteRendererComponent>()) { var sprite = entity.GetComponent <SpriteRendererComponent>().Sprite; if (sprite != null) { _renderer2D.RenderSprite(sprite, transformationMatrix); } } if (entity.HasComponent <TextRendererComponent>()) { var textRenderer = entity.GetComponent <TextRendererComponent>(); _renderer2D.RenderText(textRenderer.Text, textRenderer.FontSize, textRenderer.Color, transformationMatrix); } if (entity.HasComponent <RectangleRendererComponent>()) { var rectangleRenderer = entity.GetComponent <RectangleRendererComponent>(); var rectangle = new AxisAlignedRectangle(rectangleRenderer.Dimension); _renderer2D.RenderRectangle(rectangle, rectangleRenderer.Color, rectangleRenderer.FillInterior, transformationMatrix); } if (entity.HasComponent <EllipseRendererComponent>()) { var ellipseRenderer = entity.GetComponent <EllipseRendererComponent>(); var ellipse = new Ellipse(ellipseRenderer.RadiusX, ellipseRenderer.RadiusY); _renderer2D.RenderEllipse(ellipse, ellipseRenderer.Color, ellipseRenderer.FillInterior, transformationMatrix); } } }