コード例 #1
0
ファイル: NavMesh.cs プロジェクト: janPierdolnikParda/RPG
        public void GetPortals()
        {
            Portals.Clear();

            for (int i = 0; i < TriPath.Count - 1; i++)
            {
                Pair <Vector3, Vector3> portal = new Pair <Vector3, Vector3>();

                if (TriPath[i].U == TriPath[i + 1].U ||
                    TriPath[i].U == TriPath[i + 1].V || TriPath[i].U == TriPath[i + 1].W)
                {
                    portal = new Pair <Vector3, Vector3>(TriPath[i].U.A, TriPath[i].U.B);
                }
                else if (TriPath[i].V == TriPath[i + 1].U ||
                         TriPath[i].V == TriPath[i + 1].V || TriPath[i].V == TriPath[i + 1].W)
                {
                    portal = new Pair <Vector3, Vector3>(TriPath[i].V.A, TriPath[i].V.B);
                }
                else
                {
                    portal = new Pair <Vector3, Vector3>(TriPath[i].W.A, TriPath[i].W.B);
                }

                if (Op2D.Cross(portal.first - TriPath[i].Center, portal.second - TriPath[i].Center) < 0)
                {
                    Vector3 tmp = portal.first;
                    portal.first  = portal.second;
                    portal.second = tmp;
                }
                Portals.Add(portal);
            }
            Portals.Add(new Pair <Vector3, Vector3>(EndPt, EndPt));
        }
コード例 #2
0
ファイル: NavMesh.cs プロジェクト: janPierdolnikParda/RPG
            public bool IsPointInside(Vector3 pt)
            {
                float area = 0;

                area += Op2D.TriArea(U.A - pt, U.B - pt);
                area += Op2D.TriArea(V.A - pt, V.B - pt);
                area += Op2D.TriArea(W.A - pt, W.B - pt);
                if (area > Area * 1.001f)
                {
                    return(false);
                }
                return(true);
            }
