Example #1
0
    /// <summary>
    /// 移动建筑物到指定位置
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void DraggingBuildingTo(int x, int y)
    {
        //REMARK 一个建筑格子占2个小格子
        if (x % 2 == 1)
        {
            x -= 1;
        }
        if (y % 2 == 1)
        {
            y -= 1;
        }
        if (currentBuilding.GetTilePos().x == x && currentBuilding.GetTilePos().y == y)
        {
            return;
        }
        if (!draged)
        {
            draged = true;
            EventDispather.DispatherEvent(GameEvents.BEGIN_DRAG_BUILDING);
        }
        currentBuilding.SetTilePosition(new TilePoint(x, y));
        currentBuilding.view.transform.position = currentBuilding.GetRenderPosition();
        var pos = IsoHelper.GridToPosition(x, y);

        buildableView.gameObject.SetActive(true);
        buildableView.transform.position = pos;
        IsoHelper.MoveAlongCamera(buildableView.transform, 5f);
        buildArrow.transform.position = pos;
//        IsoHelper.MoveAlongCamera(buildArrow.transform,-1f);
        buildConfirm.transform.position = pos;
        IsoHelper.MoveAlongCamera(buildConfirm.transform, -1f);

        //  是否可以建造
        buildableView.SetBuildable(IsoMap.Instance.CanPlaceBuilding(x, y, currentBuilding.width, currentBuilding.height));
    }
Example #2
0
    private void SetRange(float range, float blindRange)
    {
        if (rangeView == null)
        {
            rangeView = transform.FindChild("Range");
            rangeView.localPosition = IsoHelper.MoveAlongCamera(Vector3.zero, Constants.RANGE_Z_ORDER);
        }
        var rangeScale = range * 0.1f;

        rangeView.localScale = new Vector3(rangeScale, rangeScale, rangeScale);//素材占半径占10格子 r/10
        if (blindRange > 0)
        {
            if (blindRangeView == null)
            {
                blindRangeView               = ((GameObject)Instantiate(rangeView.gameObject)).transform;
                blindRangeView.parent        = transform;
                blindRangeView.localPosition = IsoHelper.MoveAlongCamera(Vector3.zero, Constants.BLIND_RANGE_Z_ORDER);
                var sprites = blindRangeView.GetComponentsInChildren <tk2dSprite>();
                foreach (var sprite in sprites)
                {
                    sprite.color = new Color(0.8f, 0.2f, 0.2f);
                }
            }
            blindRangeView.gameObject.SetActive(true);
            var blindRangeScale = blindRange * 0.1f;
            blindRangeView.localScale = new Vector3(blindRangeScale, blindRangeScale, blindRangeScale);
        }
        else
        {
            if (blindRangeView != null)
            {
                blindRangeView.gameObject.SetActive(false);
            }
        }
    }
Example #3
0
    public GameEffect(int effectId, string effectName, Vector3 pos, bool loop)
    {
        this.effectId = effectId;
        var prefab = (GameObject)ResourceManager.Instance.Load("Effects/" + effectName);

        if (prefab != null)
        {
            m_view = PoolManager.Instance.Spawn(prefab);
        }
        if (m_view != null)
        {
            m_view.transform.position = pos;
            IsoHelper.MoveAlongCamera(m_view.transform, Constants.EFFECT_Z_ORDER);
            IsoHelper.FaceToWorldCamera(m_view.transform);
            //  REMARK:循环效果应该手动释放
            if (!loop)
            {
                var trigger = m_view.GetComponent <EffectTrigger>();
                if (trigger != null)
                {
                    trigger.CompleteEvent += trigger_CompleteEvent;
                }
                else
                {
                    //TODO 临时方案
                    m_animator = m_view.GetComponentInChildren <tk2dSpriteAnimator>();
                    if (m_animator != null)
                    {
                        m_animator.AnimationCompleted   = OnAnimationCompleted;
                        m_animator.DefaultClip.wrapMode = (loop ? tk2dSpriteAnimationClip.WrapMode.Loop : tk2dSpriteAnimationClip.WrapMode.Once);
                        if (!m_animator.playAutomatically)
                        {
                            m_animator.Play(m_animator.DefaultClip);
                        }
                    }
                    else
                    {
                        Debug.LogWarning(string.Format("effect {0} miss animator...", effectName));
                    }
                }
            }
        }
    }
