コード例 #1
0
ファイル: Character.cs プロジェクト: o3a/ZombieSmashersXNA4
        private void CheckXCol(Map map, Vector2 pLoc)
        {
            if (Trajectory.X + ColMove > 0f)
                if (map.CheckCol(new Vector2(Loc.X + 25f, Loc.Y - 15f)))
                    Loc.X = pLoc.X;

            if (Trajectory.X + ColMove < 0f)
                if (map.CheckCol(new Vector2(Loc.X - 25f, Loc.Y - 15f)))
                    Loc.X = pLoc.X;
        }
コード例 #2
0
ファイル: Character.cs プロジェクト: o3a/ZombieSmashersXNA4
        public void Update(Map map, ParticleManager pMan, Character[] c)
        {
            if (Ai != null)
                Ai.Update(c, ID, map);

            PressedKey = PressedKeys.None;
            if (KeyAttack)
            {
                PressedKey = PressedKeys.Attack;
                if (KeyUp) PressedKey = PressedKeys.Lower;
                if (KeyDown) PressedKey = PressedKeys.Upper;
            }
            if (KeySecondary)
            {
                PressedKey = PressedKeys.Secondary;
                if (KeyUp) PressedKey = PressedKeys.SecUp;
                if (KeyDown) PressedKey = PressedKeys.SecDown;
            }
            if (PressedKey > PressedKeys.None)
            {
                if (GotoGoal[(int)PressedKey] > -1)
                {
                    SetFrame(GotoGoal[(int)PressedKey]);

                    if (KeyLeft)
                        Face = CharDir.Left;
                    if (KeyRight)
                        Face = CharDir.Right;

                    PressedKey = PressedKeys.None;

                    for (int i = 0; i < GotoGoal.Length; i++)
                        GotoGoal[i] = -1;

                    frame = 0f;
                }
            }

            if (StunFrame > 0f)
                StunFrame -= Game1.FrameTime;

            #region Update Dying
            if (DyingFrame > -1f)
            {
                DyingFrame += Game1.FrameTime;

            }
            #endregion

            #region Update Animation
            if (DyingFrame < 0f)
            {
                Animation animation = charDef.GetAnimation(Anim);
                KeyFrame keyframe = animation.GetKeyFrame(AnimFrame);

                frame += Game1.FrameTime * 30.0f;

                if (frame > (float)keyframe.Duration)
                {
                    int pframe = AnimFrame;

                    script.DoScript(Anim, AnimFrame);
                    CheckTrig(pMan);

                    frame -= (float)keyframe.Duration;
                    if (AnimFrame == pframe)
                        AnimFrame++;

                    keyframe = animation.GetKeyFrame(AnimFrame);

                    if (AnimFrame >=
                        animation.getKeyFrameArray().Length)
                        AnimFrame = 0;

                }

                if (keyframe.FrameRef < 0)
                    AnimFrame = 0;

                if (AnimName == "jhit")
                {
                    if (Trajectory.Y > -100f)
                        SetAnim("jmid");
                }
            }
            #endregion

            #region Collison w/ other characters
            for (int i = 0; i < c.Length; i++)
            {
                if (i != ID)
                {
                    if (c[i] != null)
                    {
                        if (!ethereal && !c[i].ethereal)
                        {
                            if (Loc.X > c[i].Loc.X - 90f * c[i].Scale &&
                                Loc.X < c[i].Loc.X + 90f * c[i].Scale &&
                                Loc.Y > c[i].Loc.Y - 120f * c[i].Scale &&
                                Loc.Y < c[i].Loc.Y + 10f * c[i].Scale)
                            {
                                float dif = (float)Math.Abs(Loc.X - c[i].Loc.X);
                                dif = 180f * c[i].Scale - dif;
                                dif *= 2f;
                                if (Loc.X < c[i].Loc.X)
                                {
                                    ColMove = -dif;
                                    c[i].ColMove = dif;
                                }
                                else
                                {
                                    ColMove = dif;
                                    c[i].ColMove = -dif;
                                }
                            }
                        }
                    }
                }
            }
            if (ColMove > 0f)
            {
                ColMove -= 400f * Game1.FrameTime;
                if (ColMove < 0f) ColMove = 0f;
            }
            if (ColMove < 0f)
            {
                ColMove += 400f * Game1.FrameTime;
                if (ColMove > 0f) ColMove = 0f;
            }
            #endregion

            #region Update loc by trajectory

            Vector2 pLoc = new Vector2(Loc.X, Loc.Y);

            if (State == CharState.Grounded || (State == CharState.Air && Floating))
            {
                if (Trajectory.X > 0f)
                {
                    Trajectory.X -= Game1.Friction * Game1.FrameTime;
                    if (Trajectory.X < 0f) Trajectory.X = 0f;
                }
                if (Trajectory.X < 0f)
                {
                    Trajectory.X += Game1.Friction * Game1.FrameTime;
                    if (Trajectory.X > 0f) Trajectory.X = 0f;
                }
            }
            Loc.X += Trajectory.X * Game1.FrameTime;
            Loc.X += ColMove * Game1.FrameTime;
            if (State == CharState.Air)
            {
                Loc.Y += Trajectory.Y * Game1.FrameTime;
            }
            #endregion

            #region Collision detection
            if (State == CharState.Air)
            {
                #region Air State
                if (Floating)
                {
                    Trajectory.Y += Game1.FrameTime * Game1.Gravity * 0.5f;
                    if (Trajectory.Y > 100f) Trajectory.Y = 100f;
                    if (Trajectory.Y < -100f) Trajectory.Y = -100f;

                }
                else
                    Trajectory.Y += Game1.FrameTime * Game1.Gravity;

                CheckXCol(map, pLoc);

                #region Land on ledge
                if (Trajectory.Y > 0.0f)
                {
                    for (int i = 0; i < 16; i++)
                    {
                        if (map.GetLedgeTotalNodes(i) > 1)
                        {

                            int ts = map.GetLedgeSec(i, pLoc.X);
                            int s = map.GetLedgeSec(i, Loc.X);
                            float fY;
                            float tfY;
                            if (s > -1 && ts > -1)
                            {

                                tfY = map.GetLedgeYLoc(i, s, pLoc.X);
                                fY = map.GetLedgeYLoc(i, s, Loc.X);
                                if (pLoc.Y <= tfY && Loc.Y >= fY)
                                {
                                    if (Trajectory.Y > 0.0f)
                                    {

                                        Loc.Y = fY;
                                        ledgeAttach = i;
                                        Land();
                                    }
                                }
                                else

                                    if (map.GetLedgeFlags(i) == (int)LedgeFlags.Solid
                                        &&
                                        Loc.Y >= fY)
                                    {
                                        Loc.Y = fY;
                                        ledgeAttach = i;
                                        Land();
                                    }
                            }

                        }
                    }
                }
                #endregion

                #region Land on col
                if (State == CharState.Air)
                {
                    if (Trajectory.Y > 0f)
                    {
                        if (map.CheckCol(new Vector2(Loc.X, Loc.Y + 15f)))
                        {
                            Loc.Y = (float)((int)((Loc.Y + 15f) / 64f) * 64);
                            Land();
                        }
                    }
                }
                #endregion

                #endregion
            }
            else if (State == CharState.Grounded)
            {
                #region Grounded State

                if (ledgeAttach > -1)
                {
                    if (map.GetLedgeSec(ledgeAttach, Loc.X) == -1)
                    {
                        FallOff();
                    }
                    else
                    {
                        Loc.Y = map.GetLedgeYLoc(ledgeAttach,
                            map.GetLedgeSec(ledgeAttach, Loc.X), Loc.X);
                    }
                }
                else
                {
                    if (!map.CheckCol(new Vector2(Loc.X, Loc.Y + 15f)))
                        FallOff();
                }

                CheckXCol(map, pLoc);

                #endregion
            }
            #endregion

            #region Key input
            if (AnimName == "idle" || AnimName == "run" ||
                (State == CharState.Grounded && CanCancel))
            {
                if (AnimName == "idle" || AnimName == "run")
                {
                    if (KeyLeft)
                    {
                        SetAnim("run");
                        Trajectory.X = -Speed;
                        Face = CharDir.Left;
                    }
                    else if (KeyRight)
                    {
                        SetAnim("run");
                        Trajectory.X = Speed;
                        Face = CharDir.Right;
                    }
                    else
                    {
                        SetAnim("idle");
                    }
                }
                if (KeyAttack)
                {
                    SetAnim("attack");
                }
                if (KeySecondary)
                {
                    SetAnim("second");
                }
                if (KeyJump)
                {
                    SetAnim("jump");
                }
                if (RightAnalog.X > 0.2f || RightAnalog.X < -0.2f)
                {
                    SetAnim("roll");
                    if (AnimName == "roll")
                    {
                        if (RightAnalog.X > 0f)
                            Face = CharDir.Right;
                        else
                            Face = CharDir.Left;
                    }
                }
            }

            if (AnimName == "fly" ||
                (State == CharState.Air && CanCancel))
            {
                if (KeyLeft)
                {
                    Face = CharDir.Left;
                    if (Trajectory.X > -Speed)
                        Trajectory.X -= 500f * Game1.FrameTime;
                }
                if (KeyRight)
                {
                    Face = CharDir.Right;
                    if (Trajectory.X < Speed)
                        Trajectory.X += 500f * Game1.FrameTime;
                }
                if (KeySecondary)
                {
                    SetAnim("fsecond");
                }
                if (KeyAttack)
                {
                    SetAnim("fattack");
                }
            }

            #endregion
        }