示例#1
0
        public void Straight()
        {
            ep.Add(-20);
            if (ep.IsZero)
            {
                return;
            }

            Point3D targetPosition = this.position.Copy();

            SkillManager.showSkill(Skill.straight, this);

            for (int i = 0; i < 3; ++i)
            {
                targetPosition.MoveFor(this.vector, 1);
                Grid targetGrid = World.GetAt(targetPosition);

                if (targetGrid == null)
                {
                    return;
                }
                if (targetGrid.Obj == null)
                {
                    continue;
                }

                if (targetGrid.Obj is Attackable)
                {
                    Attackable target = (Attackable)targetGrid.Obj;
                    target.BeAttack(this);
                }
            }
        }
示例#2
0
        // return null   : 創造失敗,能量不夠、格子不存在、格子已經有東西了.
        // return Animal : 創造的生物.
        private Animal CreateAnimalAt(Point3D position)
        {
            if (energy.Value < ReserveEnergy)
            {
                return(null);
            }

            Grid grid = World.GetAt(position);

            if (grid == null)
            {
                return(null);
            }

            var animal = new Animal(position, this);

            if (grid.InsertObj(animal))
            {
                energy.Add(-costOfAnimal);

                // 生成器等級越高,生物越強.
                int extra = costOfAnimal / 10 * Level;
                energy.Add(-extra);
                animal.Strong(Level * 10, Level, Level);

                SkillManager.showSkill(Skill.create, position);
                GlobalAsset.animals.Add((Animal)grid.Obj);
                return((Animal)grid.Obj);
            }

            return(null);
        }
示例#3
0
        public void Attack()
        {
            // 攻擊不耗魔.

            Point3D targetPosition = this.position.Copy();

            SkillManager.showSkill(Skill.attack, this);

            targetPosition.MoveFor(this.vector, 1);
            Grid targetGrid = World.GetAt(targetPosition);

            if (targetGrid == null)
            {
                return;
            }
            if (targetGrid.Obj == null)
            {
                return;
            }

            if (targetGrid.Obj is Attackable)
            {
                Attackable enemy = (Attackable)(targetGrid.Obj);
                enemy.BeAttack(this);
            }
        }
示例#4
0
        public override void Destroy()
        {
            base.Destroy();
            Grid grid = World.GetAt(position);

            if (hungry.BarRate != 0)
            {
                grid.InsertObj(new Food(this.position, (int)((hp.Max + ep.Max) * hungry.BarRate)));
            }
        }
示例#5
0
        public void Horizon()
        {
            ep.Add(-20);
            if (ep.IsZero)
            {
                return;
            }

            Point2D  targetPosition = this.posit.Copy();
            Vector2D targetVector   = this.Vect;

            if (GlobalAsset.player != null && this.Plain.Dimention == GlobalAsset.player.Plain.Dimention)
            {
                SkillManager.showSkill(Skill.horizon, this);
            }
            else
            {
                SkillManager.showSkill(Skill.attack, this);
            }

            targetPosition.MoveFor(targetVector, 1);
            targetVector = VectorConvert.Rotate(targetVector);
            targetPosition.MoveFor(targetVector, 2);
            targetVector = VectorConvert.Invert(targetVector);

            for (int i = 0; i < 3; ++i)
            {
                targetPosition.MoveFor(targetVector, 1);
                Grid targetGrid = World.GetAt(targetPosition.Binded);

                if (targetGrid == null)
                {
                    continue;
                }
                if (targetGrid.Obj == null)
                {
                    continue;
                }

                if (targetGrid.Obj is Attackable)
                {
                    Attackable target = (Attackable)targetGrid.Obj;
                    target.BeAttack(this);
                }
            }
        }
示例#6
0
        public void createAnimals(int number)
        {
            Iterator iter = new Iterator(this.positionOnPlain, 1);

            do
            {
                Point2D point = iter.Iter;
                Grid    grid  = World.GetAt(point.Binded);

                if (grid == null || !grid.IsEmpty())
                {
                    continue;
                }

                if (number-- != 0)
                {
                    CreateAnimalAt(point.Binded.Copy());
                }
            } while (iter.MoveToNext());
        }