Example #4
0
    private void UpdateDestroy(float dt)
    {
        //  死亡了 但尚未置于死亡状态(则释放)
        if (currentHp <= 0 && this.State != EntityStateType.Dead)
        {
            this.State = EntityStateType.Dead;

            if (GameWorld.Instance.worldType == WorldType.Battle || GameWorld.Instance.worldType == WorldType.Replay)
            {
                BattleManager.Instance.OnEntityDestroy(this);
            }

            //创建战斗时被销毁的残渣
            if (EntityTypeUtil.IsAnyBuilding(entityType))
            {
                GameObject.Destroy(view.body.gameObject);
                GameObject.Destroy(view.hpBar.gameObject);
                if (view.shadow != null)
                {
                    GameObject.Destroy(view.shadow.gameObject);
                }
                GameEffectManager.Instance.AddEffect("Phouse_die", GetRenderPosition());//TODO
                var destroyView = (GameObject)ResourceManager.Instance.LoadAndCreate("Misc/DestroyedBuildingView");
                view.AddSubView(destroyView, Vector3.zero);
                IsoHelper.FaceToWorldCamera(destroyView.transform);
                IsoHelper.MoveAlongCamera(destroyView.transform, 19);
                Destroy(false);
                IsoMap.Instance.RemoveBuildingMapData(this);//移除占地数据,对象还是在IsoMap中
            }
            else if (EntityTypeUtil.IsAnyActor(entityType))
            {
                GameEffectManager.Instance.AddEffect("Psoldier_die", GetRenderPosition());
                IsoMap.Instance.DelayRemoveEntity(this);
                Destroy();
            }
            else
            {
                IsoMap.Instance.DelayRemoveEntity(this);
                Destroy();
            }
        }
    }
Example #5
0
    virtual public void Init()
    {
        if (!inited)
        {
            inited = true;

            view.Init();
            view.transform.position = GetRenderPosition();

            if (EntityTypeUtil.IsFlyable(model.entityType))
            {
                view.body.Translate(0, Constants.FLY_HEIGHT, 0, Space.Self);
                IsoHelper.MoveAlongCamera(view.shadow, -2 * Constants.SHADOW_Z_ORDER);//空军的阴影显示在建筑上面
            }

            foreach (var tileEntityComponent in components)
            {
                tileEntityComponent.Init();
            }
        }
    }
    virtual public void Init(bool faceToMainCamera = true)
    {
        _animationFinishCallbacks = null;

        if (faceToMainCamera)
        {
            foreach (Transform child in transform)
            {
                if (child.GetComponent <tk2dSprite>() != null)
                {
                    IsoHelper.FaceToWorldCamera(child);
                    if (child.name.IndexOf("Shadow") != -1)
                    {
                        shadow = child;
                        IsoHelper.MoveAlongCamera(child, Constants.SHADOW_Z_ORDER);
                    }
                }
            }
        }
        body = transform.FindChild("body");
        if (body == null)//多状态的
        {
            SwitchBody(1);
        }
        else
        {
            animator = body.GetComponentInChildren <tk2dSpriteAnimator>();
            //attachPoint = body.GetComponentInChildren<tk2dSpriteAttachPoint>();
            this.effAttachPoint = body.GetComponentInChildren <tk2dSpriteAttachPoint>();
            //RegisterSpriteChangeEvent();
        }
        if (animator != null)
        {
            animator.AnimationCompleted      = ontk2dSpriteAnimatorCallback_Completed;
            animator.AnimationEventTriggered = OnAnimationEventTriggered;

            animator.enabled = false;   //  REMARK:禁用掉然后手动更新
        }
    }
