Jump to content
  • Advertisement
Sign in to follow this  
MadHaTr

C# / XNA: Help with Object class and Draw methods

This topic is 3984 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

Here is my object class, for storing Airports and future objects.
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsGame3
{
    public class gameObject
    {
        public class Airport
        {
            public int gates;
            public int economy;
            public coord xy = new coord();
            public string name;
            public int population;
            public float growth;
            public int AirportNumber;

        }
        public class Attractions
        {
            public string name;
            public float mutator;
            
        }
        public class Airplane
        {

        }
    }

    public class coord
    {
        public int x;
        public int y;
    }

}




Here is the method I use to draw the various Airports to the World. Immediate problems can be seen. I'm going to have a lot more airports than just the two seen here and this is obviously a bad way of coding this. Once everything is said and done. I will have all the airport(objects) created from a text file. So users can add there own or remove some if they wish. My code will obvisouly brake in this event. I need help to create a better drawing method.
void drawAirports()
        {


            if (toronto.xy.x > CamLoc.X && toronto.xy.x < (CamLoc.Width+CamLoc.X) && 
                toronto.xy.y > CamLoc.Y && toronto.xy.y < (CamLoc.Height+CamLoc.Y))
            { x = toronto.xy.x - CamLoc.X; y = toronto.xy.y - CamLoc.Y; draw = true; }
            else draw = false;
            if (draw)
            {
                spriteBatch.Draw(airport, new Vector2(x, y), Color.White);
            }

            if (calgary.xy.x > CamLoc.X && calgary.xy.x < (CamLoc.Width + CamLoc.X) &&
                calgary.xy.y > CamLoc.Y && calgary.xy.y < (CamLoc.Height + CamLoc.Y))
            {  x1 = calgary.xy.x - CamLoc.X; y1 = calgary.xy.y - CamLoc.Y; draw1 = true; }
            else draw1 = false;
            if (draw1)
            {
                spriteBatch.Draw(airport, new Vector2(x1, y1), Color.White);
                
            }    
            
        }




Thanks MadHaTr [Edited by - MadHaTr on January 15, 2008 1:15:42 AM]

Share this post


Link to post
Share on other sites
Advertisement
Well you can just have an array or List<airport> that holds all your airports. Then in the drawAirports() function, you iterate over the airports and draw the ones that are visible. I would also put the visibility test in a separate function for clarity.

Something like this:


// This is a member variable
List<airport> airports = new List<airport>();

void drawAirports()
{
Vector2 pos = new Vector2();
foreach (airport a in airports)
if (testVisibility(a, ref pos))
spriteBatch.Draw(a, pos, Color.White);
}

Share this post


Link to post
Share on other sites
As I was trying to use the advice you gave me I relized I can't quite figure out how to create the objects in the list. I'm getting all sorts of errors. I've looked around for the answers but can't quite find the same situation.

So i've started the list by aadding this line
public List<gameObject> gameObjects = new List<gameObject>();
which as you can see is a list of my class(posted earlier)
Following is where I placed this line of code.

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
SpriteBatch spriteBatch;
SpriteFont TypoDermic;
Texture2D worldMap,airport,bottomMenu;
KeyboardState ksKeyboardState;
Rectangle CamLoc = new Rectangle();
Rectangle viewSize = new Rectangle();
Rectangle mapSize = new Rectangle();
gameObject.Airport toronto = new gameObject.Airport(); // for testing, not really used
gameObject.Airport calgary = new gameObject.Airport(); // for testing, not really used

int speed = 8, x=0, y=0, x1 = 0, y1 = 0;
string CameraLocationX, CameraLocationY;
bool draw = true, draw1 = true;

public List<gameObject> gameObjects = new List<gameObject>();



public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}




I tried to create a function to add(create) airports into the list.
 
void testCreateAirports()
{
gameObjects.Add(chicago);

}



The funtion is called here(init funtion)

protected override void Initialize()
{
// TODO: Add your initialization logic here
ksKeyboardState = new KeyboardState();
CamLoc.Width = 800; CamLoc.X = 0;
CamLoc.Height = 600; CamLoc.Y = 0;
viewSize.X = 0; viewSize.Y = 0; viewSize.Width = 800; viewSize.Height = 600;
mapSize.X = 0; mapSize.Y = 0; mapSize.Width = 1600; mapSize.Height = 1200;

testCreateAirports();


toronto.gates = 1; toronto.economy = 500; toronto.xy.x = 900; toronto.xy.y = 100;
calgary.gates = 1; calgary.economy = 500; calgary.xy.x = 800; calgary.xy.y = 200;


this.IsMouseVisible = true;

base.Initialize();
}


Ive also tried
gameObjects.Add(gameObject.Airport chicago);
bunch of other variations of this. always getting errors. Mainly "Error 1 The name 'chicago' does not exist in the current context"

which is obvious because im trying to create a chicago airport in the list with this line. so using the list How do I create all the airports I want?

Share this post


