public override void OnAddedToEntity() { var texture = Entity.Scene.Content.LoadSpriteAtlas("Content/Assets/Player/Player.atlas"); _boxCollider = Entity.GetComponent <BoxCollider>(); _mover = Entity.GetComponent <TiledMapMover>(); _animator = Entity.AddComponent(new SpriteAnimator()); _animator.AddAnimationsFromAtlas(texture); _animator.Speed = 0.7f; _animator.Play("idle"); SetupInput(); }
void IUpdatable.Update() { // handle movement and animations var moveDir = new Vector2(_xAxisInput.Value, 0); string animation = null; if (moveDir.X < 0) { if (_collisionState.Below) { animation = "running"; } _animator.FlipX = true; _velocity.X = -MoveSpeed; } else if (moveDir.X > 0) { if (_collisionState.Below) { animation = "running"; } _animator.FlipX = false; _velocity.X = MoveSpeed; } else { _velocity.X = 0; if (_collisionState.Below) { animation = "idle"; } } if (_collisionState.Above) { _velocity.Y = 0; } if (_collisionState.Below && _jumpInput.IsPressed) { animation = "jump"; _velocity.Y = -Mathf.Sqrt(2f * JumpHeight * Gravity); } if (!_collisionState.Below && _velocity.Y >= 0) { animation = "fall"; // apply gravity } _velocity.Y += Gravity * Time.DeltaTime; // move _mover.Move(_velocity * Time.DeltaTime, _boxCollider, _collisionState); if (_collisionState.Below) { _velocity.Y = 0; } if (animation != null && !_animator.IsAnimationActive(animation)) { _animator.Play(animation); } }