Пример #1
0
    virtual public void MakeDamage(float damage)
    {
#if UNITY_EDITOR
        BattleManager.logDamageList.Add(GameRecord.Frame + ":" + this.model.nameForView + ":" + damage + ":" + currentHp);
#endif  //  UNITY_EDITOR

        //  REMARK:damage如果为负则回血
        currentHp -= damage;
        HandleMessage(EntityMessageType.MakeDamage, damage);
        //  限制不能超过最大maxHp(在回血的情况下)
        currentHp = Mathf.Clamp(currentHp, 0, maxHp);
        float percent = currentHp / (float)maxHp;
        view.SetHpBarPercent(percent);

        if (halfDestroyEffectId == 0 && percent < 0.5f && EntityTypeUtil.IsAnyBuilding(entityType))
        {
            halfDestroyEffectId = GameEffectManager.Instance.AddEffect("Phouse_huo", GetRenderPosition(), true);
        }
    }
Пример #2
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();
            }
        }
    }
Пример #3
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);
    }
Пример #4
0
    /// <summary>
    /// 初始化战斗中的防御区域
    /// </summary>
    public void InitGuardAreaMap()
    {
        //  1、初始化空
        m_guardAreaMap = new GuardAreaValue[Constants.EDGE_WIDTH, Constants.EDGE_HEIGHT];
        for (int x = 0; x < Constants.EDGE_WIDTH; x++)
        {
            for (int y = 0; y < Constants.EDGE_HEIGHT; y++)
            {
                m_guardAreaMap[x, y] = GuardAreaValue.NIL;
            }
        }

        //  2、根据建筑初始化防御区域,这里获取Defender方建筑。

        //  REMARK:扩展范围宽度 这个值可以调整
        int extendWidth = 2;

        foreach (var entity in GetAllEntitiesByOwner(OwnerType.Defender))
        {
            if (!EntityTypeUtil.IsAnyBuilding(entity.entityType))
            {
                continue;
            }

            int bgnX = Mathf.Max(entity.GetTilePos().x - extendWidth, 0);
            int endX = Mathf.Min(entity.GetTilePos().x + entity.width + extendWidth, Constants.EDGE_WIDTH - 1);

            int bgnY = Mathf.Max(entity.GetTilePos().y - extendWidth, 0);
            int endY = Mathf.Min(entity.GetTilePos().y + entity.height + extendWidth, Constants.EDGE_HEIGHT - 1);

            for (int x = bgnX; x <= endX; x++)
            {
                for (int y = bgnY; y <= endY; y++)
                {
                    m_guardAreaMap[x, y] = GuardAreaValue.Uninitialized;
                }
            }
        }

        //  3、计算每个防御区域格子贴图方向
        for (int x = 0; x < Constants.EDGE_WIDTH; x++)
        {
            for (int y = 0; y < Constants.EDGE_HEIGHT; y++)
            {
                if (m_guardAreaMap[x, y] == GuardAreaValue.NIL || m_guardAreaMap[x, y] != GuardAreaValue.Uninitialized)
                {
                    continue;
                }

                //  计算防御区域方向(默认为zero无方向)
                GuardAreaValue dir = GuardAreaValue.Zero;
                {
                    //  上
                    if (y + 1 < Constants.EDGE_HEIGHT && m_guardAreaMap[x, y + 1] == GuardAreaValue.NIL)
                    {
                        dir |= GuardAreaValue.Top;
                    }

                    //  下
                    if (y - 1 >= 0 && m_guardAreaMap[x, y - 1] == GuardAreaValue.NIL)
                    {
                        dir |= GuardAreaValue.Bottom;
                    }

                    //  左
                    if (x - 1 >= 0 && m_guardAreaMap[x - 1, y] == GuardAreaValue.NIL)
                    {
                        dir |= GuardAreaValue.Left;
                    }

                    //  右
                    if (x + 1 < Constants.EDGE_WIDTH && m_guardAreaMap[x + 1, y] == GuardAreaValue.NIL)
                    {
                        dir |= GuardAreaValue.Right;
                    }
                }

                //  设置新的方向
                m_guardAreaMap[x, y] = dir;
            }
        }

        if (m_guardAreaView == null)
        {
            var go = new GameObject();
            go.name         = "GuardAreaView";
            m_guardAreaView = go.AddComponent <GuardAreaView>();
            go.SetActive(false);
        }
        m_guardAreaView.Init(m_guardAreaMap);
    }