コード例 #3
0
        public CharacterDecTree()
        {
            // #1
            DecTree.FirstSucc resetAnim = new DecTree.FirstSucc(
                new DecTree.Assert(ch => ch.AnimBlender.CurrentAnimSet == "PickItemDown"),
                new DecTree.Job(ch =>
            {
                ch.AnimBlender.ResetAnimSet("PickItemDown");
                ch.AnimBlender.SetAnimSet("PickItemDown");
                ch.Velocity = Vector3.ZERO;
                ch.TurnTo(ch.PickingTarget.Position);
                return(true);
            }));
            // #2
            Func <Character, bool> animBendDown = (
                ch => ch.AnimBlender.AnimSetPhase("PickItemDown") > 0.5f
                );

            DecTree.FirstSucc bendDown = new DecTree.FirstSucc(
                new DecTree.Assert(animBendDown),
                new DecTree.FirstFail(
                    new DecTree.Assert(ch => ch.PickingTarget.Exists), // #2.1
                    new DecTree.Job(animBendDown)
                    )
                );
            // #3
            DecTree.FirstSucc pickItem = new DecTree.FirstSucc(
                new DecTree.Assert(ch => ch.PickingTarget == null || !ch.PickingTarget.Exists),
                new DecTree.Job(ch =>
            {
                ch.Inventory.Add(ch.PickingTarget.Profile.Clone());
                Engine.Singleton.ObjectManager.Destroy(ch.PickingTarget);
                ch.PickingTarget = null;
                return(true);  // #3.1
            }));
            // #4
            DecTree.Job bendUp = new DecTree.Job(
                ch => ch.AnimBlender.AnimSetPhase("PickItemDown") > 0.99f
                );

            PickItemDownSeq = new DecTree.FirstFail(
                resetAnim,
                bendDown,
                pickItem,
                bendUp);

            DecTree.Job       cleanUp      = new DecTree.Job(ch => { ch.PickItemOrder = false; ch.Contact = null; return(true); });
            DecTree.FirstFail pickItemNode = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.PickItemOrder),
                new DecTree.FirstSucc(PickItemDownSeq, cleanUp), // #1
                cleanUp);                                        // #2

            TurnJob = new DecTree.Job(ch =>
            {
                if (ch.TurnDelta != 0)
                {
                    Quaternion rotation = Quaternion.IDENTITY;
                    rotation.FromAngleAxis(new Degree(ch.TurnDelta), Vector3.UNIT_Y);
                    ch.Orientation *= rotation;
                    ch.TurnDelta    = 0;
                }
                return(true);
            });

            DecTree.FirstFail walkNode = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.MoveOrder),
                new DecTree.Job(ch =>
            {
                ch.Velocity = ch.Orientation * Vector3.UNIT_Z * ch.Profile.WalkSpeed;


                if (ch.RunOrder)
                {
                    ch.AnimBlender.SetAnimSet("Run");
                }
                else if (ch.Sword != null)
                {
                    if (ch.Sword.InUse)
                    {
                        ch.AnimBlender.SetAnimSet("WalkSword");
                    }
                    else
                    {
                        ch.AnimBlender.SetAnimSet("Walk");
                    }
                }
                else
                {
                    ch.AnimBlender.SetAnimSet("Walk");
                }

                return(true);
            }),
                TurnJob);

            DecTree.FirstFail walkNodeBack = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.MoveOrderBack),
                new DecTree.Job(ch =>
            {
                ch.Velocity = -(ch.Orientation * Vector3.UNIT_Z * ch.Profile.WalkSpeed);
                ch.AnimBlender.SetAnimSet("WalkBack");

                return(true);
            }),
                TurnJob);

            DecTree.FirstFail idleNode = new DecTree.FirstFail(
                new DecTree.Job(ch =>
            {
                ch.TalkPerm      = true;
                ch.InventoryPerm = true;
                ch.Velocity      = Vector3.ZERO;
                ch.AnimBlender.SetAnimSet("Idle");
                return(true);
            }),
                TurnJob);

            DecTree.FirstFail followPathNode = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.FollowPathOrder),
                new DecTree.Job(ch =>
            {
                if (Op2D.Dist(ch.Position, ch.WalkPath[0]) < 0.3f)
                {
                    ch.WalkPath.RemoveAt(0);
                }

                if (ch.obejdz)
                {
                    //ch.WalkPath.Insert(0, ch.Position + new Vector3(1, 0, 0));
                    ch.obejdz = false;
                    Engine.Singleton.CurrentLevel.navMesh.AStar(ch.Position + new Vector3(1, 0, 0), ch.Activities.Activities[ch.Activities.Index].v3);
                    if (Engine.Singleton.CurrentLevel.navMesh.TriPath.Count > 1)
                    {
                        Engine.Singleton.CurrentLevel.navMesh.GetPortals();
                        ch.WalkPath = Engine.Singleton.CurrentLevel.navMesh.Funnel();
                    }
                }

                if (ch.WalkPath.Count == 0)
                {
                    ch.FollowPathOrder = false;

                    if (ch.Activities.InProgress &&
                        ch.Activities.Activities[ch.Activities.Index].Type == ActivityType.WALK)
                    {
                        ch.Activities.EndActivity();
                    }

                    return(true);
                }
                else if (ch.Contacts.Count > 0)
                {
                    Engine.Singleton.CurrentLevel.navMesh.AStar(ch.Position, ch.Activities.Activities[ch.Activities.Index].v3);
                    if (Engine.Singleton.CurrentLevel.navMesh.TriPath.Count > 1)
                    {
                        Engine.Singleton.CurrentLevel.navMesh.GetPortals();
                        ch.WalkPath = Engine.Singleton.CurrentLevel.navMesh.Funnel();

                        ch.FollowPathOrder       = true;
                        ch.Activities.InProgress = true;
                    }
                }
                else
                {
                    ch.Orientation = Quaternion.Slerp(
                        0.2f,
                        ch.Orientation,
                        Vector3.UNIT_Z.GetRotationTo(
                            (Op2D.XZ * (ch.WalkPath[0] - ch.Position)).NormalisedCopy),
                        true
                        );
                    ch.Velocity = ch.Orientation * Vector3.UNIT_Z * ch.Profile.WalkSpeed;
                    ch.AnimBlender.SetAnimSet("Walk");
                }
                return(false);
            }
                                ));

            // get sword

            DecTree.FirstSucc resetAnimGetSword = new DecTree.FirstSucc(
                new DecTree.Assert(ch => ch.AnimBlender.CurrentAnimSet == "GetSword"),
                new DecTree.Job(ch =>
            {
                ch.Velocity = Vector3.ZERO;
                ch.AnimBlender.ResetAnimSet("GetSword");
                ch.AnimBlender.SetAnimSet("GetSword");

                return(true);
            }));

            Func <Character, bool> animGetSword1 = (ch => ch.AnimBlender.AnimSetPhase("GetSword") > 0.5f);

            DecTree.FirstSucc getSwordWait1 = new DecTree.FirstSucc(
                new DecTree.Assert(animGetSword1),
                new DecTree.FirstFail(
                    new DecTree.Assert(ch => ch.GetSwordOrder),
                    new DecTree.Job(animGetSword1)
                    )
                );

            Func <Character, bool> animGetSword2 = (ch => ch.AnimBlender.AnimSetPhase("GetSword") > 0.99f);

            DecTree.FirstSucc getSwordWait2 = new DecTree.FirstSucc(
                new DecTree.Assert(animGetSword2),
                new DecTree.FirstFail(
                    new DecTree.Assert(ch => ch.GetSwordOrder),
                    new DecTree.Job(animGetSword2)
                    )
                );

            DecTree.FirstSucc getSword = new DecTree.FirstSucc(
                new DecTree.Job(ch =>
            {
                ItemSword sword = ch.Sword;
                ch.UnequipSword();
                ch.EquipSwordToSword(sword);

                return(true);
            }));

            GetSwordSeq = new DecTree.FirstFail(
                resetAnimGetSword,
                getSwordWait1,
                getSword,
                getSwordWait2
                );

            DecTree.Job cleanUpGetSword = new DecTree.Job(ch => { ch.GetSwordOrder = false; return(true); });

            DecTree.FirstFail getSwordNode = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.GetSwordOrder),
                new DecTree.FirstSucc(GetSwordSeq, cleanUpGetSword),
                cleanUpGetSword);


            // hide sword

            DecTree.FirstSucc resetAnimHideSword = new DecTree.FirstSucc(
                new DecTree.Assert(ch => ch.AnimBlender.CurrentAnimSet == "HideSword"),
                new DecTree.Job(ch =>
            {
                ch.Velocity = Vector3.ZERO;
                ch.AnimBlender.ResetAnimSet("HideSword");
                ch.AnimBlender.SetAnimSet("HideSword");
                return(true);
            }));

            Func <Character, bool> animHideSword1 = (ch => ch.AnimBlender.AnimSetPhase("HideSword") > 0.5f);

            DecTree.FirstSucc hideSwordWait1 = new DecTree.FirstSucc(
                new DecTree.Assert(animHideSword1),
                new DecTree.FirstFail(
                    new DecTree.Assert(ch => ch.HideSwordOrder),
                    new DecTree.Job(animHideSword1)
                    )
                );

            Func <Character, bool> animHideSword2 = (ch => ch.AnimBlender.AnimSetPhase("HideSword") > 0.99f);

            DecTree.FirstSucc hideSwordWait2 = new DecTree.FirstSucc(
                new DecTree.Assert(animHideSword2),
                new DecTree.FirstFail(
                    new DecTree.Assert(ch => ch.HideSwordOrder),
                    new DecTree.Job(animHideSword2)
                    )
                );

            DecTree.FirstSucc hideSword = new DecTree.FirstSucc(
                new DecTree.Job(ch =>
            {
                ItemSword sword = ch.Sword;
                ch.UnequipSword();
                ch.EquipSwordToLongswordSheath(sword);


                return(true);
            }));

            HideSwordSeq = new DecTree.FirstFail(
                resetAnimHideSword,
                hideSwordWait1,
                hideSword,
                hideSwordWait2
                );

            DecTree.Job cleanUpHideSword = new DecTree.Job(ch => { ch.HideSwordOrder = false; return(true); });

            DecTree.FirstFail hideSwordNode = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.HideSwordOrder),
                new DecTree.FirstSucc(HideSwordSeq, cleanUpHideSword),
                cleanUpHideSword);

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // go left
            DecTree.FirstFail go_left = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.MoveLeftOrder),
                new DecTree.Job(ch =>
            {
                Quaternion orient = Quaternion.IDENTITY;
                orient.FromAngleAxis(new Degree(90), Vector3.UNIT_Y);
                ch.Velocity = ch.Orientation * orient * Vector3.UNIT_Z * ch.Profile.WalkSpeed;
                ch.AnimBlender.SetAnimSet("Idle");                          // ### ANIMACJA CHODZENIA!

                return(true);
            }),
                TurnJob);

            DecTree.FirstFail go_right = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.MoveRightOrder),
                new DecTree.Job(ch =>
            {
                Quaternion orient = Quaternion.IDENTITY;
                orient.FromAngleAxis(new Degree(-90), Vector3.UNIT_Y);
                ch.Velocity = ch.Orientation * orient * Vector3.UNIT_Z * ch.Profile.WalkSpeed;
                ch.AnimBlender.SetAnimSet("Idle");                     // ### ANIMACJA CHODZENIA!

                return(true);
            }),
                TurnJob);

            DecTree.FirstSucc resetAnimAttack = new DecTree.FirstSucc(
                new DecTree.Assert(ch => ch.AnimBlender.CurrentAnimSet == "GetSword"),
                new DecTree.Job(ch =>
            {
                ch.AnimBlender.ResetAnimSet("GetSword");
                ch.AnimBlender.SetAnimSet("GetSword");
                Engine.Singleton.SoundManager.PlaySound("Other/haa.mp3");
                return(true);
            }));
            // #2
            Func <Character, bool> animAttack = (
                ch => ch.AnimBlender.AnimSetPhase("GetSword") > 0.99f
                );

            DecTree.FirstSucc AnimAttack = new DecTree.FirstSucc(
                new DecTree.Assert(animAttack),
                new DecTree.FirstFail(
                    new DecTree.Job(animAttack)
                    )
                );

            // #4

            AttackSeq = new DecTree.FirstFail(
                resetAnimAttack,
                AnimAttack);

            DecTree.Job cleanUpA = new DecTree.Job(ch => { ch.AttackOrder = false; Atakowanie(); return(true); });

            DecTree.FirstFail AttackNode = new DecTree.FirstFail(
                new DecTree.Assert(ch => ch.AttackOrder),
                new DecTree.FirstSucc(AttackSeq, cleanUpA),           // #1
                cleanUpA);



            Children.Add(getSwordNode);
            Children.Add(hideSwordNode);
            Children.Add(walkNodeBack);
            Children.Add(go_left);
            Children.Add(go_right);
            Children.Add(AttackNode);

            Children.Add(pickItemNode);
            Children.Add(followPathNode);
            Children.Add(walkNode);
            Children.Add(idleNode);
        }
