Lighting and generating random objects.

Hello all. I’m hoping you can all give me a hand with my game project again.

My game uses a grid of nodes to determine where everything is and for pathfinding. Each node is 20 by 20 pixels. I’m having an issue with lighting. As you can see in my screenshot, the wall in the upper left is visible to the player, even though it really shouldn’t be.

https://dl-web.dropbox.com/get/Public/screenshot.png?w=a8e9e862

I’ve been trying to figure out an efficient way to have the system check to see if the nodes are out of blocked but nothing has come up that works. I am currently using the following:

public void update(){
		for(int i = 0; i<width; i++){
			for(int j=0; j<height; j++){
				Node n = nodeMap[i][j];
				for(int k=0; k<player.getParty().size(); k++){
					if(n.getDistance(player.getParty().get(k).getNode()) > 160){
							n.setSight(false);
					}
					else{
						ArrayList<Node> temp = n.getNeighbours();
						int wallCheck = 0;
						for(int l=0; l<temp.size(); l++){
							if(!temp.get(l).getTraversible()){
								wallCheck++;
							}
						}
						if(wallCheck != temp.size()){
							n.setSight(true);
							break;
						}
						else{
							n.setSight(false);
						}
					}
				}
			}
		}
	}

This code is called during every update cycle, which I accept may be too many times since it only really needs to be called whenever the player moves. If a node has it’s inSight value set to false, then it is not drawn. Any ideas for how I can check if there are walls between the player characters and the nodes? Note WallCheck checks to see if any node is completely surrounded by walls, because if it is then there’s no point to drawing it.

Additionally, I want to be able to create random objects. For example, lets say I have a treasure chest and I want it to be filled with a random assortment of weapons. How can I do this? Currently, I have Items like Longsword and Quarterstaff extending Weapon which extends Item, but I need to use new in order to create these objects. How can I do this?

Many thanks :slight_smile:

n.setSight(false);

seems you’re changing the visibility of the same node for multiple players over and over again without really doing anything (i assume that the node is rendered after calling update() in which case the visibility will depend on the last player checked??? just guessing…)

and yeah, you really should get on updating only when the player moves, that’s kind of important

[quote]Additionally, I want to be able to create random objects
[/quote]
There is more than one way to do this.

If all the Weapon children classes have default constructors, you could… (warning: quick, dirty, untested code example)

make a white list of Weapons that could be spawned in a treasure chest:


final Class[] classesOfWeaponsThatCouldSpawnInA_TreasureChest = { Axe.class, Longsword.class, Quaterstaff.class };

Then, when the environment loads, for each treasure chest:


import java.lang.reflect.*;

// the following code should be called for each treasure chest

final int capacityOfChest = 3; // this should probably be defined in treasure chest's class definition

// randomly choose how many Weapons to spawn in this treaure chest
final int numberOfWeaponsToSpawn = (int)Math.rint( Math.random() * (double)capacityOfChest );

for( int i = 0; i < numberOfWeaponsToSpawn; ++i )
{
    // randomly choose which kind of Weapon to spawn
    final int classIndex = (int)( Math.random() * (double)classesOfWeaponsThatCouldSpawnInA_TreasureChest.length );

    Class weaponClassToSpawn = classesOfWeaponsThatCouldSpawnInA_TreasureChest[ classIndex ];

    // cast to Weapon because newInstance() returns type Object, and we assume addItem() requires Item or subclass instance
    treaureChestInstance.addItem( (Weapon)weaponClassToSpawn.getConstructor().newInstance() );
}