示例#7
0
        private void Move()
        {
            Point3D temp = this.position.Copy();

            temp.MoveFor(vector, 1);
            Grid targetGrid = World.GetAt(temp);

            if (targetGrid == null)
            {
                return;
            }

            if (targetGrid.Obj != null)
            {
                if (targetGrid.Obj is Food)
                {
                    EatFood((Food)targetGrid.Obj);
                }
                else
                {
                    return;
                }
            }

            World.Swap(position, temp);
            RegisterEvent(ObjEvent.posit);

            // 當照著路線走時,會把走過的標記移除.
            if (route != null && route.Count > 0)
            {
                if (route.Peek() == this.Vect) // 照著路線走.
                {
                    route.Pop();
                }
                else // 偏離路線,原本的路線已經沒用了.
                {
                    route = null;
                }
            }
        }
示例#8
0
        // 創造營養價值 nutrientBase*20 的食物.
        // false : 創造失敗,能量不夠、格子不存在、格子已經有東西了.
        private bool CreateFoodAt(Point3D position, int nutrientBase)
        {
            if (energy.Value < ReserveEnergy)
            {
                return(false);
            }

            Grid grid = World.GetAt(position);

            if (grid == null)
            {
                return(false);
            }

            if (grid.InsertObj(new Food(position, nutrientBase * 20)))
            {
                energy.Add(-nutrientBase);
                return(true);
            }

            return(false);
        }
示例#9
0
        // 漫無目的亂走.
        private void Wander()
        {
            Vector2D targetVector = RandomVector(10);

            for (int i = 0; i < 4; ++i)
            {
                Point2D target = this.posit.Copy();
                target.MoveFor(targetVector, 1);
                Grid targetGrid = World.GetAt(target.Binded);

                if (targetGrid != null && (targetGrid.IsEmpty() || targetGrid.Obj is Food))
                {
                    break;
                }
                else
                {
                    targetVector = VectorConvert.Rotate(targetVector);
                }
            }


            moveCommamd = Convert(targetVector);
        }
示例#10
0
        public void Build()
        {
            ep.Add(-10);
            if (ep.IsZero)
            {
                return;
            }

            Point3D targetPosition = this.position.Copy();

            targetPosition.MoveFor(this.vector, 1);
            Grid targetGrid = World.GetAt(targetPosition);

            if (targetGrid == null)
            {
                return;
            }

            if (targetGrid.IsEmpty())
            {
                targetGrid.InsertObj(new Wall(targetPosition, Power * 5));
            }
        }
示例#11
0
        public void Together()
        {
            Iterator iter = new Iterator(this.PositOnScene, 3);

            do
            {
                Point2D point = iter.Iter;
                Grid    grid  = World.GetAt(point.Binded);

                if (grid == null || grid.Obj == null)
                {
                    continue;
                }

                if (grid.Obj is Animal)
                {
                    Animal animal = (Animal)grid.Obj;
                    if (animal.Color.Equals(this.Color))
                    {
                        animal.FollowFriend(this);
                    }
                }
            } while (iter.MoveToNext());
        }
示例#12
0
        // 招集附近的流浪夥伴一起蓋新家.
        private void FindNewHome()
        {
            if (this.Hometown != null)
            {
                return;
            }

            Point3D position = this.position.Copy();

            position.MoveFor(this.vector, 1);

            Grid targetGrid = World.GetAt(position);

            if (targetGrid == null || targetGrid.Obj != null)
            {
                return;
            }


            Point2D positOnPlain         = new Point2D(position, Dimention.Z);
            int     stoneCount           = 0;
            int     wallCount            = 0;
            int     animalCount          = 0;
            bool    sameColorOfAllAnimal = true;

            Iterator iter = new Iterator(positOnPlain, 2);

            do
            {
                Point2D point = iter.Iter;
                Grid    grid  = World.GetAt(point.Binded);

                if (grid == null || grid.Obj == null)
                {
                    continue;
                }

                else if (grid.Obj is Stone)
                {
                    ++stoneCount;
                }

                else if (grid.Obj is Wall)
                {
                    ++wallCount;
                }

                else if (grid.Obj is Animal && grid.Obj != this)
                {
                    Animal animal = (Animal)grid.Obj;

                    if (animal.Color.Equals(this.Color))
                    {
                        if (animal.Hometown == null)
                        {
                            ++animalCount;
                        }
                    }
                    else
                    {
                        sameColorOfAllAnimal = false;
                        break;
                    }
                }

                else if (grid.Obj is Creater)
                {
                    Creater creater = (Creater)grid.Obj;
                    this.ChangeHomeTown(creater);
                    return;
                }
            } while (iter.MoveToNext());

            if (sameColorOfAllAnimal && animalCount > 1)
            {
                Creater newHome = new Creater(position, this.Color);
                targetGrid.InsertObj(newHome);
                this.Hometown = newHome;
                GlobalAsset.creaters.Add(newHome);
            }
        }