Link to post
Share on other sites
First, you have created a list that holds objects of type 'gameObject', but you probably want a list that will hold airports (BTW, your gameObject class doesn't actually have any data or functionality. It acts more like a namespace. I suggest that you get rid of it and declare Airport, Attractions etc. inside the WindowsGame3 namespace).

Second, there is no variable called 'chicago' in your program, which is why you are getting the error (but even if there was, you would still get an error because the list holds gameObject objects and not Airport objects).

In general, you would use a list of airports like this (assuming you have taken the Airport class outside of the gameObject class):


List<airport> airports = new List<airport>();

void testList()
{
Airport a = new Airport(...);

// Set the airports attributes (gates, economy, name, ...)
// ...

// Add it to the list
airports.Add(a);
}
/source]

Share this post


Link to post
Share on other sites
Okay, thank you. The class will eventurally have some functionality, at the moment im just working on some basic workings of things, Like the lists.

I've changed my test function now, I relized that in order to add something to the list it all ready needed to be created. I though I could create on the fly using the add feature. so here is the new code
[source lang=c#"]
void testCreateAirports()
{
//This will turn into a loop eventurally with a file reader to import the airports
gameObject.Airport Chicago = new gameObject.Airport();

gameObjects.Add(Chicago);
}




So my question now, Airport is inside the gameObject class, so why when I do things this way, will the list not accept Chicago(gameObject.Airport) I tried type casting and that didn't work. What I am eventually working towards is one list for airports, airplanes, weather effects, and so on. SO this is why the list is of gameObject and not gameObject.Airport, all future objects will be within the gameObject class.


[Edited by - MadHaTr on January 15, 2008 11:32:55 AM]

Share this post


Link to post
Share on other sites
Quote:
Airport is inside the gameObject class, so why when I do things this way, will the list not accept Chicago(gameObject.Airport)


I'm not sure what you mean. Can you post the exact code?

Quote:
What I am eventually working towards is one list for airports, airplanes, weather effects, and so on. SO this is why the list is of gameObject and not gameObject.Airport, all future objects will be within the gameObject class.


You seem to think that a list that holds gameObjects should be able to hold any type that is declared inside of gameObject (Airport, Airplane, ...), but that is not right. Putting several class declarations inside a declaration for another class (or inside a namespace) doesn't create a "connection" between those types (at least not in the way you think). They are still different types and cannot be treated uniformly.

There is actually a way to achieve what you want, using inheritance and virtual functions, but before you explore this option, think whether you really want to do this. Ask yourself this - why do you want to have one list that holds all of these objects which don't have a lot in common? What advantage will this have over having a separate list for each object type?

(Well there are some advantages, but I want to hear your answer before going into that.)

Share this post


Link to post
Share on other sites
I think what you really want to do is make "gameobject" an abstract class. Then Airport can inherit from gameobject, and you can have a list of gameobjects

for example, heres the gameobject class from my game:


using System;
using System.Threading;
using System.Configuration;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Endurance_7
{
public abstract class gameObject
{
#region fields
public Boolean active = true; //if active is false, the object will be removed
public Vector3 position = new Vector3(0);
public Vector3 oldPosition = new Vector3(0);
public Vector3 velocity = new Vector3(0);
public float mass = 1f;
public float scaleFactor;
public Vector3 scale=Vector3.One;
public float facingDirection=0f;
public Matrix orientation=Matrix.Identity;
public bool hasMoved = false;

public enum ObjectType
{
simple,
weapon,
vehicle,
tank,
building,
human
}
public ObjectType objectType;

#endregion

public virtual void load()
{
//--load graphics content
}
public abstract void render(GraphicsDevice device);
public abstract void update(float elapsedTime);

public float distanceFromPlayer()
{
return (float)Vector3.Distance(position, playerManager.player.position);

}


}
}



Airport would then inherit from gameObject like this:

class airport: gameObject
{
}



You can then create a generic list of gameObjects

public List<gameObject> objects = new List<gameObject>();
objects.add(new airport());



the only tricky part of doing it like this is the fact that the only things that will be directly accessible through this list are the things declared in gameObject. Everything else only exists in the scope of the Airport class or whatever.

gameObject then becomes your base class for every object in the game.

this might get kind of complicated for you though (I'm guessing your a beginner right?)so you might just want to stick with a simple list of airports

Share this post


Link to post
Share on other sites
Here is the class, and I think i see what you mean, just because I have class Airport inside class gameObject, there not connected the way I though. I also don't really want to get into inheritance and what not.

Reason i though having everything in one class/list was to make it easy to change things and have things manipulated inside the class, I really don't know enough about this sort of thing to give you a better answer.


using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsGame3
{
public class gameObject
{
public class Airport // AIRPORT INSIDE GAME OBJECT
{
public int gates;
public int economy;
public coord xy = new coord();
public string name;
public int population;
public float growth;
public int AirportNumber;

}

}

public class coord
{
public int x;
public int y;
}

}



Okay so up there, i though having airport within gameObject made them all gameObjects. so in order to make it work the way I though I was making it work is to give airport in heritence to gameObject(sorry don't know how its worded)


Thank You,
MadHaTr

Share this post


Link to post
Share on other sites
Quote:
Okay so up there, i though having airport within gameObject made them all gameObjects. so in order to make it work the way I though I was making it work is to give airport in heritence to gameObject(sorry don't know how its worded)

yup the only way to do that is to have airport inherit gameObject like in my example.

But there is really no reason why you can't just have a list of airports

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!