Example #7
0
    public void Init(GuardAreaValue[,] guardMap)
    {
        transform.position = IsoHelper.MoveAlongCamera(Vector3.zero, Constants.GUARD_VIEW_Z_ORDER);
        foreach (Transform child in transform)
        {
            Destroy(child.gameObject);
        }
        var tile01 = (GameObject)ResourceManager.Instance.Load("Misc/Tile01");
        var tile02 = (GameObject)ResourceManager.Instance.Load("Misc/Tile02");
        var tile03 = (GameObject)ResourceManager.Instance.Load("Misc/Tile03");

        for (int i = 0; i < Constants.EDGE_WIDTH; i++)
        {
            for (int j = 0; j < Constants.EDGE_HEIGHT; j++)
            {
                GameObject prefab = null;
                var        value  = guardMap[i, j];
                var        angle  = 0;
                if (value == GuardAreaValue.BottomRight)
                {
                    prefab = tile02;
                    angle  = 0;
                }
                else if (value == GuardAreaValue.BottomLeft)
                {
                    prefab = tile02;
                    angle  = 90;
                }
                else if (value == GuardAreaValue.TopLeft)
                {
                    prefab = tile02;
                    angle  = 180;
                }
                else if (value == GuardAreaValue.TopRight)
                {
                    prefab = tile02;
                    angle  = -90;
                }
                else if (value == GuardAreaValue.Right)
                {
                    prefab = tile01;
                    angle  = 0;
                }
                else if (value == GuardAreaValue.Bottom)
                {
                    prefab = tile01;
                    angle  = 90;
                }
                else if (value == GuardAreaValue.Left)
                {
                    prefab = tile01;
                    angle  = 180;
                }
                else if (value == GuardAreaValue.Top)
                {
                    prefab = tile01;
                    angle  = -90;
                }
                else if (value == GuardAreaValue.Zero)
                {
                    if (i + 1 < Constants.EDGE_WIDTH && j - 1 > 0 && guardMap[i + 1, j - 1] == GuardAreaValue.NIL)
                    {
                        prefab = tile03;
                        angle  = 0;
                    }
                    else if (i - 1 > 0 && j - 1 > 0 && guardMap[i - 1, j - 1] == GuardAreaValue.NIL)
                    {
                        prefab = tile03;
                        angle  = 90;
                    }
                    else if (i - 1 > 0 && j + 1 < Constants.EDGE_HEIGHT && guardMap[i - 1, j + 1] == GuardAreaValue.NIL)
                    {
                        prefab = tile03;
                        angle  = 180;
                    }
                    else if (i + 1 < Constants.EDGE_WIDTH && j + 1 < Constants.EDGE_HEIGHT &&
                             guardMap[i + 1, j + 1] == GuardAreaValue.NIL)
                    {
                        prefab = tile03;
                        angle  = -90;
                    }
                }
                if (prefab != null)
                {
                    var v1 = Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
                    v1.transform.parent        = this.transform;
                    v1.transform.localRotation = Quaternion.Euler(90, angle, 0);
                    v1.transform.localPosition = new Vector3(i - 0.5f, 0, j - 0.5f);
                    if (GameWorld.Instance.worldType == WorldType.Battle)
                    {
                        v1.GetComponent <tk2dSprite>().color = new Color(0.8f, 0.2f, 0.2f);
                    }
                }
            }
        }
    }