コード例 #4
0
        public EnemyDecTree()
        {
            TurnJob = new DecTree.Enemies.e_Job(en =>
            {
                if (en.TurnDelta != 0)
                {
                    Quaternion rotation = Quaternion.IDENTITY;
                    rotation.FromAngleAxis(new Degree(en.TurnDelta), Vector3.UNIT_Y);
                    en.Orientation *= rotation;
                    en.TurnDelta    = 0;
                }
                return(true);
            });

            DecTree.Enemies.e_FirstFail walkNode = new DecTree.Enemies.e_FirstFail(
                new DecTree.Enemies.e_Assert(en => en.MoveOrder),
                new DecTree.Enemies.e_Job(en =>
            {
                en.Velocity = en.Orientation * Vector3.UNIT_Z * en.Profile.WalkSpeed;

                //en.Animation("IdleLegs").Enabled = false;
                //en.Animation("WalkLegs").Enabled = true;
                //en.Animation("WalkLegs").Loop = true;
                //en.Animation("WalkLegs").AddTime(1.0f / 90.0f);
                return(true);
            }),
                TurnJob);

            DecTree.Enemies.e_FirstFail idleNode = new DecTree.Enemies.e_FirstFail(
                new DecTree.Enemies.e_Job(en =>
            {
                en.TalkPerm      = true;
                en.InventoryPerm = true;
                en.Velocity      = Vector3.ZERO;



                //en.Animation("WalkLegs").Enabled = false;
                //en.Animation("IdleLegs").Enabled = true;
                //en.Animation("IdleLegs").Loop = true;
                return(true);
            }),
                TurnJob);

            DecTree.Enemies.e_FirstFail followPathNode = new DecTree.Enemies.e_FirstFail(
                new DecTree.Enemies.e_Assert(en => en.FollowPathOrder),
                new DecTree.Enemies.e_Job(en =>
            {
                if (Op2D.Dist(en.Position, en.WalkPath[0]) < 0.5f)
                {
                    en.WalkPath.RemoveAt(0);
                }
                if (en.WalkPath.Count == 0)
                {
                    en.FollowPathOrder = false;
                    return(true);
                }
                else
                {
                    en.Orientation = Quaternion.Slerp(
                        0.2f,
                        en.Orientation,
                        Vector3.UNIT_Z.GetRotationTo(
                            (Op2D.XZ * (en.WalkPath[0] - en.Position)).NormalisedCopy),
                        true
                        );
                    en.Velocity = en.Orientation * Vector3.UNIT_Z * en.Profile.WalkSpeed;


                    //en.Animation("IdleLegs").Enabled = false;

                    //en.Animation("WalkLegs").Enabled = true;
                    //en.Animation("WalkLegs").Loop = true;
                }
                return(false);
            }
                                          ));


            e_Children.Add(followPathNode);
            e_Children.Add(walkNode);
            e_Children.Add(idleNode);
        }
