Imagine we have four different types of enemies and you want them to show with the following frequencies:
Thanks Raul, but your code is exactly the code i had now in my game. A table with desired percentages in game.
The problem is not do a percentage random spawn, the goal is a dynamic percentage random spawn.
A sort of SpawnManager who controls the actual percentage of enemys in screen and change frequency dinamically.
For example, i had 5 enemyType1 in screen, 2 enemyType2 in screen, 2 enemyType3 in screen and 1 enemyType4 in screen.
So i had:
enemyType1 = 50%
enemyType2 = 20%
enemyType3 = 20%
enemyType4 = 10%
So i want a code that looks every spawn time at screen (i had now enemy count and a spawn population number) and change dynamically the frequency of the enemy if the enemyType is populating too much the screen.
In this example, i want the enemyType1 to spawn at 20% frequency, but if enemyType2 goes crazy, i want to change this frequency "in-game"
How can i do this in code?
I am not sure if I understood correctly the problem, but I think what you need is to simply re-calculate the desired percentages of frequency and decide how often you want to do it.
This re-calculation could be done right before each new spawn or maybe invoke it every few seconds:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| -- initial values
local enemyType1Freq = 10
local enemyType2Freq = 15
local enemyType3Freq = 25
local enemyType4Freq = 50
local function recalculateSpawnFrequencies()
-- very rough calculation of new spawn frequency
local function getNewSpawnFrequency( typeCount, totalCount )
local currentPercentage = typeCount * 100 / totalCount
return 100 - currentPercentage
end
-- recalculate percentages
enemyType1Freq = getNewSpawnFrequency( enemyType1Count, totalEnemyCount )
enemyType2Freq = getNewSpawnFrequency( enemyType2Count, totalEnemyCount )
enemyType3Freq = getNewSpawnFrequency( enemyType3Count, totalEnemyCount )
enemyType4Freq = getNewSpawnFrequency( enemyType4Count, totalEnemyCount )
end
-- recalculate frequencies every 5 seconds
timer.performWithDelay(5000, recalculateSpawnFrequencies, 0) |
Maybe this will help you or other members:
####main.lua####
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| local found
local min
--Here I define a table
--You can use what value you want for you
local t={3, -200, -91, 7, 4}
--the main function
function main()
--with this function you will found the elemeen that you want to increase his value
local function increaseMe()
--first we compose a basic function that find the minimun value between 2 parameters a, b
--we use this function later
local function minim(a, b)
if a<b then
return a
else
return b
end
end
--we create a function here that look in a generic table to find the minimum element in it
--we use in it the minim function
local function foundMin(table1)
for i=1,#table1 do
found=table1[1]
for j=2,#table1 do
found=minim(found,table1[j])
end
return found
end
end
--here we apply this function to our table an print the minimum value from it
min=foundMin(t)
--this is the final result of the function increaseMe
print("The min value is :"..min)
end
--we load this function
increaseMe()
--after you found the minimum value , you look in the defined table to this value
for i=1,#t do
if min==t[i] then
--Here I suppose is what you are looking for
print ("You must increase the value of t["..i.."]= "..t[i].." with 1 unit")
end
end
end
main() |
views:1356 update:2011/10/22 9:46:13