Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

Gameplay.DynamicObject


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
//
// DynamicObject Class
// 
// - Destroyable object
// - Chunks are large child objects in Karma physics
// - Fragments are small child objects in the new physics system
// - Sounds and effects have been removed pending the new sound and effects system integration
//

class DynamicObject extends Engine.HavokActor 
	native 
	placeable;

cpptext
{
	virtual void initialiseHavokDataObject();	
	virtual void HavokPreSyncCallback(const FVector& deltaPos);
}

var (DynamicObject) float   Mass			"The mass of the object (kg)";
var (DynamicObject) float   Elasticity      "The elasticity [0,1]";
var (DynamicObject) float   friction		"The amount of friction [0,1]";
var (DynamicObject) float   LinearDamping   "Linear Damping [0,1] 0 = no damping";
var (DynamicObject) float   AngularDamping  "Angular Damping [0,1] 0 = no damping";
var (DynamicObject) float	Health			"The amount of health in the object";
var (DynamicObject) bool	Invulnerable	"Object will be invulnerable to weapon attacks";
var (DynamicObject) bool	Blocking	    "Blocks players and collide with other objects";
var (DynamicObject) bool	Stationary		"The object is stationary and cannot move";
var (DynamicObject) bool	Enabled			"Start enabled in physics right away";
var (DynamicObject) bool	Shadows			"Use projected shadows with this object (expensive)";
var (DynamicObject) bool	Volatile		"Object will explode immediately on collision or when hit with a weapon";
var (DynamicObject) bool	Freeze		    "Child objects all freeze immediately on spawning (useful for art testing)";
var (DynamicObject) bool    FadeOut		    "Fadeout object when destroyed (requires material to be setup correctly)";
var (DynamicObject) bool	LimitedLife		"If true, object will only hang around for Lifetime amount of time after spawning";
var (DynamicObject) float	Lifetime		"Object will only appear in the world for this long before fading out";		
var (DynamicObject) bool	Explosion     	"Launch child objects with initial explosion (true/false)";
var (DynamicObject) float   ExplosionForce	"Force applied to child objects when this object is destroyed";

var (DynamicObject) array< class<DynamicObject> > Children		"Child objects spawned when this object is destroyed";

var (DynamicObject) array< class<Explosion> > Explosions        "Explosion objects spawned when this object is destroyed";

var array<ColorModifier> Style;
var bool StyleCreated;

var bool FadingOut;
var float FadeOutAlpha;
var float FadeOutTime;
var float FadeOutLength;

var bool NextVersionPending;
var bool NextVersionCalled;	

// currently can switch of dynamic on minspec machine
enum EScalabilitySetting
{
	DS_NoDynamics,
	DS_Full,
};

overloaded function Construct( actor Owner, optional optional name Tag, 
				               optional vector Location, optional rotator Rotation, optional float Scale)
{
    Label = Tag;
	setNativeActorData(Scale);
	super.construct(Owner, Tag, Location, Rotation);
    Label = Tag;
}

// this is used to set script const data in the Construct call
native final function setNativeActorData(float Scale);

event PreBeginPlay()
{
	// local data -> actor

	bActorShadows = Shadows;
	
	if (!Blocking) 
        SetCollision(false, false, false);

	// force stationary if scalability says so
	if (GetScalabilitySetting() == DS_NoDynamics)
		Stationary = true;

	if (Stationary)
	{
		SetPhysics(PHYS_None);
	}
	else
	{
		// initially inactive (to stop decorations falling off walls etc)
		HavokActivate(Enabled);
	}

	Super.PreBeginPlay();
}

static simulated function PrecacheDynamicObjectRenderData(LevelInfo Level, class<DynamicObject> DynClass)
{
	local int i;

	if (DynClass.default.StaticMesh != None)
	{
		Level.AddPrecacheStaticMesh(DynClass.default.StaticMesh);
//		Log("PRECACHING DYNAMICOBJECT MESH "$DynClass.default.StaticMesh.Name);
	}

	// recurse through children
    for (i=0; i<DynClass.default.Children.Length; i++)
	{
		if (DynClass.default.Children[i] != None)
			PrecacheDynamicObjectRenderData(Level, DynClass.default.Children[i]);
	}
}

