Пример #1
0
        /// <summary>
        /// Move ghost with successful raycastHit onto m_PlacementAreaMask
        /// </summary>
        protected virtual void MoveGhostWithRaycastHit(RaycastHit raycast)
        {
            // We successfully hit one of our placement areas
            // Try and get a placement area on the object we hit
            m_CurrentArea = raycast.collider.GetComponent <IPlacementArea>();

            if (m_CurrentArea == null)
            {
                Debug.LogError("There is not an IPlacementArea attached to " +
                               "the collider found on the m_PlacementAreaMask");
                return;
            }

            m_GridPosition = m_CurrentArea.WorldToGrid(
                raycast.point, m_CurrentTower.controller.dimensions);

            TowerFitStatus fits = m_CurrentArea.Fits(
                m_GridPosition, m_CurrentTower.controller.dimensions);

            m_CurrentTower.Show();
            m_GhostPlacementPossible = fits == TowerFitStatus.Fits && IsValidPurchase();

            m_CurrentTower.Move(
                m_CurrentArea.GridToWorld(m_GridPosition, m_CurrentTower.controller.dimensions),
                m_CurrentArea.transform.rotation,
                m_GhostPlacementPossible);
        }
        public void CreateTower(TowerData towerData, IPlacementArea placementArea, IntVector2 placeGrid, Vector3 position, Quaternion rotation)
        {
            if (towerData == null)
            {
                return;
            }

            TowerLevelData towerLevelData = towerData.GetTowerLevelData(0);

            if (dataPlayer.Energy < towerLevelData.BuildEnergy)
            {
                return;
            }

            dataPlayer.AddEnergy(-towerLevelData.BuildEnergy);

            Tower tower = dataTower.CreateTower(towerData.Id);

            if (tower == null)
            {
                Log.Error("Create tower fail,Tower data id is '{0}'.", towerData.Id);
                return;
            }

            entityLoader.ShowEntity(towerData.EntityId, TypeUtility.GetEntityType(tower.Type),
                                    (entity) =>
            {
                EntityTowerBase entityTowerBase = entity.Logic as EntityTowerBase;
                dicTowerInfo.Add(tower.SerialId, TowerInfo.Create(tower, entityTowerBase, placementArea, placeGrid));
            }
                                    , EntityDataTower.Create(tower, position, rotation));

            HidePreviewTower();
        }
Пример #3
0
    protected override GameObject Create()
    {
        IPlacementArea targetArea  = (IPlacementArea)args[0];
        IntVector2     destination = (IntVector2)args[1];


        long       id    = currentTargetLevelData.monster.Id;
        GameObject agent = Instantiate(Resources.Load <GameObject>("Prefab/Monster/" + id), transform);

        Animator          animator          = agent.GetComponent <Animator>();
        CharacterAnimator characterAnimator = GetComponentInParent <CharacterAnimator>();
        AIBehaviors       ai = GetComponent <AIBehaviors>();
        AIAnimationStates animationStates = GetComponentInParent <AIAnimationStates>();

        characterAnimator.anim = animator;


        PatrolState patrolState = ai.GetState <PatrolState>();

        Transform[] transforms = new Transform[2];
        transforms[0] = targetArea.transform;
        transforms[1] = targetArea.transform;
        patrolState.SetPatrolPoints(transforms);
        patrolState.GetTrigger <WithinDistanceTrigger>().center = transform;
        //IdleState ldleState = ai.GetState<IdleState>();
        //ldleState.currentNode = targetArea.transform;
        //ldleState.GetTrigger<WithinDistanceTrigger>().center = targetArea.transform;

        AttackState attackState = ai.GetState <AttackState>();

        attackState.GetTrigger <BeyondDistanceTrigger>().center = targetArea.transform;

        //AI
        //BaseState baseState = ComponentHelper.AddComponentByName(ai.transform.Find("States").gameObject, "GotHitState") as BaseState;
        //baseState.name = "GotHitState";
        //GotHitState gotHitState = baseState as GotHitState;
        //gotHitState.hitStateDuration = 0;
        //gotHitState.returnToPreviousState = true;
        //gotHitState.animationStates[0] = animationStates.GetStateWithName("Hit");
        //ai.AddSubTrigger(gotHitState);

        //初始化
        UnityEngine.Object alignment = Resources.Load("Data/Alignment/TowerAlignment");

        if (this.configuration.alignment == null)
        {
            this.configuration.alignment = new SerializableIAlignmentProvider();
            this.configuration.alignment.unityObjectReference = alignment;
        }
        else
        {
            this.configuration.alignment.unityObjectReference = alignment;
        }

        ai.Initialize();
        //放置
        UpdateTargetPos(targetArea, destination);
        return(agent);
    }
        protected override void OnHide(bool isShutdown, object userData)
        {
            base.OnHide(isShutdown, userData);

            currentArea            = null;
            m_GridPosition         = IntVector2.zero;
            entityDataTowerPreview = null;
        }
