Author Topic: How to make unstable asteroids.  (Read 4334 times)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
How to make unstable asteroids.
« on: July 01, 2010, 09:31:38 PM »
So, w4tc asked if it was possible to make 'unstable' asteroids that had changing attributes. Yes, it is, but when s/he tried, it didn't work. So here's a template, in case my explanation was a little unclear. Everything between the two horizontal lines is what you need. Text in red is stuff that you need to make it work, but can be changed to fit your level, as long as it does roughly the same, like a while...do loop, or something.  Text in green is what affects how it works, like numbers. It is still necessary, but affects how it works, not if it works. Purple text is completely unnecessary for the code, but will help you understand it. Everything else is pretty much vital that it stays exactly as it is.



--This template only deals with the instability of the asteroid, and assumes you already know how to create asteroids. If you don't, then check out annikk.exe's Guide to making levels for Absolute Beginners.

--It also assumes that you have made four asteroids, that have ids of 0, 1, 2, and 3, although it will work with any number of asteroids. (Asteroid ids always start from 0.)


function LevelLogic()
--In this example, the energy of asteroid 2 changes to a random value between 0 and 1, roughly every second, and the speed of asteroid 0 changes to a random value between 0.5 and 0.75 roughly every 3 seconds.
Roid0SpeedUnstableTimer = GetGameTime() + 3
Roid2EnergyUnstableTimer = GetGameTime() + 1



while GameRunning() do
-- Everything from here until the phrase 'instability setup complete' must be inside a loop that will never stop until the level ends, such as while GameRunning() do...end.

if GetGameTime() >= Roid0SpeedUnstableTimer then
               GetAsteroid(0):SetSpeed(math.random(50, 75)/100)
                Roid0SpeedUnstableTimer = GetGameTime() +
3
end

if GetGameTime() >= Roid2EnergyUnstableTimer then
                GetAsteroid(2):SetEnergy(math.random())
                Roid2EnergyUnstableTimer = GetGameTime() + 1
end

--Instability setup complete.
coroutine.yield()
end


An explanation of the code is in the next post.
« Last Edit: July 02, 2010, 09:53:26 PM by Pilchard123 »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: How to make unstable asteroids.
« Reply #1 on: July 01, 2010, 09:31:55 PM »
CODE BREAKDOWN - Green text can be altered in your levels.

Roid0SpeedUnstableTimer = GetGameTime() + 3
Roid2EnergyUnstableTimer = GetGameTime() + 1

This sets up the timers for changing the attributes of the asteroids. The general formula is

Timervariablename = GetGameTime() + IntervalInSeconds. IntervalInSeconds can be randomized.

while GameRunning() do...end
Makes sure that the instability code is always running. You can change it if you know how(eg. If you want to make an asteroid that stabilizes all the others when captured), but to be on the safe side, only use while...do loops that will always be running, like while GameRunning() do, or while gamewon == 0, if you are following annikk's guide.


if GetGameTime() >= Roid0SpeedUnstableTimer then...end
Checks whether the timer has run out, and the asteroid should change.

                GetAsteroid(0):SetSpeed(...)
Picks which asteroid to change and which attribute to change.

                Roid0SpeedUnstableTimer = GetGameTime() + 3
Resets the timer for that attribute.


HOW TO USE math.random() IN LUA.
math.random() will return a value from 0-1.
If you want to specify a range, then it is more complicated. Because math.random() can only take integral arguments, you must do this. (It's quite ugly for such a simple outcome. F=ma ftw.) In this example, I will make it output numbers between 0.456 and 0.78912.


First, take the number with the most decimal places. (0.78912)
How many decimal places does it have? (5)
Multiply it by 10^LargestNumberOfDecimalPlaces (This would be 100000 in our example, and gives us an answer of 78912, an integer. math.random() understands this.)
Multiply the other number by 10^LargestNumberOfDecimalPlaces (45600)
Now put these in math.random() like so: math.random(smallestnumber, largestnumber). (math.random(45600, 78912))
Add '/10^LargestNumberOfDecimalPlaces, so you now have math.random(smallestnumber, largestnumber)/10^LargestNumberOfDecimalPlaces. (math.random(45600, 78912)/100000)
Put this in SetAttribute(here). (SetSpeed(math.random(45600, 78912)/1000000))

Job done. Yay.
« Last Edit: July 01, 2010, 10:00:42 PM by Pilchard123 »