[java] listing classes that inherit a particular class

Started by
10 comments, last by The_Neverending_Loop 12 years, 9 months ago

How are you going to use this HashMap? If you can show us how you plan to use this collection, we'd be in a better place to help you build it.


Initially, it would be used during terrain generation when determining which resources appear on which map tiles. So, I'd iterate through the list of resources for each terrain tile to check to see which one was present at a given location. Here's some pseudo-Java-code:

HashMap resources = new HashMap();
// load all the resource object types into this array

terrain_block tb[][] = new terrain_block[100][100];

for (x = 0; x < 100; x++) {
for (y = 0; y < 100; y++) {
tb[x][y] = new terrain_block();

foreach(resources as r) {
if (resource.exists_at(x, y)) {
tb[x][y].resource = r;
break;
}
}
}
}
Advertisement

why do u want to write such ugly code?
can u describe to us what u want to achieve with this?


I agree that there is more than likely a better way to achieve what he wants, but I dont think thats we should approach him in that manner as it comes off as rude.

@TimmerCA there are better way to implement something like this, I dont know your code so I wont be able to give you more then a very abstract suggestion. Not saying whatever you had in mind wont work, but there are more elegant ways to go about it, and why not be elegant when possible? itll make your code more readible and itll be easier to work with.

As for my suggestion.

When I come across something like this I always have some kind of Event/Trigger Packages. These Events and Triggers I program to contain some of the common actions and events that can happen during the game.

So i might have an interface called Event, which has a method called execute() which will execute whatever Event Inherits it, so that way for ANY event that happens they can all be execute the same way with the event class

after that I would Inhert the Event Interface into all the kind of events for example

DealDamageEvent
ChangeGraphicEvent

this are just some abtract ideas and names. Anyhow so basically we will have one class that checks for the triggers (conditions for the event to happen) and once it sees that a trigger has been pushed, then it executes its attached event.

So I might have something like this (psuedo style).....

ExplodingIronOre.attachEvent("on_destruction", new DealDamageEvent(50_HITPOINTS)); // I attach a DealDamageEvent to the Exploding Iron Ore that is told to deal
........

if(Player.Destroys(ExplodingIronOre)) // We check to see if the player destroyed the Exploding Ore Object we attached the event to
{
ExplodingIronOre.trigger("on_destruction", Player); // If they did destroy it lets execute the "on_destruction" Event, (If any were attached)
}
.......


ExplodingIronOre.trigger(String trigger_name, Entity focus)
{
.....
getEvent(trigger_name).execute(Entity); // one of the overloads of the execute class might allow you to specify a specific entity to execute the commands on
// in this case we are focusing it on the player who triggered the event
}


This is very abstract and I Hope you can kind of understand my methodology, I hope this was usefull. If you are interested more in this kind of design but are a little confused on how to implement its, Id be more then happy to help you If you PM me. Best of luck to you and your project.

This topic is closed to new replies.

Advertisement