Пример #5
0
 public virtual void Initialize(IPlacementArea targetArea, IntVector2 destination)
 {
     UpdateTargetPos(targetArea, destination);
     SetLevel(0);
     //if (TargetDefense.Level.BattleField.instanceExists)
     //{
     //    TargetDefense.Level.BattleField.instance.levelStateChanged += OnLevelStateChanged;
     //}
 }
            public static TowerInfo Create(Tower tower, EntityTowerBase entityTower, IPlacementArea placementArea, IntVector2 placeGrid)
            {
                TowerInfo towerInfo = ReferencePool.Acquire <TowerInfo>();

                towerInfo.Tower         = tower;
                towerInfo.EntityTower   = entityTower;
                towerInfo.PlacementArea = placementArea;
                towerInfo.PlaceGrid     = placeGrid;
                return(towerInfo);
            }
        public static BuildTowerEventArgs Create(TowerData towerData, IPlacementArea placementArea, IntVector2 placeGrid, Vector3 position, Quaternion rotation, object userData = null)
        {
            BuildTowerEventArgs buildTowerEventArgs = ReferencePool.Acquire <BuildTowerEventArgs>();

            buildTowerEventArgs.TowerData     = towerData;
            buildTowerEventArgs.PlacementArea = placementArea;
            buildTowerEventArgs.PlaceGrid     = placeGrid;
            buildTowerEventArgs.Position      = position;
            buildTowerEventArgs.Rotation      = rotation;
            return(buildTowerEventArgs);
        }
Пример #8
0
    //给塔用的
    public virtual void UpdateTargetPos(IPlacementArea targetArea = null, IntVector2 destination = default(IntVector2))
    {
        if (placementArea != null)
        {
            placementArea.Clear(gridPosition, dimensions);
        }
        placementArea = targetArea;
        gridPosition  = destination;

        if (targetArea != null)
        {
            transform.position = placementArea.GridToWorld(destination, dimensions);
            transform.rotation = placementArea.transform.rotation;
            targetArea.Occupy(destination, dimensions);
            targetArea.SetController(transform);
        }
    }
        private void MoveGhostWithRaycastHit(RaycastHit raycast)
        {
            currentArea = raycast.collider.GetComponent <IPlacementArea>();

            if (currentArea == null)
            {
                Log.Error("There is not an IPlacementArea attached to the collider found on the m_PlacementAreaMask");
                return;
            }
            m_GridPosition = currentArea.WorldToGrid(raycast.point, entityDataTowerPreview.TowerData.Dimensions);
            TowerFitStatus fits = currentArea.Fits(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions);

            SetVisiable(true);
            canPlace = fits == TowerFitStatus.Fits;
            Move(currentArea.GridToWorld(m_GridPosition, entityDataTowerPreview.TowerData.Dimensions),
                 currentArea.transform.rotation,
                 canPlace);
        }
Пример #10
0
 public void FixedUpdate()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Ray        camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(camRay, out hit, 1000f, hitLayers))
         {
             //Check if hitting grid
             IPlacementArea placement = hit.collider.GetComponent <IPlacementArea>();
             if (placement != null)
             {
                 //Snap position tower to Grid element
                 transform.position = placement.Snap(hit.point, new IntVector2(1, 1));
             }
         }
     }
 }
Пример #11
0
        /// <summary>
        /// Provide the tower with data to initialize with
        /// </summary>
        /// <param name="targetArea">The placement area configuration</param>
        /// <param name="destination">The destination position</param>
        public virtual void Initialize(IPlacementArea targetArea, IntVector2 destination)
        {
            placementArea = targetArea;
            gridPosition  = destination;

            if (targetArea != null)
            {
                transform.position = placementArea.GridToWorld(destination, dimensions);
                transform.rotation = placementArea.transform.rotation;
                targetArea.Occupy(destination, dimensions);
            }

            SetLevel(0);
            if (LevelManager.instanceExists)
            {
                LevelManager.instance.levelStateChanged += OnLevelStateChanged;
            }
        }
