Holy Exploding Planets Batman!

edited November 2018 in EmptyEpsilon
Greetings all!

I've still been tinkering with adding elements to the environment, and have something new I wanted to share with the community. And if you read the title, you probably already guessed it -- exploding planets! Yes, there is already a very nice explosion visual available in the provided script library (thank you Daid!), but I wanted to take that up a level and add planetary "chunks" that explode outward in a randomized pattern and velocity. This is the basic set up to do just that.

I've commented the code and all the variables should be self-explanatory. There's also a little bit of console print statements for debugging purposes, but of course you don't need to keep these.

So run this on the server, and then access the player ship from a client and watch the explosion from the main screen and different positions. It's pretty neat! (You initiate the explosion countdown via the "Kaboom" button in the GM screen.)

Obviously, this is just the base set up. Next level … yup, supernovas! Bet you can't wait. ;]

Copy everything below into your script file, save, run, enjoy, repeat! ;]

-- Name: Exploding Planet Test ver 0.4
-- Description: Planets, exploding, in a test 
-- Type: Basic


function kaboom(exploding_planet_x_location, exploding_planet_y_location)

	-- set variable to initiate countdown of test planetary explosion
	countdown_active = true
	countdown_timer = 300 -- 60 'ticks' per second
	-- set up the initial multi-dimensional array to contain all the exploding chunks of the planet (asteroids)
	explosive_chunks = 250 -- careul here -- 500 seems to be about the max before server crashes; seems to handle 250 ok
	explosive_chunks_matrix = {}
	-- set up the min and max boundaries for exploding chunk sizes 
	-- a random value will be assigned from this range
	chunk_min_size = 50
	chunk_max_size = 750
	-- set up the min and max velocity boundaries for exploding chunks
	-- a random value will be assigned from this range
	chunk_min_velocity = 20
	chunk_max_velocity = 100

	for i=1,explosive_chunks do
		explosive_chunks_matrix[i] = {}
		-- for each verticle "record" in the multi-dimensional array, these are the items in the record:
		-- item 1:  an asteroid object (our 'exploding chunks'), with a randomly generated size, 
		-- initially set to the same location as the planet to explode
			:setSize(math.random(chunk_min_size, chunk_max_size))
			:setPosition(exploding_planet_x_location, exploding_planet_y_location)
		-- item 2:  the heading of the chunk as it explodes outward, randomly generated
		explosive_chunks_matrix[i][2]=math.random(1, 360)
		-- item 3:  the velocity of the chunk, randomly generated
		explosive_chunks_matrix[i][3]=math.random(chunk_min_velocity, chunk_max_velocity)
	print("Explosion matrix initiated with " .. explosive_chunks .. " chunks!")
	print(countdown_timer/60 .. " second countdown underway....")
	return countdown_active, countdown_timer, explosive_chunks, explosive_chunks_matrix

function init()

	print("\n","\n","Exploding Planet Test Scenario newly initiated")
	-- Spawn a player ship (Atlantis) (mostly just for seeing objects through the ship's forward view screens)
	player_ship = PlayerSpaceship()
		:setFaction("Human Navy")
		:setPosition(0, 20000)
	-- player_ship:setShieldsActive(true)
	countdown_active = false
	explosion_in_progress = false
	exploding_planet_x_location = 0
	exploding_planet_y_location = 0
	exploding_planet_radius = 2000
	-- very important!  set the distance from the explosion center you'd like the chunks (asteroids) to be 
	-- removed from the game, otherwise they'll continue on forever and forever.... ;]
	clean_up_distance = 100000
	exploding_planet = Planet()
		:setPosition(exploding_planet_x_location, exploding_planet_y_location)

	-- put in the button to activate the planet explosion countdown
			countdown_active, countdown_timer, explosive_chunks, explosive_chunks_matrix 
				= kaboom(exploding_planet_x_location, exploding_planet_y_location)
			remaining_number_of_chunks = explosive_chunks

function update(delta)

	if delta ~= 0 then

		-- Countdown first.....
		if countdown_active then
			countdown_timer = countdown_timer -1
			if countdown_timer == 0 then
				explosion_in_progress = true
				countdown_active = false
				local x, y = exploding_planet:getPosition()
				-- kill off the planet object
				-- replace the planet with an explosion effect:  
					:setSize(exploding_planet_radius * 5)
		if explosion_in_progress then
			-- iterate through the explosive_chunks_matrix and move chunks according to previously randomized heading and speed
			for i,chunk in ipairs(explosive_chunks_matrix) do
				if chunk[1]:isValid() then
					chunk_pos_x, chunk_pos_y = chunk[1]:getPosition()
					-- from utils.lua:  
					-- setCirclePos(obj, x, y, angle, distance)
					--   obj: An object.
					--   x, y: Origin coordinates.
					--   angle, distance: Relative heading and distance from the origin.
					-- also remember the array positions for each asteroid (chunk) in the matrix:
						-- index 1:  is the asteroid object
						-- index 2:  is the asteroid's heading
						-- index 3:  is the asteroid's speed
					setCirclePos(chunk[1], chunk_pos_x, chunk_pos_y, chunk[2], chunk[3])
					-- check to see if the chunk has gone beyond the established "clean_up_distance", and if so, remove from game
					-- and clean up the array
					if distance(chunk[1], exploding_planet_x_location, exploding_planet_y_location) > clean_up_distance then
						chunk[2] = nil
						chunk[3] = nil
						-- these next few lines print the remaining number of chunkc to the console
						-- (I need to find a more elegant way of calling an end to the explosion as some chunks
						-- are lost due to collision with ships and this number is not tracked at the moment)
						remaining_number_of_chunks = remaining_number_of_chunks - 1
						print("Remaining number of chunks = ", remaining_number_of_chunks)
						if remaining_number_of_chunks == 0 then 
							explosion_in_progress = false


Sign In or Register to comment.