simulated function UpdatePrecacheRenderData()
{
	Super.UpdatePrecacheRenderData();
	PrecacheDynamicObjectRenderData(Level, Class);
}

event Tick(float DeltaTime)
{
    if (NextVersionPending)
	{
        NextVersion();
	}
    
	if (FadingOut)
	{
        FadeOutTime += DeltaTime;
        FadeOutAlpha = (1 - FadeOutTime / FadeOutLength) * 255;
        SetAlpha(FadeOutAlpha);
	}

	if (FadeOutAlpha<=0)
	{
        Destroy();
	}

	if (!FadingOut && LimitedLife)
	{
		LifeTime -= DeltaTime;
		if (LifeTime <= 0)
			Remove();
	}
}


function Trigger(Actor Other, Pawn EventInstigator)
{
	Health = 0;
    NextVersion();
}



function StartFadeOut()
{
	// start fade out

	CreateAlphaStyle();
	FadingOut = true;
}


event NextVersion()
{
	if (NextVersionCalled)
		return;

	if (!FadingOut)
	{
		NextVersionCalled = true;

		// trigger event on next version
		triggerEvent(Event, self, None);

		// handle child objects
		if (Children.Length>0)
		{
			// destroy self and spawn children
			SpawnChildren();
		}

		Remove();
	}
}


function Explode()
{
    local int i;

	local Explosion ExplosionObject;

	local String SocketName;
	local Vector SocketPosition;
	local Rotator SocketRotation;
	local Vector SocketScale;

    for (i=0; i<Explosions.length; i++)
    {
        GetExplosionSocketName(i, SocketName);

        if (getSocket(SocketName, SocketPosition, SocketRotation, SocketScale))
    		ExplosionObject = spawn(Explosions[i], , , SocketPosition, SocketRotation);
        else
		    ExplosionObject = spawn(Explosions[i], , , unifiedGetCOMPosition(), Rotation);

		ExplosionObject.Trigger(self, None);
    }
}

function Remove()
{
    TriggerEffectEvent('Destroyed');

	Explode();

	if (FadeOut)
		StartFadeOut();
	else
		Destroy();
}

function SpawnChildren()
{
    local int i;

    for (i=0; i<Children.Length; i++)
        SpawnChild(i);
}


function SpawnChild(int Index)
{
	local DynamicObject Child;

	local String SocketName;
	local Vector SocketPosition;
	local Rotator SocketRotation;
	local Vector SocketScale;

	local String ChildName;
    
	local Vector Impulse;
	
	if (Children[Index] == None)
		return;

    GetChildSocketName(Index, SocketName);
    
	if (GetScalabilitySetting() == DS_NoDynamics && !Children[Index].default.Stationary)
		return;
	
    ChildName = Label$"_"$Index;

    if (getSocket(SocketName, SocketPosition, SocketRotation, SocketScale))
        Child = new(Outer) Children[Index](Self, Name(ChildName), SocketPosition, SocketRotation, DrawScale * SocketScale.X);
    else
        Child = new(Outer) Children[Index](Self, Name(ChildName), Location, Rotation, DrawScale);

	Child.bClientHavokPhysics = bClientHavokPhysics;

	if (Child.Stationary || Freeze)
	{
	    Child.setPhysics(PHYS_None);
	}
	else if (Explosion)
	{
	    Impulse = Child.unifiedGetComPosition() - unifiedGetComPosition();
	    
	    if (Impulse==vect(0,0,0))
	        impulse = VRand();
		
		Impulse += vect(0,0,0.5);
		
		Impulse = Normal(Impulse);
		
		Impulse *= (FRand()*0.25 + 1.0) * ExplosionForce;

		Child.HavokImpartImpulse(Impulse, Child.unifiedGetCOMPosition() + VRand()*5);
	}
}

// scalability queries of main level config
native function EScalabilitySetting GetScalabilitySetting();