Пример #12
0
        private void FixedUpdate()
        {
            //Perform raycast
            Ray        camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(camRay, out hit, 1000f, hitLayers))
            {
                //check if the grid is hit
                IPlacementArea placement = hit.collider.GetComponent <IPlacementArea>();
                //get grid point
                if (placement != null)
                {
                    //snap position of tower to the grid element
                    transform.position = placement.Snap(hit.point, new IntVector2(1, 1));
                }
            }
        }
Пример #13
0
        public void ImplementMitigation(IPlacementArea area, IntVector2 gridPosition, IntVector2 sizeOffset)
        {
            if (currentCourseOfAction != null)
            {
                // Withdraw cost
                PlayerStats.I.Worth -= currentCourseOfAction.GetValue();

                // Occupy area in grid
                area.Occupy(gridPosition, sizeOffset, PlacementTileState.Filled);

                // Initialize the mitigation
                var mitigation = UnityHelper
                                 .Instantiate(CourseOfActionPrefab, area.GridToWorld(gridPosition, sizeOffset))
                                 .GetComponent <MitigationBehaviour>();
                mitigation.Initialize(area, gridPosition, sizeOffset, currentCourseOfAction);

                ExitBuildMode();
            }
        }
Пример #14
0
        /// <summary>
        /// Move ghost with the given ray
        /// </summary>
        protected virtual void MoveGhostOntoWorld(Ray ray, bool hideWhenInvalid)
        {
            m_CurrentArea = null;

            if (!hideWhenInvalid)
            {
                RaycastHit hit;
                // check against all layers that the ghost can be on
                Physics.SphereCast(ray, sphereCastRadius, out hit, float.MaxValue, ghostWorldPlacementMask);
                if (hit.collider == null)
                {
                    return;
                }
                m_CurrentTower.Show();
                m_CurrentTower.Move(hit.point, hit.collider.transform.rotation, false);
            }
            else
            {
                m_CurrentTower.Hide();
            }
        }
Пример #15
0
    /// <summary>
    /// Start this instance.
    /// </summary>
    void Start()
    {
        Debug.Assert(spawnPoint, "Wrong initial settings");
        IPlacementArea placementArea = GetComponentInParent <IPlacementArea>();

        defPoint        = placementArea.transform.gameObject.GetComponentInChildren <DefendPoint>();
        cooldownCounter = cooldown;
        // Upgrade all existing defenders on tower build
        foreach (Transform point in defPoint.GetDefendPoints())
        {
            // If defend point already has defender
            Targetable defender = point.GetComponentInChildren <Targetable>();
            if (defender != null)
            {
                // Spawn new defender in the same place
                Spawn(defender.transform, point);
                // Destroy old defender
                Destroy(defender.gameObject);
            }
        }
    }
Пример #16
0
        protected virtual void MoveGhostWithRaycastHit(RaycastHit raycast)
        {
            //m_CurrentArea = raycast.collider.GetComponent<BuildingPlaceDisplayMappingBind>().GetBindGameObject().GetComponent<IPlacementArea>();
            m_CurrentArea = raycast.collider.GetComponent <IPlacementArea>();
            if (m_CurrentArea == null)
            {
                Debug.LogError("There is not an IPlacementArea attached to the collider found on the m_PlacementAreaMask");
                return;
            }

            m_GridPosition = m_CurrentArea.WorldToGrid(raycast.point);
            TowerFitStatus fits = m_CurrentArea.Fits(m_GridPosition);

            m_CurrentGhost.Show();
            m_GhostPlacementPossible = fits == TowerFitStatus.Fits && IsValidPurchase();
            if (m_GhostPlacementPossible)
            {
                //print("可以放置");
            }
            m_CurrentGhost.transform.position = raycast.point;
        }
        private void MoveGhostOntoWorld(Ray ray, bool hideWhenInvalid)
        {
            currentArea = null;

            if (!hideWhenInvalid)
            {
                RaycastHit hit;
                // check against all layers that the ghost can be on
                Physics.SphereCast(ray, sphereCastRadius, out hit, float.MaxValue, ghostWorldPlacementMask);
                if (hit.collider == null)
                {
                    return;
                }
                SetVisiable(true);
                Move(hit.point, hit.collider.transform.rotation, false);
            }
            else
            {
                SetVisiable(false);
            }
        }
