Reputation: 127
I have a game in which blocks are added in a style similar to Minecraft. Lately, I've been running into a bit of a snag with these blocks.
There is one block called a power generator, and basically if it is on, and another block (light) is in the area, the light turns on. This is achieved through the following code, which is rather simple.
foreach (Block b in game.blphys.blocklist9)
{
foreach (Block v in game.blphys.blocklist7)
{
if (b.powerboxon == true && b.powerarea.Intersects(v.blockrectangle))
{
v.power = true;
}
else
{
v.power = false;
}
}
}
This has been working so far. However, since multiple generators can be added through listing, the moment I make a new power generator item the previously created power generator fails to work. The bounding box for the original generator is still there, and its still on (I have it change sprites while its on), it just looks like the code above stopped working for it.
Any help would be appreciated, I have a feeling I'm just being dumb about something
EDIT: Here is the code used while adding the block
if (game.player.Builder == true && game.player.LMBpressed == true && blockspawnamount >= placeblock)
{
if (game.build.BlockID == 10 && game.menu.open == false)
{
position = new Vector2((int)(game.cursor.cursorPos.X / 58) * 58, (int)(game.cursor.cursorPos.Y / 58) * 58);
game.blocktex9 = game.powerboxoff;
block9 = new Block(game, game.blocktex9, new Vector2(position.X, position.Y), layer);
blockpos9.Add(position);
blocklayer9.Add(layer);
blocklist9.Add(block9);
placeblock = 200.0f;
}
}
I tried the following
foreach (Block v in game.blphys.blocklist7)
{
foreach (Block b in game.blphys.blocklist9)
{
if (v.blockrectangle.Intersects(b.powerarea))
{
if (b.powerboxon == true)
{
v.power = true;
}
if (b.powerboxon == false)
{
v.power = false;
}
}
}
}
Which somewhat relieves my problems. Now, if there are multiple generators in the area, one remains off, one remains on, if you add more the lights turn off, but you can turn on one of the generators and it works.
Adding generators out of the area works perfectly too, once turned on, they supply power, providing they are far enough away from the other generators.
I might just add some code which prevents you from building another generator within the same power area.
Upvotes: 0
Views: 56
Reputation: 511
You are testing every power block with your light blocks and using that to set v.power
. This means that only the last power block you test will have an effect on the light block.
I'd recommend doing something like this instead:
iterate over light blocks
default power to false
iterate over power blocks
if power block is on and close to light block, set power = true and stop iterating power blocks
Upvotes: 2
Reputation: 1273
If the 2nd power generate is not near the 1st light, it will turn the power off. I suggest reversing the for each loops. If a light is near any power generator that is on, turn the light on and stop looking for power generators.
To explain further:
Upvotes: 1