コード例 #5
0
ファイル: NavMesh.cs プロジェクト: janPierdolnikParda/RPG
 public void CalcArea()
 {
     Center = (U.A + U.B + V.A + V.B + W.A + W.B) / 6.0f;
     Area   = Op2D.TriArea(U.B - U.A, V.B - V.A);
 }
コード例 #6
0
ファイル: NavMesh.cs プロジェクト: janPierdolnikParda/RPG
        public List <Vector3> Funnel()
        {
            List <Vector3> path = new List <Vector3>();
            Vector3        right, left, current;
            int            rightId, leftId;

            right   = left = current = StartPt;
            rightId = leftId = 0;

            for (int i = 0; i < Portals.Count; i++)
            {
                if (left == current || Op2D.Cross(left - current, Portals[i].first - current) >= 0)
                {
                    if (Op2D.Cross(right - current, Portals[i].first - current) > 0)
                    {
                        path.Add(right);
                        current = left = right;
                        i       = leftId = rightId;
                        continue;
                    }
                    else
                    {
                        left   = Portals[i].first;
                        leftId = i;
                    }
                }

                if (right == current || Op2D.Cross(right - current, Portals[i].second - current) <= 0)
                {
                    if (Op2D.Cross(left - current, Portals[i].second - current) < 0)
                    {
                        path.Add(left);
                        current = right = left;
                        i       = rightId = leftId;
                        continue;
                    }
                    else
                    {
                        right   = Portals[i].second;
                        rightId = i;
                    }
                }
            }

            path.Add(EndPt);



            Engine.Singleton.SceneManager.DestroyManualObject("line");

            // destroy the SceneNode (or keep it to add other manual objects)
            Engine.Singleton.SceneManager.DestroySceneNode("line_node");

            ManualObject manOb = Engine.Singleton.SceneManager.CreateManualObject("line");

            manOb.Begin("line_material", RenderOperation.OperationTypes.OT_LINE_LIST);

            for (int i = 1; i < path.Count; i++)
            {
                Vector3 adam = path[i - 1];
                adam.y = 0.4f;
                manOb.Position(adam);

                Vector3 adam1 = path[i];
                adam1.y = 0.4f;
                manOb.Position(adam1);
            }

            manOb.End();
            SceneNode moNode = Engine.Singleton.SceneManager.RootSceneNode.CreateChildSceneNode("line_node");

            moNode.SetPosition(0, 0, 0);

            moNode.AttachObject(manOb);

            return(path);
        }