local ColliderManager = require("Battle/Logic/Level/ColliderManager") local EventSystem = require('Event/EventSystem') local Base = require("Battle/Logic/Level/LevelItemFactory/LevelItem") local LevelItem_GrassArea = class("LevelItem_GrassArea", Base) local VisualObject = require("Battle/Expression/VisualObject") -- 构造函数 ,使用的时候用new ,如果没有特殊的变量设置,不需要这个函数 function LevelItem_GrassArea:initialize(data) Base.initialize(self,data) if data.Type == LuaConst.LevelShapeType.Circle then self._colliderID = ColliderManager.CreateCircleCollider(data.ItemPos,data.Radius * data.Radius) --注意!目前圆形草丛没有资源,不好铺视图,所以编辑器里把圆形草丛的视图给屏蔽了 elseif data.Type == LuaConst.LevelShapeType.Square then self._colliderID = ColliderManager.CreateRectCollider(data.ItemPos,data.ItemDir,data.Length,data.Width) end self._grassGroup = data.Group ColliderManager.AddEnterEvent(self._colliderID,self.OnTriggerEnter,self) ColliderManager.AddExitEvent(self._colliderID,self.OnTriggerExit,self) self:ShowExpression() end --region --***************生命周期***************-- function LevelItem_GrassArea:OnActive() Base.OnActive(self) ColliderManager.ActiveCollider(self._colliderID, LogicUpdateManager.ValidAllEntityIter) --未完成HYW now now now now 这里用的是dic,可能会导致不一致,需要拓展ColliderManager.ActiveCollider,支持iter的列表 end function LevelItem_GrassArea:OnDeactive() Base.OnDeactive(self) if self._colliderID then ColliderManager.DeActiveCollider(self._colliderID) end end --纯视图代码,可以不用考虑帧同步↓ function LevelItem_GrassArea:ShowExpression() if self.ItemData.Type == LuaConst.LevelShapeType.Circle then logging.error(LTAG.ERROR, "HYW : 关卡策划注意!不支持圆形草丛视图! 关卡ID : ", LevelManager.GetCurLevelID(), " 关卡元素ID : ", self.ItemData.LevelItemID) elseif self.ItemData.Type == LuaConst.LevelShapeType.Square then --目前草丛资源的单元格尺寸为1*1的正方形,所以下面的算法没有代入长度,只能用于边长为1的正方形的单元格 --if self.ItemData.ItemDir ~= FP.Const.Vector2_Forward and self.ItemData.ItemDir ~= Vector2_Right then local rawLength = FP.tonumberV1(self.ItemData.Length) local rawWidth = FP.tonumberV1(self.ItemData.Width) local length = math.floor(rawLength + 0.5) --整数化网格长度 local width = math.floor(rawWidth + 0.5) --整数化网格宽度 local dx, dz = FP.tonumberV2(self.ItemData.ItemDir) --local diagonal = math.sqrt(rawLength * rawLength + rawWidth * rawWidth) local dirSqrt = math.sqrt(dx * dx + dz * dz) local cos = dx / dirSqrt local sin = dz / dirSqrt local rad = math.acos(cos) local offsetRad = math.pi * 0.75 - rad local gridHalfDiagonal = math.sqrt(2) / 2 --logging.error(LTAG.ERROR, "HYW : rad : ", rad, " math.acos(cos) : ", math.acos(cos)) local gridOffsetX = math.cos(offsetRad) * gridHalfDiagonal * -1 --正方形单元格的offset --logging.error(LTAG.ERROR, "HYW : math.cos(offsetRad) : ", math.cos(offsetRad), " gridHalfDiagonal : ", gridHalfDiagonal) local gridOffsetZ = math.sin(offsetRad) * gridHalfDiagonal --正方形单元格的offset --logging.error(LTAG.ERROR, "HYW : gridOffset : ", gridOffsetX, gridOffsetZ) local grassVisualObject local root = ExpLevelManager.SceneObjParent local px, pz = FP.tonumberV2(self.ItemData.ItemPos) local curColumn = 1 local curRow = 1 local startX = px - rawWidth * cos / 2 + rawLength * sin / 2 + gridOffsetX --logging.error(LTAG.ERROR, "HYW : startX : ", startX, " px : ", px ) local startZ = pz - rawWidth * sin / 2 - rawLength * cos / 2 + gridOffsetZ --logging.error(LTAG.ERROR, "HYW : startZ : ", startZ, " pz : ", pz) local curX = startX local curZ = startZ local count = length * width if count < 1 then count = 1 end --logging.error(LTAG.ERROR, "HYW : 长宽 : ", length, width, " 一共", count, "个草垛") for i=1,count do local prefabPath = self.ItemData.ObjectPath if string.isNilOrEmpty(prefabPath) then prefabPath = ConstAssetPathConfig.prefabPath end grassVisualObject = VisualObject.CreateVisualObject(prefabPath , root) grassVisualObject:SetDirection(dx, dz) grassVisualObject:SetPosition(curX, 0 , curZ) curColumn = curColumn + 1 if curColumn > width then curColumn = 1 curRow = curRow + 1 curX = startX - (curRow - 1) * dz curZ = startZ + (curRow - 1) * dx else curX = curX + dx curZ = curZ + dz end end --end end end --视图代码,可以不用考虑帧同步↑ function LevelItem_GrassArea:OnDestroy() Base.OnDestroy(self) if self._colliderID then ColliderManager.DestroyCollider(self._colliderID) end end function LevelItem_GrassArea:OnTriggerEnter(entity) if entity:IsActive() then entity.components.visualField:SetVisibility(1, self._grassGroup, self.ItemData.LevelItemID) entity.components.visualField:SetVisual(1, self._grassGroup, self.ItemData.LevelItemID) end end function LevelItem_GrassArea:OnTriggerExit(entity) if entity:IsActive() then entity.components.visualField:SetVisibility(0, -1, self.ItemData.LevelItemID) entity.components.visualField:SetVisual(0, -1, self.ItemData.LevelItemID) end end --region 生命周期 --region --***************事件相关***************-- function LevelItem_GrassArea:OnPlayerDead(entity) if entity._logicInitData.CharacterType == LuaConst.CharacterType.Adjutant then return end LevelItemManager.Global:Notify_PlayerDead(entity, self.LevelItemID) end --endregion 事件相关 return LevelItem_GrassArea