Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Lighting and generating random objects.  (Read 1036 times)
0 Members and 1 Guest are viewing this topic.
Offline syrinth

Senior Newbie





« Posted 2011-10-03 17:32:12 »

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.



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:

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  
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 Smiley
Offline counterp

Senior Duke


Medals: 11



« Reply #1 - Posted 2011-10-04 01:25:35 »

1  
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
Offline dezzroy

Senior Newbie


Exp: 10 years



« Reply #2 - Posted 2011-10-04 05:57:10 »

Quote
Additionally, I want to be able to create random objects

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:
1  
final Class[] classesOfWeaponsThatCouldSpawnInA_TreasureChest = { Axe.class, Longsword.class, Quaterstaff.class };


Then, when the environment loads, for each treasure chest:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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() );
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (32 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!