Reputation: 498
I'm making my enemies spawn based on a random number, the first way I wrote, there are times where the player can hide in one corner and nothing comes for a while making the game easy. The second way I wrote is slightly better with the fact the player can't sit still for too long but this means there is nothing spawning anywhere else on the screen which doesn't look right.
The first being:
public Weight createWeight() {
decider = Math.random() * 1;
// creates rocks randomly with the lowest chance for l, and the highest chance for m
if (decider <= 0.33) {
// small weight
return new WeightSmall(BitmapFactory.decodeResource(getResources(), R.drawable.weight_s), new Random().nextInt(screenWidth), -10);
} else if (decider <= 0.5 && decider > 0.33) {
// large weight
return new WeightLarge(BitmapFactory.decodeResource(getResources(), R.drawable.weight_l), new Random().nextInt(screenWidth), -10);
} else {
// medium weight
return new WeightMedium(BitmapFactory.decodeResource(getResources(), R.drawable.weight_m), new Random().nextInt(screenWidth), -10);
}
}
The second:
public Weight createWeight() {
decider = Math.random() * 1;
Random random = new Random();
spawnRange = random.nextInt((player.getX() + player.getWidth()) - (player.getX() - player.getWidth())) + (player.getX() - player.getWidth());
// creates rocks randomly with the lowest chance for l, and the highest chance for m
if (decider <= 0.33) {
// small weight
return new WeightSmall(BitmapFactory.decodeResource(getResources(), R.drawable.weight_s), spawnRange, -10);
} else if (decider <= 0.5 && decider > 0.33) {
// large weight
return new WeightLarge(BitmapFactory.decodeResource(getResources(), R.drawable.weight_l), spawnRange, -10);
} else {
// medium weight
return new WeightMedium(BitmapFactory.decodeResource(getResources(), R.drawable.weight_m), spawnRange, -10);
}
}
I'd like to progress the second method as it feels the most natural, I just need a chance of spawning them elsewhere on screen.
Upvotes: 0
Views: 679
Reputation: 1887
I think I get what you're saying. Basically the spawn position of the mobs, should be random, but a higher portion should be around the player.
Well, you're going to want to come up with an exponential equation to work out the spawn distance from the player. So, you want to take the player distance, and seed it with a random value. To get this seed you want to generate a random number, and then multiply it by itself. This gives you the distance from the player to spawn the mob within.
For example, if your playing square was 50x50
.
Player is at 0,0
.
Generate a random number from 0-5
, multiply by itself.
0-1 = within 1 block
1-2 = within 4 blocks
2-3 = within 9 blocks
3-4 = within 16 blocks
4-5 = within 25 blocks
This means you have a 60% chance of have the mobs spawn within the 40% of the playing area from where the player is.
You'll need to tweak the exact formula you use yourself, and obviously come up with a suitable playing area size.
This will actually work quite well, as it means if the player stands next to a wall, half the box is outside the playing area, in which case you would spawn the mob against the wall. So the mobs have a half-sized area to spawn, which will help prevent the player camping in a corner (which would give them a 1/4 sized spawn area).
Upvotes: 2
Reputation: 798
As pointed out, math.random
returns a value between 0 and 1. Multiplying that by 1 gives you the same value. Thus, the *1
is obsolete. What you can do is *100
and cast it as an int
, to give you a rounded whole number between 0 and 100.
Your spawnRange
is player.getX() - player.getWidth()
. If you properly named this, you got a whole load of issues. First, if player is at x=0
, you'll be trying to spawn stuff offscreen.
Second, the enemies will always spawn behind the player and can only spawn in the area that is as big as the player, so probably you can only spawn one enemy at a time with at most a few pixels between it and the player.
What you generally want, is to spawn enemies near a player, not actually in his face. If you also would like to spawn within the entire map, you need to apply the following math:
Spawnrange
is at minimum player.width
away from player's x
(preferably twice the width for some breathing room), to at most the highest x
position.
minimumDistance = player.getX()+player.getWidth();
Spawnrange = random.nextInt( screenXSize - minimumDistance ) ) + minimumDistance;
This spawns you a result within the screen's x
bounds, with at least a player's size away from the player's position. You can of course also incorporate the values that are behind the player by reversing the math.
On to the next problem: You want to spawn enemies away and close by. What I'd do, is spawn two enemies at once: one close by, one far away. One from behind, the other from the front. Use the random function to determine if it'll be close or far, and then apply the proper random.nextInt()
.
Upvotes: 0