Ejemplo n.º 1
0
        /// <summary>
        /// Init: pixie herself (a default implementation is in Level)
        /// </summary>
        protected virtual void InitPixie()
        {
            pixie = new Pixie();
            pixie.PositionAndTarget = PIXIE_STARTING_POS;
            pixie.TargetSpeed       = PIXIE_TARGETSPEED;
            Add(pixie);

            keyControl = new PixieKeyControl();
            pixie.Add(keyControl);
        }
Ejemplo n.º 2
0
        protected override void OnUpdate(ref UpdateParams p)
        {
            base.OnUpdate(ref p);

            // update BoundingRectangle values to latest and greatest
            BoundingRectangle.X = TargetX;
            BoundingRectangle.Y = TargetY;

            // update position of the smooth motion of this Thing in the TTengine
            // update position when attached to a parent Thing
            if (Parent is Thing)
            {
                Thing t = Parent as Thing;
                Target          = t.Target + AttachmentPosition;
                Motion.Position = Motion.ScaleAbs * FromPixels(AttachmentPosition);
            }
            else
            {                                                                                         // not attached to a parent Thing
                Motion.Position = Screen.Center + Motion.ScaleAbs * (FromPixels(Position - ViewPos)); // TODO ViewPos smoothing using Draw cache?
                //Motion.Position = Position - ViewPos; // alternative to above
            }

            // compute target move for this thing based on child controls
            TargetMove = Vector2.Zero;
            foreach (Gamelet g in Children)
            {
                if (g is ThingControl)
                {
                    ThingControl control = g as ThingControl;
                    if (control.IsTargetMoveDefined)
                    {
                        TargetMove += control.TargetMove;
                        TargetMove *= control.TargetMoveMultiplier;
                    }
                }
            }

            // compute new facingDirection from final TargetMove
            if (TargetMove.LengthSquared() > 0f)
            {
                FacingDirection = TargetMove;
                FacingDirection.Normalize();

                // take steering inputs if any, and move Thing, applying collision detection
                // if passable...
                List <Thing> cols = DetectCollisions(TargetMove);
                cols.RemoveAll(item => Children.Contains(item)); // do not heed my own attached-items in collision - they move along
                if (IsCollisionFree || (!CollidesWithBackground(TargetMove) && cols.Count == 0))
                {
                    bool ok = true;
                    if (!IsCollisionFree)
                    {
                        // check all attached Things too
                        foreach (Gamelet g in Children)
                        {
                            if (g is Thing && g.Visible)
                            {
                                Thing t = g as Thing;
                                if (t.IsCollisionFree)
                                {
                                    continue;
                                }

                                // first, test if hits background
                                if (t.CollidesWithBackground(TargetMove))
                                {
                                    ok = false;
                                    break;
                                }

                                // if not, test if it hits others that are not attached to same parent and are not pixie
                                List <Thing> colsChild = t.DetectCollisions(TargetMove);
                                colsChild.RemoveAll(item => Children.Contains(item));
                                colsChild.Remove(Level.Current.pixie);
                                if (colsChild.Count > 0)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                    }
                    // if there are no objections of main Thing (or its attachment) to the move, then move.
                    if (ok)
                    {
                        Target += TargetMove;
                        TTutil.Round(ref Target);
                    }
                }
            }

            Vector2 vdif = Target - Position;

            if (vdif.LengthSquared() > 0f) // if target not reached yet
            {
                if (false)
                {
                    Position = Target; // debug
                }
                else
                {
                    Vector2 vmove = vdif;
                    vmove.Normalize();
                    vmove *= TargetSpeed * Velocity;
                    // convert speed vector to move vector (x = v * t)
                    vmove *= p.Dt;
                    // check if target reached already (i.e. move would overshoot target)
                    if (vmove.LengthSquared() >= vdif.LengthSquared())
                    {
                        Position = Target;
                    }
                    else
                    {
                        // apply move towards target
                        Position += vmove;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        protected override void OnUpdate(ref UpdateParams p)
        {
            base.OnUpdate(ref p);

            // update position of the smooth motion of this Thing in the TTengine
            // update position when attached to a parent Thing
            if (Parent is Thing)
            {
                Thing t = Parent as Thing;
                Target          = t.Target + AttachmentPosition;
                Motion.Position = Motion.ScaleAbs * FromPixels(AttachmentPosition);
            }
            else
            {                                                                                         // not attached to a parent Thing
                Motion.Position = Screen.Center + Motion.ScaleAbs * (FromPixels(Position - ViewPos)); // TODO ViewPos smoothing using Draw cache?
                //Motion.Position = Position - ViewPos; // alternative to above
            }

            // compute target move for this thing based on child ThingControl controls
            TargetMove = Vector2.Zero;
            foreach (Gamelet g in Children)
            {
                if (g is ThingControl)
                {
                    ThingControl control = g as ThingControl;
                    if (control.IsTargetMoveDefined)
                    {
                        TargetMove += control.TargetMove;
                        TargetMove *= control.TargetMoveMultiplier;
                    }
                }
            }

            // compute new facingDirection from final TargetMove
            if (TargetMove.LengthSquared() > 0f)
            {
                FacingDirection = TargetMove;
                FacingDirection.Normalize();
            }

            // take steering inputs if any, and move Thing, applying collision detection
            if (TargetMove.LengthSquared() > 0f)
            {
                // check if passable...
                List <Thing> cols = DetectCollisions(TargetMove);

                if (!IsCollisionFree && Pushing != null && !IsCollisionFree && cols.Count > 0 && Pushing.Force > 0f)
                {
                    // no - so try to push neighbouring things away
                    foreach (Thing t in cols)
                    {
                        if (t.Pushing != null)
                        {
                            t.Pushing.BePushed(TargetMove);
                        }
                    }
                }

                if (IsCollisionFree || (!CollidesWithBackground(TargetMove) && cols.Count == 0))
                {
                    // yes - passable
                    bool ok = true;
                    if (!IsCollisionFree)
                    {
                        // check all attached Things too
                        foreach (Gamelet g in Children)
                        {
                            if (g is Thing)
                            {
                                Thing t = g as Thing;
                                if (t.IsCollisionFree)
                                {
                                    continue;
                                }

                                // first, test if hits background
                                if (t.CollidesWithBackground(TargetMove))
                                {
                                    ok = false;
                                    break;
                                }

                                // if not, test if it hits others
                                List <Thing> colsChild = t.DetectCollisions(TargetMove);
                                if (colsChild.Count > 0)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                    }
                    // if there are no objections of main Thing (or its attachment) to the move, then move.
                    if (ok)
                    {
                        Target += TargetMove;
                        TTutil.Round(ref Target);
                    }
                }
            }

            Vector2 vdif = Target - Position;

            if (vdif.LengthSquared() > 0f) // if target not reached yet
            {
                Vector2 vmove = vdif;
                vmove.Normalize();
                vmove *= TargetSpeed * Velocity;
                // convert speed vector to move vector (x = v * t)
                vmove *= p.Dt;
                // check if target reached already (i.e. move would overshoot target)
                if (vmove.LengthSquared() >= vdif.LengthSquared())
                {
                    Position = Target;
                }
                else
                {
                    // apply move towards target
                    Position += vmove;
                }
            }
        }