public static void Update(EntityComponentStorage ecs, GameTime gameTime) { Vector2 oldPos; Vector2 newPos; // for all entities with a position component foreach (Eid posEid in ecs.componentEids[ecs.ComponentCids[typeof(PositionComponent)]]) { // position component of current entity changes according to that entities movement component PositionComponent posComp = ((PositionComponent)ecs.Entities[posEid].Components[ ecs.ComponentCids[typeof(PositionComponent)]]); // entity movement component stores velocity MovementComponent moveComp = ((MovementComponent)ecs.Entities[posEid].Components[ ecs.ComponentCids[typeof(MovementComponent)]]); if (posComp != null && moveComp != null) { // new position += delta * speed oldPos = posComp.Position; newPos = oldPos + Vector2.Multiply(moveComp.Velocity, (float)gameTime.ElapsedGameTime.TotalMilliseconds ); posComp.Position = newPos; } } }
// for now, camera follows entity with eid <see cref="CurCameraEid"/> public static void Update(EntityComponentStorage ecs, GameTime gameTime) { if (CurCameraEid != null) { _cameraComponent = (CameraComponent)(ecs.Entities[(int)CurCameraEid]. Components[ecs.ComponentCids[typeof(CameraComponent)]]); _positionComponent = (PositionComponent)(ecs.Entities[(int)CurCameraEid]. Components[ecs.ComponentCids[typeof(PositionComponent)]]); _movementComponent = (MovementComponent)(ecs.Entities[(int)CurCameraEid]. Components[ecs.ComponentCids[typeof(MovementComponent)]]); if ((_cameraComponent != null) && (_positionComponent != null)) { var moveFactor = _movementComponent.MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; _cameraComponent.Position += new Vector3((_positionComponent.Position.X - _cameraComponent.Position.X) * moveFactor.X, (_positionComponent.Position.Y - _cameraComponent.Position.Y) * moveFactor.Y, 0 ); } else { System.Diagnostics.Debug.WriteLine("Sorry Sir! Null component reference"); } } }
public static void Draw(EntityComponentStorage ecs) { Component renderComponent; Component positionComponent; Texture2D renderSpriteSheet = null; Rectangle renderRect = new Rectangle(0, 0, 0, 0); Vector2 renderToPos = new Vector2(0, 0); // render all entities with a position component foreach (Eid renderEid in ecs.componentEids[ecs.ComponentCids[typeof(RenderComponent)]]) { renderComponent = ecs.Entities[renderEid].Components[ ecs.ComponentCids[typeof(RenderComponent)]]; positionComponent = ecs.Entities[renderEid].Components[ ecs.ComponentCids[typeof(PositionComponent)]]; // need position & render components if (positionComponent != null && renderComponent != null) { renderToPos = ((PositionComponent)positionComponent).Position; renderSpriteSheet = ((RenderComponent)renderComponent).SpriteSheet; renderRect = ((RenderComponent)renderComponent).Rect; RPGGame.spriteBatch.Draw(renderSpriteSheet, renderToPos, renderRect, Color.White ); } } }
public MovementComponent( EntityComponentStorage ecs, Vector2 velocity, Vector2 moveSpeed) : base(ecs) { this.Velocity = velocity; this.MoveSpeed = moveSpeed; }
public RenderComponent(EntityComponentStorage ecs, Texture2D spriteSheet, Rectangle rect ) : base(ecs) { this.SpriteSheet = spriteSheet; this.Rect = rect; }
public CameraComponent(EntityComponentStorage ecs, Eid curCameraEid, Vector3 origin, Vector3 position, float scale) : base(ecs) { this.Origin = origin / scale; // since transform matrix multiplied by scale this.Position = position; this.Scale = scale; CameraSystem.CurCameraEid = curCameraEid; }
public static void Update(EntityComponentStorage ecs, KeyboardState keyState) { // "Keys" is a monogame enum Keys[] pressedKeys = keyState.GetPressedKeys(); // grab first entity with input component. // input component contains no data, just indicates which Entity should be acted // upon by InputSystem Eid inputEid = ecs.componentEids[ ecs.ComponentCids[typeof(InputComponent)]][0]; // this entity must have a movement component MovementComponent movementComponent = ((MovementComponent)ecs.Entities[inputEid]. Components[ecs.ComponentCids[typeof(MovementComponent)]]); if (movementComponent != null) { // reset velocity of current entity movementComponent.Velocity = Vector2.Zero; // delegate modifies position component of the entity with input component Action <Vector2> addToInputEntVelocity; addToInputEntVelocity = value => ((MovementComponent)ecs.Entities[inputEid]. Components[ecs.ComponentCids[typeof(MovementComponent)]]).Velocity += value; // appropriately modify position component of input entity foreach (Keys pressedKey in pressedKeys) { switch (pressedKey) { case Keys.W: addToInputEntVelocity(MovementSystem.MoveVectors.UP * movementComponent.MoveSpeed); break; case Keys.A: addToInputEntVelocity(MovementSystem.MoveVectors.LEFT * movementComponent.MoveSpeed); break; case Keys.S: addToInputEntVelocity(MovementSystem.MoveVectors.DOWN * movementComponent.MoveSpeed); break; case Keys.D: addToInputEntVelocity(MovementSystem.MoveVectors.RIGHT * movementComponent.MoveSpeed); break; } } } }
protected Component(EntityComponentStorage ecs) { // assign a component to a unique Cid, if this has not already been done if (!ecs.ComponentCids.ContainsKey(this.GetType())) { ecs.ComponentCids.Add(this.GetType(), (Cid)(EntityComponentStorage.NumComponents - ecs.NumComponentsAdded - 1) ); ecs.NumComponentsAdded++; } }
public PositionComponent(EntityComponentStorage ecs, Vector2 position) : base(ecs) { this.Position = position; }
public InputComponent(EntityComponentStorage ecs) : base(ecs) { }
public MovementComponent(EntityComponentStorage ecs) : this(ecs, new Vector2(0, 0), new Vector2(1, 1)) { }
public HealthComponent(EntityComponentStorage ecs) : base(ecs) { }
public CollisionComponent(EntityComponentStorage ecs) : base(ecs) { }