function Died(Controller Killer, class<DamageType> damageType, vector HitLocation)
{
	if(Killer != None && Killer.Pawn != None)
		dispatchMessage(new class'MessageDestroyed'(Killer.Pawn.label, label));
	else
		dispatchMessage(new class'MessageDestroyed'(label, label));
}

// rowan: had to override this, as we set bSkipEncroachment as an optimisation, but still want to take damage form projectiles (base class returns !bSkipEncroachment)
simulated event bool ShouldProjectileHit(Actor projInstigator)
{
	return bProjTarget;
}

function PostTakeDamage(float Damage, Pawn EventInstigator, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional float projectileFactor)
{
	local Controller Killer;
	Killer = None;
	if (EventInstigator != None)
		Killer = EventInstigator.Controller;

	if (!Stationary) 
	    HavokImpartImpulse(Momentum, HitLocation);

	if (Invulnerable)
		return;

    if (Volatile)
    {
        NextVersion();

		// post destroyed message
		Died(Killer, damageType, HitLocation);
    }
    else if (Health>0)
	{
		Health -= Damage;

		if (Health<=0) 
		{
			NextVersion();

			// post destroyed message
			Died(Killer, damageType, HitLocation);
		}
	}
}


function CreateAlphaStyle()
{
	local int i;

	if (Skins.Length==0) 
        LoadSkinsFromMaterials();

	if (!StyleCreated)
	{
		for(i=0; i<Skins.Length; i++)
		{
			if (Skins[i] == none)
				break;

			if (!StyleCreated)
				Style[i] = new(none) class'ColorModifier'();

			Style[i].Material = Skins[i];
			Style[i].Color.A = 255;

			Skins[i] = Style[i];
		}

		StyleCreated = true;
	}
}


function SetAlpha(byte Amount)
{
	local int i;

	if (StyleCreated)
	{
		for (i=0; i<Style.Length; i++)
		{
			Style[i].Color.A = Amount;
		}
	}
}    


function unifiedAddImpulse(Vector impulse)
{
	if (!Stationary)
        super.unifiedAddImpulse(impulse);
}


function unifiedAddImpulseAtPosition(Vector impulse, Vector position)
{
	if (!Stationary)
		super.unifiedAddImpulseAtPosition(impulse, position);
}


function unifiedAddForce(Vector force)
{
	if (!Stationary)
		super.unifiedAddForce(force);
}


function unifiedAddForceAtPosition(Vector force, Vector position)
{
	if (!Stationary)
		super.unifiedAddForceAtPosition(force, position);
}


function unifiedAddTorque(Vector torque)
{
	if (!Stationary)
		super.unifiedAddTorque(torque);
}


native function LoadSkinsFromMaterials();
native function Initialize(float Scale, StaticMesh Mesh);
native function GetChildSocketName(int Index, out String Name);
native function GetExplosionSocketName(int Index, out String Name);


defaultproperties
{
	// official properties

	Health = 100
	Mass = 50
	Elasticity = 0.3
	friction = 0.8
	LinearDamping = 0
	AngularDamping = 0.05

	Invulnerable = false
	Blocking = true
	Stationary = false
	Enabled = false
	Shadows = true
	Volatile = false
	Freeze = false
	FadeOut = false

	Explosion = true
	ExplosionForce = 10000

	LimitedLife = false
	LifeTime = 0

	// fade properties

	FadingOut = false
	FadeOutAlpha = 255
	FadeOutTime = 0
	FadeOutLength = 0.5

    // next version state

    NextVersionPending = false
	NextVersionCalled = false

	// actor properties
	bAcceptsProjectors=false	// dont want this on the majority of little meshes (big CPU waster). artists can turn on for specific larger meshes

    bNoDelete = false
	bActorShadows = true
	RemoteRole = ROLE_None
	StaticMesh = StaticMesh'PhysicsObjects.Cube'

	bNeedLifetimeEffectEvents = true
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: ne 5.9.2004 15:53:20.000 - Creation time: st 23.5.2018 00:10:43.172 - Created with UnCodeX