private void SpawnRegrow()
        {
            CraftData data = craftable.GetData();

            if (data != null && !string.IsNullOrEmpty(unique_id.unique_id) && !PlayerData.Get().HasWorldRegrowth(unique_id.unique_id))
            {
                int nb = Craftable.CountObjectInRadius(data, transform.position, range);
                if (nb < max)
                {
                    //Find position
                    Vector3 position = FindPosition();
                    if (IsPositionValid(position))
                    {
                        Quaternion rotation = transform.rotation;
                        float      scale    = 1f;
                        if (random_rotation)
                        {
                            rotation = Quaternion.Euler(rotation.eulerAngles.x, Random.Range(0f, 360f), 0f);
                        }
                        if (random_scale)
                        {
                            scale = Random.Range(0.75f, 1.25f);
                        }

                        CreateRegrowthData(unique_id.unique_id, data, SceneNav.GetCurrentScene(), position, rotation, scale, duration, probability);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void GrowPlant(int grow_stage)
        {
            if (data != null && growth_stage >= 0 && growth_stage < nb_stages)
            {
                SowedPlantData sdata = PlayerData.Get().GetSowedPlant(GetUID());
                if (sdata == null)
                {
                    //Remove this plant and create a new one (this one probably was already in the scene)
                    if (!was_spawned)
                    {
                        PlayerData.Get().RemoveObject(GetUID()); //Remove Unique id
                    }
                    sdata = PlayerData.Get().AddPlant(data.id, SceneNav.GetCurrentScene(), transform.position, transform.rotation, grow_stage);
                }
                else
                {
                    //Grow current plant from data
                    PlayerData.Get().GrowPlant(GetUID(), grow_stage);
                }

                growth_progress = 0f;
                PlayerData.Get().SetCustomValue(GetProgressUID(), 0);
                plant_list.Remove(this); //Remove from list so spawn works!

                Spawn(sdata.uid);
                Destroy(gameObject);
            }
        }
 private void OnBuild()
 {
     if (data != null)
     {
         TrainedCharacterData cdata = PlayerData.Get().AddCharacter(data.id, SceneNav.GetCurrentScene(), transform.position, transform.rotation);
         unique_id.unique_id = cdata.uid;
     }
 }
Exemplo n.º 4
0
 private void OnBuild()
 {
     if (data != null)
     {
         SowedPlantData splant = PlayerData.Get().AddPlant(data.id, SceneNav.GetCurrentScene(), transform.position, transform.rotation, growth_stage);
         unique_id.unique_id = splant.uid;
     }
 }
        //Create a totally new one that will be added to save file
        public static Character Create(CharacterData data, Vector3 pos)
        {
            Quaternion           rot   = Quaternion.Euler(0f, 180f, 0f);
            TrainedCharacterData ditem = PlayerData.Get().AddCharacter(data.id, SceneNav.GetCurrentScene(), pos, rot);
            GameObject           build = Instantiate(data.character_prefab, pos, rot);
            Character            unit  = build.GetComponent <Character>();

            unit.data                = data;
            unit.was_spawned         = true;
            unit.unique_id.unique_id = ditem.uid;
            return(unit);
        }
Exemplo n.º 6
0
        //Create a new item that existed in inventory (such as when dropping it)
        public static Item Create(ItemData data, Vector3 pos, int quantity, float durability, string uid)
        {
            DroppedItemData ditem = PlayerData.Get().AddDroppedItem(data.id, SceneNav.GetCurrentScene(), pos, quantity, durability, uid);
            GameObject      obj   = Instantiate(data.item_prefab, pos, data.item_prefab.transform.rotation);
            Item            item  = obj.GetComponent <Item>();

            item.data                = data;
            item.was_spawned         = true;
            item.unique_id.unique_id = ditem.uid;
            item.quantity            = quantity;
            return(item);
        }
        //Spawn the prefab from existin regrowth data, after its timer reaches the duration
        public static GameObject SpawnRegrowth(WorldRegrowthData data)
        {
            CraftData craftable = CraftData.Get(data.data_id);

            if (craftable != null && data.scene == SceneNav.GetCurrentScene())
            {
                if (Random.value < data.probability)
                {
                    GameObject nobj = Craftable.Create(craftable, data.pos);
                    nobj.transform.rotation   = data.rot;
                    nobj.transform.localScale = nobj.transform.localScale * data.scale;
                    return(nobj);
                }
            }
            return(null);
        }
        void Start()
        {
            if (!was_spawned && PlayerData.Get().IsObjectRemoved(GetUID()))
            {
                Destroy(gameObject);
                return;
            }

            //Set current position
            SceneObjectData sobj = PlayerData.Get().GetSceneObject(GetUID());

            if (sobj != null && sobj.scene == SceneNav.GetCurrentScene())
            {
                transform.position = sobj.pos;
                transform.rotation = sobj.rot;
            }

            DetectGrounded(); //Check grounded
        }
        //Spawn an existing one in the save file (such as after loading)
        public static Character Spawn(string uid, Transform parent = null)
        {
            TrainedCharacterData tcdata = PlayerData.Get().GetCharacter(uid);

            if (tcdata != null && tcdata.scene == SceneNav.GetCurrentScene())
            {
                CharacterData cdata = CharacterData.Get(tcdata.character_id);
                if (cdata != null)
                {
                    GameObject cobj = Instantiate(cdata.character_prefab, tcdata.pos, tcdata.rot);
                    cobj.transform.parent = parent;

                    Character character = cobj.GetComponent <Character>();
                    character.data                = cdata;
                    character.was_spawned         = true;
                    character.unique_id.unique_id = uid;
                    return(character);
                }
            }
            return(null);
        }
Exemplo n.º 10
0
        //Spawn an existing one in the save file (such as after loading)
        public static Construction Spawn(string uid, Transform parent = null)
        {
            BuiltConstructionData bdata = PlayerData.Get().GetConstructed(uid);

            if (bdata != null && bdata.scene == SceneNav.GetCurrentScene())
            {
                ConstructionData cdata = ConstructionData.Get(bdata.construction_id);
                if (cdata != null)
                {
                    GameObject build = Instantiate(cdata.construction_prefab, bdata.pos, bdata.rot);
                    build.transform.parent = parent;

                    Construction construct = build.GetComponent <Construction>();
                    construct.data                = cdata;
                    construct.was_spawned         = true;
                    construct.unique_id.unique_id = uid;
                    return(construct);
                }
            }
            return(null);
        }
Exemplo n.º 11
0
        //Spawn an existing one in the save file (such as after loading)
        public static Item Spawn(string uid, Transform parent = null)
        {
            DroppedItemData ddata = PlayerData.Get().GetDroppedItem(uid);

            if (ddata != null && ddata.scene == SceneNav.GetCurrentScene())
            {
                ItemData idata = ItemData.Get(ddata.item_id);
                if (idata != null)
                {
                    GameObject build = Instantiate(idata.item_prefab, ddata.pos, idata.item_prefab.transform.rotation);
                    build.transform.parent = parent;

                    Item item = build.GetComponent <Item>();
                    item.data                = idata;
                    item.was_spawned         = true;
                    item.unique_id.unique_id = uid;
                    item.quantity            = ddata.quantity;
                    return(item);
                }
            }
            return(null);
        }
Exemplo n.º 12
0
        //Spawn an existing one in the save file (such as after loading)
        public static Plant Spawn(string uid, Transform parent = null)
        {
            SowedPlantData sdata = PlayerData.Get().GetSowedPlant(uid);

            if (sdata != null && sdata.scene == SceneNav.GetCurrentScene())
            {
                PlantData pdata = PlantData.Get(sdata.plant_id);
                if (pdata != null)
                {
                    GameObject prefab = pdata.GetStagePrefab(sdata.growth_stage);
                    GameObject build  = Instantiate(prefab, sdata.pos, sdata.rot);
                    build.transform.parent = parent;

                    Plant plant = build.GetComponent <Plant>();
                    plant.data                = pdata;
                    plant.growth_stage        = sdata.growth_stage;
                    plant.was_spawned         = true;
                    plant.unique_id.unique_id = uid;
                    return(plant);
                }
            }
            return(null);
        }
        void Update()
        {
            if (TheGame.Get().IsPaused())
            {
                return;
            }

            if (IsDead())
            {
                return;
            }

            if (buildable && buildable.IsBuilding())
            {
                return;
            }

            attack_timer        += Time.deltaTime;
            move_timer          += Time.deltaTime;
            navmesh_timer       += Time.deltaTime;
            ground_refesh_timer += Time.deltaTime;

            //Detect obstacles and ground
            if (ground_refesh_timer > ground_refresh_rate)
            {
                ground_refesh_timer = Random.Range(-0.02f, 0.02f);
                DetectGrounded();
                DetectFronted();
            }

            //Save position
            PlayerData.Get().SetCharacterPosition(GetUID(), SceneNav.GetCurrentScene(), transform.position, transform.rotation);

            //Stop moving
            if (is_moving && !HasTarget() && HasReachedMoveTarget(moving_threshold * 2f))
            {
                Stop();
            }

            //Stop attacking
            if (is_attacking && !HasAttackTarget())
            {
                Stop();
            }

            //Following
            if (target != null)
            {
                Vector3 targ_dir = (target.transform.position - transform.position);
                targ_dir.y = 0f;

                if (is_moving && !is_attacking)
                {
                    if (is_escaping)
                    {
                        Vector3 targ_pos = transform.position - targ_dir.normalized * 4f;
                        move_target = targ_pos;
                    }
                    else
                    {
                        move_target = target.transform.position;

                        //Stop following
                        if ((attack_target != null || attack_player != null) && targ_dir.magnitude < GetAttackTargetHitRange() * 0.8f)
                        {
                            move_target = transform.position;
                            is_moving   = false;
                        }

                        //Stop following
                        if (attack_target == null && attack_player == null && HasReachedMoveTarget(follow_distance))
                        {
                            move_target = transform.position;
                            is_moving   = false;
                        }
                    }
                }

                //Start following again
                if (!is_moving && !is_attacking)
                {
                    if (targ_dir.magnitude > GetAttackTargetHitRange())
                    {
                        is_moving = true;
                    }
                }
            }

            //Attacking
            if (HasAttackTarget() && attack_enabled)
            {
                if (!is_attacking)
                {
                    if (attack_timer > attack_cooldown)
                    {
                        Vector3 targ_dir = (target.transform.position - transform.position);
                        targ_dir.y = 0f;

                        if (targ_dir.magnitude < GetAttackTargetHitRange())
                        {
                            is_attacking = true;
                            is_moving    = false;
                            attack_hit   = false;
                            attack_timer = 0f;

                            if (onAttack != null)
                            {
                                onAttack.Invoke();
                            }
                        }
                    }
                }

                if (is_attacking)
                {
                    move_target       = transform.position;
                    move_target_avoid = transform.position;
                    FaceTorward(target.transform.position);

                    if (!attack_hit && attack_timer > attack_windup)
                    {
                        float range = (target.transform.position - transform.position).magnitude;
                        if (range < GetAttackTargetHitRange())
                        {
                            if (attack_target != null)
                            {
                                attack_target.TakeDamage(this, attack_damage);
                            }
                            if (attack_player != null)
                            {
                                attack_player.Combat.TakeDamage(attack_damage);
                            }
                        }
                        attack_hit = true;

                        if (selectable.IsNearCamera(20f))
                        {
                            TheAudio.Get().PlaySFX("character", attack_audio);
                        }
                    }

                    if (attack_timer > attack_duration)
                    {
                        is_attacking = false;
                        attack_timer = 0f;
                        is_moving    = true;

                        if (attack_target != null)
                        {
                            Attack(attack_target);
                        }
                        if (attack_player != null)
                        {
                            Attack(attack_player);
                        }
                    }
                }

                if (attack_target != null && attack_target.IsDead())
                {
                    Stop();
                }

                if (attack_player != null && attack_player.IsDead())
                {
                    Stop();
                }
            }

            //Add an offset to escape path when fronted
            if (avoid_obstacles && !direct_move)
            {
                if (is_fronted_left && !is_fronted_right)
                {
                    avoid_side = 1f;
                }
                if (is_fronted_right && !is_fronted_left)
                {
                    avoid_side = -1f;
                }

                //When fronted on all sides, use target to influence which side to go
                if (is_fronted_center && is_fronted_left && is_fronted_right && target)
                {
                    Vector3 dir = target.transform.position - transform.position;
                    dir = dir * (is_escaping ? -1f : 1f);
                    float dot = Vector3.Dot(dir.normalized, transform.right);
                    if (Mathf.Abs(dot) > 0.5f)
                    {
                        avoid_side = Mathf.Sign(dot);
                    }
                }

                float angle       = avoid_side * 90f;
                float far_val     = is_fronted ? 1f - (front_dist / destruct.hit_range) : Mathf.Abs(angle) / 90f; //1f = close, 0f = far
                float angle_speed = far_val * 150f + 50f;
                avoid_angle = Mathf.MoveTowards(avoid_angle, is_fronted ? angle : 0f, angle_speed * Time.deltaTime);
            }
        }
Exemplo n.º 14
0
 private void OnBuild()
 {
     if (data != null)
     {
         BuiltConstructionData cdata = PlayerData.Get().AddConstruction(data.id, SceneNav.GetCurrentScene(), transform.position, transform.rotation, data.durability);
         unique_id.unique_id = cdata.uid;
     }
 }