Added the dynamic creation of a SET_ZONE, but you still need to declare all zones within the mission script.

This commit is contained in:
FlightControl_Master 2018-05-14 08:12:15 +02:00
parent 7598a6ce5c
commit 48384ac758
4 changed files with 123 additions and 0 deletions

View File

@ -96,6 +96,8 @@ function DATABASE:New()
self:HandleEvent( EVENTS.Hit, self.AccountHits )
self:HandleEvent( EVENTS.NewCargo )
self:HandleEvent( EVENTS.DeleteCargo )
self:HandleEvent( EVENTS.NewZone )
self:HandleEvent( EVENTS.DeleteZone )
-- Follow alive players and clients
--self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit ) -- This is not working anymore!, handling this through the birth event.
@ -1085,6 +1087,31 @@ function DATABASE:OnEventDeleteCargo( EventData )
end
--- Handles the OnEventNewZone event.
-- @param #DATABASE self
-- @param Core.Event#EVENTDATA EventData
function DATABASE:OnEventNewZone( EventData )
self:F2( { EventData } )
if EventData.Zone then
self:AddZone( EventData.Zone )
end
end
--- Handles the OnEventDeleteZone.
-- @param #DATABASE self
-- @param Core.Event#EVENTDATA EventData
function DATABASE:OnEventDeleteZone( EventData )
self:F2( { EventData } )
if EventData.Zone then
self:DeleteZone( EventData.Zone.ZoneName )
end
end
--- Gets the player settings
-- @param #DATABASE self
-- @param #string PlayerName

View File

@ -179,6 +179,8 @@ EVENT = {
world.event.S_EVENT_NEW_CARGO = world.event.S_EVENT_MAX + 1000
world.event.S_EVENT_DELETE_CARGO = world.event.S_EVENT_MAX + 1001
world.event.S_EVENT_NEW_ZONE = world.event.S_EVENT_MAX + 1002
world.event.S_EVENT_DELETE_ZONE = world.event.S_EVENT_MAX + 1003
--- The different types of events supported by MOOSE.
-- Use this structure to subscribe to events using the @{Base#BASE.HandleEvent}() method.
@ -209,6 +211,8 @@ EVENTS = {
ShootingEnd = world.event.S_EVENT_SHOOTING_END,
NewCargo = world.event.S_EVENT_NEW_CARGO,
DeleteCargo = world.event.S_EVENT_DELETE_CARGO,
NewZone = world.event.S_EVENT_NEW_ZONE,
DeleteZone = world.event.S_EVENT_DELETE_ZONE,
}
--- The Event structure
@ -406,6 +410,16 @@ local _EVENTMETA = {
Event = "OnEventDeleteCargo",
Text = "S_EVENT_DELETE_CARGO"
},
[EVENTS.NewZone] = {
Order = 1,
Event = "OnEventNewZone",
Text = "S_EVENT_NEW_ZONE"
},
[EVENTS.DeleteZone] = {
Order = 1,
Event = "OnEventDeleteZone",
Text = "S_EVENT_DELETE_ZONE"
},
}
@ -710,6 +724,36 @@ do -- Event Creation
world.onEvent( Event )
end
--- Creation of a New Zone Event.
-- @param #EVENT self
-- @param Core.Zone#ZONE_BASE Zone The Zone created.
function EVENT:CreateEventNewZone( Zone )
self:F( { Zone } )
local Event = {
id = EVENTS.NewZone,
time = timer.getTime(),
zone = Zone,
}
world.onEvent( Event )
end
--- Creation of a Zone Deletion Event.
-- @param #EVENT self
-- @param Core.Zone#ZONE_BASE Zone The Zone created.
function EVENT:CreateEventDeleteZone( Zone )
self:F( { Zone } )
local Event = {
id = EVENTS.DeleteZone,
time = timer.getTime(),
zone = Zone,
}
world.onEvent( Event )
end
--- Creation of a S_EVENT_PLAYER_ENTER_UNIT Event.
-- @param #EVENT self
-- @param Wrapper.Unit#UNIT PlayerUnit.
@ -874,6 +918,11 @@ function EVENT:onEvent( Event )
Event.CargoName = Event.cargo.Name
end
if Event.zone then
Event.Zone = Event.zone
Event.ZoneName = Event.zone.ZoneName
end
local PriorityOrder = EventMeta.Order
local PriorityBegin = PriorityOrder == -1 and 5 or 1
local PriorityEnd = PriorityOrder == -1 and 1 or 5

View File

@ -4657,6 +4657,9 @@ function SET_ZONE:FilterStart()
end
end
self:HandleEvent( EVENTS.NewZone )
self:HandleEvent( EVENTS.DeleteZone )
return self
end
@ -4726,3 +4729,41 @@ function SET_ZONE:IsIncludeObject( MZone )
return MZoneInclude
end
--- Handles the OnEventNewZone event for the Set.
-- @param #SET_ZONE self
-- @param Core.Event#EVENTDATA EventData
function SET_ZONE:OnEventNewZone( EventData ) --R2.1
self:F( { "New Zone", EventData } )
if EventData.Zone then
if EventData.Zone and self:IsIncludeObject( EventData.Zone ) then
self:Add( EventData.Zone.ZoneName , EventData.Zone )
end
end
end
--- Handles the OnDead or OnCrash event for alive units set.
-- @param #SET_ZONE self
-- @param Core.Event#EVENTDATA EventData
function SET_ZONE:OnEventDeleteZone( EventData ) --R2.1
self:F3( { EventData } )
if EventData.Zone then
local Zone = _DATABASE:FindZone( EventData.Zone.ZoneName )
if Zone and Zone.ZoneName then
-- When cargo was deleted, it may probably be because of an S_EVENT_DEAD.
-- However, in the loading logic, an S_EVENT_DEAD is also generated after a Destroy() call.
-- And this is a problem because it will remove all entries from the SET_ZONEs.
-- To prevent this from happening, the Zone object has a flag NoDestroy.
-- When true, the SET_ZONE won't Remove the Zone object from the set.
-- This flag is switched off after the event handlers have been called in the EVENT class.
self:F( { ZoneNoDestroy=Zone.NoDestroy } )
if Zone.NoDestroy then
else
self:Remove( Zone.ZoneName )
end
end
end
end

View File

@ -1001,6 +1001,9 @@ function ZONE_UNIT:New( ZoneName, ZoneUNIT, Radius )
self.ZoneUNIT = ZoneUNIT
self.LastVec2 = ZoneUNIT:GetVec2()
-- Zone objects are added to the _DATABASE and SET_ZONE objects.
_EVENTDISPATCHER:CreateEventNewZone( self )
return self
end
@ -1090,6 +1093,9 @@ function ZONE_GROUP:New( ZoneName, ZoneGROUP, Radius )
self._.ZoneGROUP = ZoneGROUP
-- Zone objects are added to the _DATABASE and SET_ZONE objects.
_EVENTDISPATCHER:CreateEventNewZone( self )
return self
end