Example #8
0
    public static TileEntity Create(OwnerType owner, EntityModel model, bool isFriendly = false)
    {
        var entityObj  = LoadAndCreate(model);
        var tileEntity = new TileEntity();

        tileEntity.owner      = owner;
        tileEntity.model      = model;
        tileEntity.State      = EntityStateType.Idle;
        tileEntity.aiType     = (EntityAiType)model.targetType;
        tileEntity.entityType = model.entityType;
        tileEntity.Friendly   = isFriendly;
        if (model.tileSize > 0)
        {
            tileEntity.width = tileEntity.height = model.tileSize * 2;      //  REMARK:建筑实际占用格子扩大2倍
        }
        else
        {
            tileEntity.width = tileEntity.height = 1;
        }
        tileEntity.currentHp           = tileEntity.maxHp = Mathf.Max(model.hp, 1); //  REMARK:这里设置最大hp的最小值为1,不然对于工人等直接就是dead状态。
        tileEntity.animationNamePrefix = model.nameForResource + "_";

        //  REMARK:建筑按照格子建造、士兵按照边行走(所以士兵位置比建筑偏移(0.5,0.5))
        if (EntityTypeUtil.IsAnyActor(model.entityType))
        {
            tileEntity.renderTileSizeOffset = new Vector2((tileEntity.width * 0.5f) - 1.0f, (tileEntity.height * 0.5f) - 1.0f);
        }
        else
        {
            tileEntity.renderTileSizeOffset = new Vector2((tileEntity.width * 0.5f) - 0.5f, (tileEntity.height * 0.5f) - 0.5f);
        }
        tileEntity.view = entityObj.GetComponent <EntityViewComponent>();
        if (tileEntity.view == null)
        {
            if (EntityTypeUtil.IsAnyActor(tileEntity.entityType))
            {
                tileEntity.view = entityObj.AddComponent <ActorView>();
            }
            else if (EntityTypeUtil.IsAnyTower(tileEntity.entityType))
            {
                tileEntity.view = entityObj.AddComponent <TowerView>();
            }
            else
            {
                tileEntity.view = entityObj.AddComponent <EntityViewComponent>();
            }
        }

        if (EntityTypeUtil.IsAnyBuilding(tileEntity.entityType))//[建筑类]
        {
            //1格以上建筑有地皮
            if (model.tileSize > 2)
            {
                var floorBase = (GameObject)ResourceManager.Instance.LoadAndCreate(model.raceType + "/Entities/Misc/Floor4");//地皮
                tileEntity.view.AddSubView(floorBase, Vector3.zero);
                IsoHelper.FaceToWorldCamera(floorBase.transform);
                IsoHelper.MoveAlongCamera(floorBase.transform, Constants.FLOOR_Z_ORDER);
                floorBase.transform.localScale = Vector3.one * model.tileSize / 4;//REMARK 现有素材占4x4个tilesize
            }

            if (EntityTypeUtil.IsBarracks(model))
            {
                tileEntity.blockingRange = 3;
            }
            else
            {
                tileEntity.blockingRange = model.tileSize * 2 - 1;
            }
            tileEntity.AddComponent <ConstructBuildingComponent>();
            if (model.entityType == EntityType.Tower)
            {
                tileEntity.AddComponent <TowerComponent>();
            }
            else if (model.entityType == EntityType.Wall)
            {
                tileEntity.AddComponent <WallComponent>();
            }
            else if (EntityTypeUtil.IsGatherResourceBuilding(model))
            {
                tileEntity.AddComponent <GatherResourceBuildingComponent>();
            }
            else if (EntityTypeUtil.IsStorageResoruceBuilding(model))
            {
                tileEntity.AddComponent <ResourceStorageBuildingComponent>();
            }
            else if (EntityTypeUtil.IsArmyShop(model))
            {
                tileEntity.AddComponent <ProductSoldierBuildingComponent>();
            }
            else if (EntityTypeUtil.IsSkillShop(model))
            {
                tileEntity.AddComponent <ProductSkillBuildingComponent>();
            }
            else if (EntityTypeUtil.IsWorkerHouse(model))
            {
                tileEntity.AddComponent <WorkerHouseComponent>();
            }
            else if (EntityTypeUtil.IsFederal(model))
            {
                tileEntity.AddComponent <FederalComponent>();
            }
            else if (EntityTypeUtil.IsResearch(model))
            {
                tileEntity.AddComponent <ResearchBuildingComponent>();
            }
        }
        else if (EntityTypeUtil.IsAnyActor(model.entityType))   //[角色类]
        {
            //  战斗模式 家园模式 通用组件
            tileEntity.AddComponent <ActorMoveComponent>();
            if (GameWorld.Instance.worldType == WorldType.Home || GameWorld.Instance.worldType == WorldType.Visit) //家园模式 或者 访问模式
            {
                //  家园模式
                //  工人(添加工人组件&添加到工人小屋中)
                if (EntityTypeUtil.IsWorker(model))
                {
                    tileEntity.AddComponent <WorkmanComponent>();
                    var houseComp = IsoMap.Instance.GetWorkerHouseComponent();
                    Assert.Should(houseComp != null, "worker house is not exist...");
                    houseComp.AddAWorkman(tileEntity);
                }
                else
                {
                    tileEntity.AddComponent <ArmyComponent>();
                }
            }
            else                                                                                                    //战斗模式
            {
                tileEntity.AddComponent <GameBufferComponent>();
                //  设置士兵的监视范围(寻找目标用)    REMARK:设定为4.0秒可以移动的范围
                tileEntity.monitorRange = Mathf.Clamp(model.speed * 4.0f, 5.0f, 20.0f);
                if (EntityTypeUtil.IsBombMan(model))
                {
                    tileEntity.monitorRange = 0.0f;     //  暂时不限制炸弹人
                    tileEntity.AddComponent <BombManComponent>();
                }
                else if (EntityTypeUtil.IsCurer(model))
                {
                    tileEntity.AddComponent <CurerComponent>();
                }
                else if (isFriendly)                    //  友军/援军
                {
                    tileEntity.AddComponent <FriendComponent>();
                }
                else
                {
                    tileEntity.AddComponent <ActorComponent>();
                }
                //  除【友军/援军】以外添加拆墙组件(REMARK:友军/援军不可拆墙)
                if (!isFriendly)
                {
                    tileEntity.AddComponent <ActorDestroyWallComponent>();
                }
            }
        }
        else if (EntityTypeUtil.IsAnyTrap(model))   //  [陷阱类]
        {
            tileEntity.AddComponent <TrapComponent>();
        }
        Assert.Should((tileEntity.blockingRange == 0 || (tileEntity.blockingRange % 2) == 1), "invalid blockingRange...");
        Assert.Should(tileEntity.blockingRange <= tileEntity.width - 1 && tileEntity.blockingRange <= tileEntity.height - 1, "invalid blockingRange...");
        return(tileEntity);
    }
Example #9
0
 public void Start()
 {
     IsoHelper.FaceToWorldCamera(transform);
     IsoHelper.MoveAlongCamera(transform, zOrder);
 }