--Golden Shine here. Here I've added glass shards as a response to ramming into glass objects. The Object is defined in the SOC section,
--And I've added frame D and E to SPR_WIN to use as the glass shards so we dont need an extra object for this.

freeslot("MT_WINZ") --First, I have to move MT_WIN's freeslot here because of load order priorities. 

--"s" is our actual glass object.
--"v" is whatever's colliding with it. Usually a player.
local function SHGlassShatter(s, v)

	if not (v and v.valid) --Is the collission object valid?
	or s.amshattered --Are we already shattered?
	or (v.type == s.type) --Is the object touching us not just another glass pane?
	return false --If we are, cancel collission code.
	end
	
	if v.player --Is the object a player?
	local p = v.player --If so, let's call them "p" from now on.
		
		--Spawn exactly 14 shards! Changing the "14" spawns more or less shards as desired.
		for d = 1,14
		--Now spawn these shards as if they're coming from the player object.
		--We'll use vanilla SRB2's MT_WOODDEBRIS object and edit that live to use glass shard sprites. No custom objects required.
			local shards = P_SpawnMobj(v.x+(P_RandomRange(-50,50)*v.scale), v.y+(P_RandomRange(-50,50)*v.scale), v.z+(P_RandomRange(5,90)*v.scale), MT_WOODDEBRIS)
			
			--If the object succesfully spawned in, time to edit it and give it properties.
			if shards and shards.valid 
			
			--Add these flags to make it bounce off walls and floors.
				shards.flags = $|MF_BOUNCE|MF_GRENADEBOUNCE|MF_NOCLIPTHING 
			--Thrust the shards forward in the player's angle, with a bit of RNG so they all scatter somewhat.
			--Messing with the "8" number adjusts the speed at which they fly.
			--Messing with the (-25,25) adjusts the RNG range.
				P_InstaThrust(shards, v.angle+(P_RandomRange(-25,25)*ANG1), p.speed+8*v.scale)
			--Vertical momentum calculations. Bit of RNG added for the scattering effect.
				P_SetObjectMomZ(shards, (P_RandomRange(-4,8)*v.scale)+v.momz, false)
			
			--Use this sprite. You can use SPR_PLAY to send players flying everywhere, basically.
				shards.sprite = SPR_WINZ
			--Spawn randomly at either frame D or E, with a 40% translucency flag added. TR_TRANS10 is 10% translucency, etc.
				shards.frame = (P_RandomRange(D,E))|TR_TRANS40
			--Finally, a random rotation angle for the sprite. Can be removed if not desired.
				shards.rollangle = P_RandomRange(0,359)*ANG1
			end		
		end
		if (p == displayplayer) --Only for the player currently on-screen, add a little screen shake for 4 tics.
			P_StartQuake(40*FRACUNIT, 4)
		end
		
	else --Other objects besides players can break the glass too.
		--This is just a slightly different version whenever other objects break the glass.
		-- Usually I'd optimize this, but I want to keep it simple for reading purposes.
		--Remove this whole section if you only want players to break the object.
		for d = 1,4
			local shards = P_SpawnMobj(v.x+(P_RandomRange(-50,50)*v.scale), v.y+(P_RandomRange(-50,50)*v.scale), v.z+(P_RandomRange(5,80)*v.scale), MT_WOODDEBRIS)
			if shards and shards.valid 
				shards.flags = $|MF_BOUNCE|MF_GRENADEBOUNCE|MF_NOCLIPTHING 
				P_InstaThrust(shards, v.angle+(P_RandomRange(-25,25)*ANG1), 11*v.scale) 
				shards.momx = $+v.momx
				shards.momy = $+v.momy
				P_SetObjectMomZ(shards, (P_RandomRange(-4,8)*v.scale)+v.momz, false) 	
				shards.sprite = SPR_WINZ
				shards.frame = (P_RandomRange(D,E))|TR_TRANS40
			end		
		end				
	end	

	--Play the glass broken sound
	S_StartSound(s, sfx_GLASS)
	--Add this value to signify we've shattered, and don't wanna shatter again.
	s.amshattered = true 
	return false --Finally, return false so the player doesn't physically get stopped by the glass.
	
end

--Usually I use MobjCollide and MobjMoveCollide hooks, but since this object uses MF_SPECIAL,
-- we'll call the TouchSpecial hook instead.
addHook("TouchSpecial", SHGlassShatter,  MT_WINZ)

--THE END!
--I've written these comments to help you mess with values so you can reuse this for other objects. Like vases, screens, even robots.
--Just copy&paste the LUA_GlassPhysics and change all MT_WIN values to MT_VASE, then mess with any other values as needed!

--If you want an enemy to gets its armor flung everywhere everytime when it dies
--simply replace "TouchSpecial" with "MobjDeath" and you're good.
--I left all the other applicable hooks commented out below to make this easy.

--addHook("MobjCollide", SHGlassShatter,  MT_WIN)
--addHook("MobjMoveCollide", SHGlassShatter,  MT_WIN)
--addHook("MobjDeath", SHGlassShatter,  MT_WIN)
