public void Update(GameTime gameTime) { ComponentManager cm = ComponentManager.GetInstance(); foreach (var entity in cm.GetComponentsOfType <PlayerControlComponent>()) { PlayerControlComponent playerControlComponent = (PlayerControlComponent)entity.Value; MoveComponent moveComponent = cm.GetComponentForEntity <MoveComponent>(entity.Key); if (moveComponent.CanMove) { moveComponent.Velocity = playerControlComponent.Movement.GetDirection() * moveComponent.Speed; if (playerControlComponent.Movement.GetDirection() != new Vector2(0.0f, 0.0f)) { if (cm.HasEntityComponent <AnimationGroupComponent>(entity.Key)) { AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key); } if (cm.HasEntityComponent <SoundComponent>(entity.Key)) { cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Play; } } else { cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause; } } else { cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause; } } }
public void Render(RenderHelper renderHelper) { ComponentManager cm = ComponentManager.GetInstance(); Viewport viewport = GetCurrentViewport(renderHelper.graphicsDevice); Rectangle viewportBounds = viewport.Bounds; SpriteBatch spriteBatch = renderHelper.spriteBatch; RenderTiles(renderHelper); //Render all textures foreach (var entity in textures) { TextureComponent textureComponent = entity.Item1; PositionComponent positionComponent = entity.Item2; Point position = positionComponent.Position.ToPoint() - textureComponent.Offset; Rectangle textureBounds = new Rectangle(position.X, position.Y, textureComponent.Texture.Width, textureComponent.Texture.Height); if (viewportBounds.Intersects(textureBounds)) { spriteBatch.Draw(textureComponent.Texture, position.WorldToScreen(ref viewport).ToVector2(), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, renderHelper.GetLayerDepth(textureComponent.Layer)); } } //Render all animations foreach (var entity in animations) { AnimationComponent animationComponent = entity.Item1; PositionComponent positionComponent = entity.Item2; Point position = positionComponent.Position.ToPoint() - animationComponent.Offset; Rectangle animationBounds = new Rectangle(position.X, position.Y, animationComponent.FrameSize.X, animationComponent.FrameSize.Y); if (viewportBounds.Intersects(animationBounds)) { spriteBatch.Draw(animationComponent.SpriteSheet, position.WorldToScreen(ref viewport).ToVector2(), animationComponent.SourceRectangle, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, renderHelper.GetLayerDepth(animationComponent.Layer)); } } //Render all animationgroups foreach (var entity in animationGroups) { AnimationGroupComponent animationComponent = entity.Item1; PositionComponent positionComponent = entity.Item2; Point position = positionComponent.Position.ToPoint() - animationComponent.Offset; Rectangle animationBounds = new Rectangle(position.X, position.Y, animationComponent.FrameSize.X, animationComponent.FrameSize.Y); if (viewportBounds.Intersects(animationBounds)) { spriteBatch.Draw(animationComponent.Spritesheet, position.WorldToScreen(ref viewport).ToVector2(), animationComponent.SourceRectangle, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, renderHelper.GetLayerDepth(animationComponent.Layer)); } } }
void ChangeEquipmentDirection(ComponentManager cm, ref InventoryComponent invenComp, int pos, int anim) { if (invenComp.WeaponBodyHead[pos] != 0 && cm.HasEntityComponent <AnimationGroupComponent>(invenComp.WeaponBodyHead[pos])) { AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(invenComp.WeaponBodyHead[pos]); if (animGroupComp.ActiveAnimation != anim) { animGroupComp.ActiveAnimation = anim; } } }
public void Update(GameTime gameTime) { var cm = ComponentManager.GetInstance(); foreach (var entity in cm.GetComponentsOfType <AIComponent>()) { if (cm.HasEntityComponent <MoveComponent>(entity.Key)) { MoveComponent moveComp = ComponentManager.GetInstance().GetComponentForEntity <MoveComponent>(entity.Key); PositionComponent posComp = cm.GetComponentForEntity <PositionComponent>(entity.Key); if (((AIComponent)entity.Value).TargetEntity != 0) //Vector2.Distance(((AIComponent)entity.Value).Destination.ToVector2(), posComp.Position) > 10 { if (moveComp.CanMove) { Vector2 pointToCompare = posComp.Position + new Vector2(moveComp.Direction.X * 5, moveComp.Direction.Y * 5); Vector2 nextMovement = new Vector2(((AIComponent)entity.Value).Destination.X - pointToCompare.X, ((AIComponent)entity.Value).Destination.Y - pointToCompare.Y); float distance = (float)Math.Sqrt(nextMovement.X * nextMovement.X + nextMovement.Y * nextMovement.Y); if (distance > 2f) { moveComp.Velocity = new Vector2(nextMovement.X / distance, nextMovement.Y / distance) * moveComp.Speed; cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Play; if (cm.HasEntityComponent <AnimationGroupComponent>(entity.Key)) { AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key); int anim = GetAnimationRow(moveComp.Direction) + 4; if (animGroupComp.ActiveAnimation != anim) { animGroupComp.ActiveAnimation = anim; } } } else { moveComp.Velocity = new Vector2(0, 0); cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause; } } } else { AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key); int anim = GetAnimationRow(moveComp.Direction); if (animGroupComp.ActiveAnimation != anim) { animGroupComp.ActiveAnimation = anim; } moveComp.Velocity = new Vector2(0, 0); cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause; } } } }
public void Update(GameTime gameTime) { ComponentManager cm = ComponentManager.GetInstance(); foreach (var entity in cm.GetComponentsOfType <ArmComponent>()) { ArmComponent armComp = (ArmComponent)entity.Value; MoveComponent moveComp = cm.GetComponentForEntity <MoveComponent>(armComp.playerID); AnimationGroupComponent armAnimation = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key); KnockbackComponent knockbackComp = cm.GetComponentForEntity <KnockbackComponent>(armComp.playerID); if (knockbackComp != null && knockbackComp.KnockbackActive) { return; } if (!cm.HasEntityComponent <AnimationGroupComponent>(armComp.playerID)) { return; } AnimationGroupComponent playerAnimation = cm.GetComponentForEntity <AnimationGroupComponent>(armComp.playerID); int animation = GetAnimationRow(moveComp.Direction); int walking = cm.GetComponentForEntity <MoveComponent>(armComp.playerID).Velocity != new Vector2(0.0f, 0.0f) ? 4 : 0; int attacking = cm.GetComponentForEntity <AttackComponent>(armComp.playerID).AttackCooldown > 0.0f ? 4 : 0; if (playerAnimation.ActiveAnimation != animation + walking) { playerAnimation.ActiveAnimation = animation + walking; } if (armAnimation.ActiveAnimation != animation + attacking) { armAnimation.ActiveAnimation = animation + attacking; } if (cm.HasEntityComponent <InventoryComponent>(armComp.playerID)) { InventoryComponent invenComp = cm.GetComponentForEntity <InventoryComponent>(armComp.playerID); ChangeEquipmentDirection(cm, ref invenComp, 0, animation + attacking); ChangeEquipmentDirection(cm, ref invenComp, 1, animation + walking); ChangeEquipmentDirection(cm, ref invenComp, 2, animation + walking); } } }
private void Update(GameTime gameTime, AnimationGroupComponent animationGroupComponent) { AnimationGroupComponent a = animationGroupComponent; if (a.IsPaused) { return; } a.LastFrameTime += gameTime.ElapsedGameTime.TotalMilliseconds; if (a.LastFrameTime >= a.FrameDuration) { a.GroupFrame.X = (a.GroupFrame.X + 1) % a.Animations[a.ActiveAnimation].Item2.X; if (a.GroupFrame.X == 0) { a.GroupFrame.Y = (a.GroupFrame.Y + 1) % a.Animations[a.ActiveAnimation].Item2.Y; } a.CurrentFrame = a.GroupFrame + a.Animations[a.ActiveAnimation].Item1; a.SourceRectangle = new Rectangle(a.CurrentFrame * a.FrameSize, a.FrameSize); a.LastFrameTime -= a.FrameDuration; } }
public void Update(GameTime gameTime) { ComponentManager cm = ComponentManager.GetInstance(); List <Tuple <int, PositionComponent> > players = new List <Tuple <int, PositionComponent> >(); foreach (var player in cm.GetComponentsOfType <PlayerComponent>()) { players.Add(new Tuple <int, PositionComponent>(player.Key, cm.GetComponentForEntity <PositionComponent>(player.Key))); } foreach (var entity in cm.GetComponentsOfType <AttackComponent>()) { if (cm.HasEntityComponent <AIComponent>(entity.Key) && cm.HasEntityComponent <PositionComponent>(entity.Key)) { AIComponent ai = cm.GetComponentForEntity <AIComponent>(entity.Key); PositionComponent posComp = cm.GetComponentForEntity <PositionComponent>(entity.Key); //find closest target int closestEntity = 0; float closestDist = float.MaxValue; for (int i = 0; i < players.Count; i++) { float dist = Vector2.Distance(players[i].Item2.Position, posComp.Position); if (dist < ai.DetectRange && dist < closestDist) { closestEntity = players[i].Item1; closestDist = dist; } } if (closestEntity != 0) { ai.TargetEntity = closestEntity; } else { ai.TargetEntity = 0; } // Do some check here: cm.HasEntity....<movecomp> / <attackcomp> ? MoveComponent moveComp = cm.GetComponentForEntity <MoveComponent>(entity.Key); AttackComponent attackComponent = cm.GetComponentForEntity <AttackComponent>(entity.Key); if (ai.TargetEntity != 0) { Vector2 pointToTarget, pointToTCompare; Point nextDir; PositionComponent posOftarget = cm.GetComponentForEntity <PositionComponent>(ai.TargetEntity); CollisionComponent collComp = cm.GetComponentForEntity <CollisionComponent>(entity.Key); Vector2 unNormalizedDir = new Vector2(posOftarget.Position.X - posComp.Position.X, posOftarget.Position.Y - posComp.Position.Y); float distance = (float)Math.Sqrt(unNormalizedDir.X * unNormalizedDir.X + unNormalizedDir.Y * unNormalizedDir.Y); Vector2 direction = new Vector2(unNormalizedDir.X / distance, unNormalizedDir.Y / distance); nextDir = MoveSystem.CalcDirection(direction.X, direction.Y); pointToTarget = posOftarget.Position + new Vector2(-nextDir.X * (collComp.CollisionBox.Width), -nextDir.Y * (collComp.CollisionBox.Height)); pointToTCompare = posComp.Position + new Vector2(nextDir.X * (collComp.CollisionBox.Width), nextDir.Y * (collComp.CollisionBox.Height)); ai.Destination = pointToTarget.ToPoint(); if (attackComponent.AttackCooldown <= 0.0f && Vector2.Distance(posComp.Position, pointToTarget) <= (collComp.CollisionBox.Width * Math.Abs(nextDir.X) + collComp.CollisionBox.Height * Math.Abs(nextDir.Y)) / 2 && !cm.GetComponentForEntity <KnockbackComponent>(entity.Key).KnockbackActive) { moveComp.Direction = nextDir; moveComp.CanMove = false; attackComponent.AttackCooldown = attackComponent.RateOfFire; attackComponent.IsAttacking = true; cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Attack"].Action = SoundAction.Play; if (cm.HasEntityComponent <AnimationGroupComponent>(entity.Key)) { AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key); int anim = GetAnimationRow(moveComp.Direction) + 8; if (animGroupComp.ActiveAnimation != anim) { animGroupComp.ActiveAnimation = anim; } } } } if (attackComponent.AttackCooldown > 0.0f) { attackComponent.AttackCooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds; } else { moveComp.CanMove = true; } } } }