Advertisement Jump to content
Sign in to follow this  
RLS0812

Am I Over Thinking This ?

This topic is 1780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been working on a parent class that will handle every interactive item that will be generated on the map, however I think I may be "over thinking" this a bit too much - or maybe I am not abstracting it enough.

 This class will handle trees, rocks, plants, and mining nodes - what do you think ?


public abstract class InteractiveObject {
	static String defaul1 = "Error";
	static String defaul2 = "E";
	static int defaul3 = -1;
	
	String name;
	String type;
	String inspect;
	String skill;
	String tool_required;
	int tool_quality;
	int tool_damage;
	int skill_level;
	int health;
	int regen;
	int experance;
	int rarity; // Chance to spawn on map ( x in 10000 )
	String[] state = new String[2];
	//LootNode loot;
	
	public InteractiveObject(){
		name = defaul1;
		type = defaul1;
		inspect = defaul1;
		skill = defaul1;
		tool_required = defaul1;
		tool_quality = defaul3;
		tool_damage = defaul3;
		skill_level = defaul3;
		health = defaul3;
		regen = defaul3;
		experance = defaul3;
		rarity = defaul3;
		state[0] = defaul2;
		state[1] = defaul2;
		//loot = new LootNode(defaul1);
	}
	
	public String getName(){return name;}
	public String getType(){return type;}
	public String getInspect(){return inspect;}
	public String getSkill(){return skill;}
	public String getToolRequired(){return tool_required;}
	public int getToolQuality(){return tool_quality;}
	public int getToolDamage(){return tool_damage;}
	public int getSkillLevel(){return skill_level;}
	public int getHealth(){return health;}
	public int getRegenerationRate(){return regen;}
	public int getExperance(){return experance;}
	public int getRarity(){return rarity;} 
	public String[] getStates(){return state;}
	
}

Share this post


Link to post
Share on other sites
Advertisement

Yes all those items can be interacted with by a player if they have the correct tool, and skill level.

 

 If you want to see something scary, here is the class that uses the parent class . I'm going to have to decide on how to detangle all this stuff. ( this is what my current map gen uses to produce a map )

import java.util.ArrayList;
import java.util.List;


public class EnvObjects{
	// Trees 
	InteractiveObject pine_tree;
	InteractiveObject oak_tree;
	List<InteractiveObject> tree_list = new ArrayList<InteractiveObject>();
	// Rocks 
	InteractiveObject sand_stone;
	InteractiveObject chert_stone;
	List<InteractiveObject> stone_list = new ArrayList<InteractiveObject>();
	// Mining Nodes 
	InteractiveObject copper_node;
	InteractiveObject iron_node;
	List<InteractiveObject> mining_node_list = new ArrayList<InteractiveObject>();
	// Plants
	InteractiveObject grass_plant;
	InteractiveObject mushroom_plant;
	List<InteractiveObject> plant_list = new ArrayList<InteractiveObject>();
	// \\