示例#13
0
        // 偵測周圍 7*7 內有什麼.
        // 並選擇採取什麼模式.
        private void Survey()
        {
            Iterator iter   = new Iterator(this.posit, surveyExtra);
            Animal   animal = null;

            do
            {
                Point2D point = iter.Iter;
                Grid    grid  = World.GetAt(point.Binded);

                if (grid == null || grid.Obj == null)
                {
                    continue;
                }

                if (grid.Obj is Food && this.hungry.BarRate < 0.5f)
                {
                    FeedOn((Food)grid.Obj);
                    return;
                }

                if (grid.Obj is Animal)
                {
                    if (((Animal)(grid.Obj)).Color.Equals(this.Color))
                    {
                        animal = (Animal)grid.Obj;
                    }

                    else if (ep.BarRate > 0.3f)
                    {
                        Battle((Animal)grid.Obj);
                        return;
                    }
                }

                if (grid.Obj is Creater)
                {
                    if (this.Hometown == null && grid.Obj.GetColor().Equals(this.Color))
                    {
                        this.ChangeHomeTown((Creater)grid.Obj);
                    }
                }
            } while (iter.MoveToNext());


            if (friend != null)
            {
                Follow();
            }
            else if (Hometown != null)
            {
                Patrol(Hometown);

                if (Hometown.IsDead)
                {
                    Hometown = null;
                }
            }
            else
            {
                Wander();
            }
        }
示例#14
0
        // 每個 clock 執行的事.
        // 若有創造村民,則回傳該村民.
        // 根據機率創造村民、食物.
        // 若能量滿了會升級,能量空了會死亡.
        public Animal Clock()
        {
            if (IsDead)
            {
                return(null);
            }
            reduceForClock();


            Iterator iter = new Iterator(this.positionOnPlain, this.Range);

            int createAnimalIndex = -1;

            if (UnityEngine.Random.value < rateOfCreateAnimal)
            {
                createAnimalIndex = UnityEngine.Random.Range(0, iter.Size);
            }

            int createFoodIndex = -1;

            if (UnityEngine.Random.value < rateOfCreateFood)
            {
                createFoodIndex = UnityEngine.Random.Range(0, iter.Size);
            }

            Animal animal = null;

            animalsCount = 0; // count by UpdateByObj().

            do
            {
                Point2D point = iter.Iter;
                Grid    grid  = World.GetAt(point.Binded);

                if (grid != null)
                {
                    UpdateByObj(grid.Obj);
                }

                if (createAnimalIndex-- == 0)
                {
                    animal = CreateAnimalAt(point.Binded.Copy());
                }

                if (createFoodIndex-- == 0)
                {
                    CreateFoodAt(point.Binded.Copy(), 1);
                }
            } while (iter.MoveToNext());

            averageOfAnimals = (averageOfAnimals * clocks + animalsCount) / (clocks + 1); // 應該是float.

            ++clocks;
            if (clocks > 100)
            {
                clocks = 0; // 避免數字太大溢位.
            }
            if (energy.IsFull)
            {
                LevelUp();
            }
            else if (IsDead)
            {
                Destroy();
            }

            return(animal);
        }
示例#15
0
 // 將他從 World 上移除,並登記 Destroy 事件,提醒 MapManager 將它消除.
 public virtual void Destroy()
 {
     this.RegisterEvent(ObjEvent.Destroy);
     World.GetAt(this.position).RemoveObj();
 }