Пример #18
0
        public void Initialize(
            IPlacementArea area,
            IntVector2 gridPosition,
            IntVector2 sizeOffset,
            CourseOfAction courseOfAction)
        {
            this.courseOfAction = courseOfAction;

            placementArea    = area;
            areaGridPosition = gridPosition;
            areaSizeOffset   = sizeOffset;

            Damage = courseOfAction.GetDamage();
            Range  = courseOfAction.GetRange();
            RangeIndicator.localScale = Vector3.one * Range * 2f;
            FireRate = courseOfAction.GetFireRate();

            // Hover and click actions
            ClickableBehaviour.Title = courseOfAction.custom.mitigation;
            ClickableBehaviour.Text  = Formatter.BuildStixDataEntityDescription(courseOfAction);

            ClickableBehaviour.ActionText    = "select";
            ClickableBehaviour.PrimaryAction = () => {
                SelectionHelper.DeselectAllMitigations();
                IsSelected = true;

                var title           = courseOfAction.custom.mitigation;
                var description     = Formatter.BuildStixDataEntityDescription(courseOfAction, true, false);
                var selectedActions = new SelectedAction[] {
                    new SelectedAction(ActionType.Sell, sell),
                    new SelectedAction(
                        ActionType.OpenExternalReferences,
                        () => ReferencesHelper.OpenExternalReferences(courseOfAction))
                };
                HelperObjects.SelectedInfoBar.SelectEntity(title, "Mitigation", description, selectedActions);
            };
            ClickableBehaviour.HasSecondaryAction =
                ReferencesHelper.AddReferencesAsAction(courseOfAction, ClickableBehaviour);
        }
Пример #19
0
 /// <summary>
 /// Snaps a given world positionn to this grid
 /// </summary>
 public static Vector3 Snap(this IPlacementArea placementArea, Vector3 worldPosition, IntVector2 sizeOffset)
 {
     // Calculate the nearest grid location and then change that back to world space
     return(placementArea.GridToWorld(placementArea.WorldToGrid(worldPosition, sizeOffset), sizeOffset));
 }
Пример #20
0
 public HighlightTiles(IPlacementArea _placementArea)
 {
     placementArea    = _placementArea;
     highlightedTiles = new List <ITile>();
 }
Пример #21
0
 public PopUpModel(IPlacementArea _placemanetArea)
 {
     placemanetArea = _placemanetArea;
 }
Пример #22
0
        private void Update()
        {
            // Check collisions with placement areas
            var        mouseRay = HelperObjects.CameraComponent.ScreenPointToRay(Input.mousePosition);
            RaycastHit areaHit;

            Physics.Raycast(mouseRay, out areaHit);

            // Clear all potential tiles when leaving area
            if (targetArea == null && !firstPlacement)
            {
                clearAllOldTiles();
            }

            if (areaHit.collider != null)
            {
                targetArea = areaHit.collider.GetComponent <IPlacementArea>();

                if (targetArea != null)
                {
                    // If first placement, instantly move and enable
                    if (firstPlacement)
                    {
                        firstPlacement       = false;
                        transform.position   = areaHit.point;
                        transform.localScale = Vector3.one;
                        return;
                    }

                    var snappedPosition     = targetArea.Snap(areaHit.point, SizeOffset);
                    var snappedGridPosition = targetArea.WorldToGrid(areaHit.point, SizeOffset);

                    // Check if the ghost fits and isn't too close to the path
                    var        fits             = targetArea.Fits(snappedGridPosition, SizeOffset) == TowerFitStatus.Fits;
                    Collider[] collisions       = Physics.OverlapSphere(areaHit.point, PathCollisionRadius);
                    var        collidesWithPath = collisions.Any((c) => c.gameObject.layer == Constants.PATH_LAYER);
                    if (fits && !collidesWithPath)
                    {
                        transform.localScale = Vector3.one;

                        // Set new position and update placement tile state
                        if (snappedPosition != targetPosition)
                        {
                            clearOldTiles();
                            targetArea.Occupy(snappedGridPosition, SizeOffset, PlacementTileState.Potential);

                            targetPosition = snappedPosition;
                        }
                    }
                }
            }

            // Move ghost
            if (Vector3.SqrMagnitude(transform.position - targetPosition) > 0.01f)
            {
                transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, Dampening);
            }
            else
            {
                velocity = Vector3.zero;
            }

            // Build when clicked
            if (Input.GetMouseButtonDown(0))
            {
                if (targetArea != null && !GameManager.IsUiBlocking)
                {
                    BuildManager.I.ImplementMitigation(
                        targetArea,
                        targetArea.WorldToGrid(targetPosition, SizeOffset),
                        SizeOffset);

                    DestroyGhost();
                }
            }
            else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
            {
                clearAllOldTiles();
                enabled = false;
                DestroyGhost();
            }
        }