	public EnvObjects(){
		
		//
		//====
		//Trees
		//===
		//
		
		pine_tree = new InteractiveObject();
		pine_tree.name = "Pine Tree";
		pine_tree.type = "Tree";
		pine_tree.inspect = "A pine tree.";
		pine_tree.skill = "Wood Cutting";
		pine_tree.tool_required = "Axe";
		pine_tree.tool_quality = 1;
		pine_tree.tool_damage = 5;
		pine_tree.skill_level = 1;
		pine_tree.health = 100;
		pine_tree.regen = 300;
		pine_tree.experance = 5;
		pine_tree.current_state = 0;
		pine_tree.rarity = 100;
		pine_tree.state[0] = "T";
		pine_tree.state[1] = "t";
		tree_list.add(pine_tree); 
		
		oak_tree = new InteractiveObject();
		oak_tree.name = "Oak Tree";
		oak_tree.type = "Tree";
		oak_tree.inspect = "An oak tree.";
		oak_tree.skill = "Wood Cutting";
		oak_tree.tool_required = "Axe";
		oak_tree.tool_quality = 1;
		oak_tree.tool_damage = 5;
		oak_tree.skill_level = 1;
		oak_tree.health = 100;
		oak_tree.regen = 300;
		oak_tree.experance = 5;
		oak_tree.current_state = 0;
		oak_tree.rarity = 100;
		oak_tree.state[0] = "T";
		oak_tree.state[1] = "t";
		tree_list.add(oak_tree);
		
		//
		//====
		//Rocks
		//===
		//
			
		sand_stone = new InteractiveObject();
		sand_stone.name = "Sand Stone";
		sand_stone.type = "Rock";
		sand_stone.inspect = "A bit of sandstone.";
		sand_stone.skill = "Mining";
		sand_stone.tool_required = "Pick Axe";
		sand_stone.tool_quality = 1;
		sand_stone.tool_damage = 5;
		sand_stone.skill_level = 1;
		sand_stone.health = 100;
		sand_stone.regen = 300;
		sand_stone.experance = 5;
		sand_stone.current_state = 0;
		sand_stone.rarity = 900;
		sand_stone.state[0] = "R";
		sand_stone.state[1] = "r";
		stone_list.add(sand_stone);
		
		chert_stone = new InteractiveObject();
		chert_stone.name = "Chert";
		chert_stone.type = "Rock";
		chert_stone.inspect = "A bit of chert.";
		chert_stone.skill = "Mining";
		chert_stone.tool_required = "Pick Axe";
		chert_stone.tool_quality = 1;
		chert_stone.tool_damage = 5;
		chert_stone.skill_level = 1;
		chert_stone.health = 100;
		chert_stone.regen = 300;
		chert_stone.experance = 5;
		chert_stone.current_state = 0;
		chert_stone.rarity = 200;
		chert_stone.state[0] = "R";
		chert_stone.state[1] = "r";
		stone_list.add(chert_stone);
		
		//
		//====
		//Mining Nodes
		//===
		//
		
		copper_node = new InteractiveObject();
		copper_node.name = "Copper Node";
		copper_node.type = "Mining Node";
		copper_node.inspect = "I can get copper from here.";
		copper_node.skill = "Mining";
		copper_node.tool_required = "Pick Axe";
		copper_node.tool_quality = 1;
		copper_node.tool_damage = 5;
		copper_node.skill_level = 1;
		copper_node.health = 100;
		copper_node.regen = 300;
		copper_node.experance = 5;
		copper_node.current_state = 0;
		copper_node.rarity = 300;
		copper_node.state[0] = "M";
		copper_node.state[1] = "m";
		mining_node_list.add(copper_node);
		
		iron_node = new InteractiveObject();
		iron_node.name = "Iron Node";
		iron_node.type = "Mining Node";
		iron_node.inspect = "I can get iron from here.";
		iron_node.skill = "Mining";
		iron_node.tool_required = "Pick Axe";
		iron_node.tool_quality = 1;
		iron_node.tool_damage = 5;
		iron_node.skill_level = 1;
		iron_node.health = 100;
		iron_node.regen = 300;
		iron_node.experance = 5;
		iron_node.current_state = 0;
		iron_node.rarity = 400;
		iron_node.state[0] = "M";
		iron_node.state[1] = "m";
		mining_node_list.add(iron_node);
		
		//
		//====
		//Plants
		//===
		//
		
		grass_plant = new InteractiveObject();
		grass_plant.name = "Grass Tuff";
		grass_plant.type = "Plant";
		grass_plant.inspect = "Grass - Mooooo !.";
		grass_plant.skill = "Farming";
		grass_plant.tool_required = "Shears";
		grass_plant.tool_quality = 1;
		grass_plant.tool_damage = 5;
		grass_plant.skill_level = 1;
		grass_plant.health = 100;
		grass_plant.regen = 300;
		grass_plant.experance = 5;
		grass_plant.current_state = 0;
		grass_plant.rarity = 600;
		grass_plant.state[0] = "P";
		grass_plant.state[1] = "p";
		plant_list.add(grass_plant);
		
		mushroom_plant = new InteractiveObject();
		mushroom_plant.name = "Mushroom";
		mushroom_plant.type = "Plant";
		mushroom_plant.inspect = "Badger - Badger - Badger";
		mushroom_plant.skill = "Farming";
		mushroom_plant.tool_required = "Shears";
		mushroom_plant.tool_quality = 1;
		mushroom_plant.tool_damage = 5;
		mushroom_plant.skill_level = 1;
		mushroom_plant.health = 100;
		mushroom_plant.regen = 300;
		mushroom_plant.experance = 5;
		mushroom_plant.current_state = 0;
		mushroom_plant.rarity = 100;
		mushroom_plant.state[0] = "P";
		mushroom_plant.state[1] = "p";
		plant_list.add(mushroom_plant);
		
		//
		// End
		//

		
	}
	public void say(String x){
		System.out.println(x);
	}
	
	public List<InteractiveObject> getTrees(){ return tree_list;}
	public List<InteractiveObject> getRocks(){ return stone_list;}
	public List<InteractiveObject> getMiningNodes(){ return mining_node_list;}
	public List<InteractiveObject> getPlants(){ return plant_list;}
	
}
Edited by Shippou

Share this post


Link to post
Share on other sites

Well, I suppose hard-coding everything is an option.

Most games use a data-driven approach with map editors and tools to save the map data to a file and load files at runtime. They list all game objects and expose some data. There isn't really any special handling, just a big collection of objects. Your way works for small games.


The good thing is, storing all the data in some sort of structure is the first step towards using a data file instead of hard-coding everything. Hopefully, many of the methods in this game will just work on InteractiveObjects or whatever without needing to know where they came from. It's better than data hard-coded into an if or switch statement.

Share this post


Link to post
Share on other sites

you'll get 2 advantages from loading that initialization data from a file:

 

1. people other than the coders can change the data.

 

2. no re-compile required when you change the data.

 

the slick way to do it is to alt-tab out of the game, edit the data file, then use a menu pick in the game to re-load the data file without quitting the game. or better yet, build the data  file editor into the game itself - but this might be overkill, depending on the game and the complexity of the data.  constant values for object type definitions like you're dealing with can easily be handled with a simple text file.  for something more complex like a heightmap, an internal or external editor would probably be called for.

 

as for the "all-in-one"  objecttype struct, i often debate this issue myself when starting a new title.  all in one is appealing in its perceived simplicity, but can be inefficient in terms of memory usage, cache friendliness, etc. due to unused variables.

 

some unused variables are ok. but its usually better to divvy things up into groups with similar sets of variables.

 

composition and components can reduce / eliminate unused variables, by giving you multiple simple struct types to work with.

Share this post


Link to post
Share on other sites

and definitely use #defined constants or enums instead of strings where possible. much faster. note that enums may have some type checking overhead that #defined constants don't have.

Share this post


Link to post
Share on other sites

Your class will end up being much bigger than that. The class would end up being a  World Class in that case. 

 

You have the world, and then you have things in the world. Those things can be sectioned off into different classes. You have the class of Humans and the class of Fish, and the class of trees. Each class is, in its own respect, is very detailed. You have so many different kinds of trees, with various unique properties. 

 

Now, you have one class handling rocks and trees and plants and... get the point? 

 

If you break down your classes and use inheritance for certain things, then it will make adding to the class easier in the long run. Also, your classes would be more re-usable for future things. 

 

Perhaps you want trees in your next game, but not rocks?

Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!