Advertisement

Code inside Character Controller/Steering/ or inside Gameplay/Targeting System?

Started by March 24, 2014 05:21 PM
2 comments, last by Thorim 10 years, 7 months ago

I have a Steering class that has a pointer to a Pathfinder (nodes, goal target etc.).

Another class is the Character that have pointers to Input State and Input Axis.

I have tags for my actors (character, enemy etc.) and a scene that have queries like find all actors of type etc.

My question is: is better to put character/enemies behaviours explicity inside the Gameplay State/Objective class or should I go to the OOP side and increase the dependencies (put a pointer to InputState and Pathfuider in character). The OOP side doesn't make sense!

The "player" is just an actor with a energy bar and the enemy is just another actor with steering forces applied and etc.

Another problem is: the "player" can only jump if is on ground and now it has to be acess to the physics class.

My Gameplay State code:


void Gameplay::Update(Game* pGame, float dt) 
{
	Physics* pPhysics = pGame->pPhysics;
	Scene* pScene = pGame->pScene;

	pPhysics->Update(dt, pGame); //pGame is a abstract Event Listener
	pScene->Update(dt); //manages actors

	Core* pCore = pGame->pCore;
	InputAxis* pInputAxis = pCore->GetInputAxis();
	InputState* pInputState = pCore->GetInputState();

	std::vector<Actor*> characters;
	pScene->GetActorsOfType(MASK_CHARACTER, characters);

	for (size_t i = 0; i < characters.size(); ++i)
	{
		RigidBody* pCharacterBody = characters[i]->GetRigidBody();

		const glm::vec3& normal = pCharacterBody->orientationMatrix[2];
		const float speed = 100.0f;

		pCharacterBody->Rotate(-pInputAxis->y * dt, -pInputAxis->x * dt, 0.0f);

		if (pInputState->isUp)
		{
			pCharacterBody->ApplyForce(normal * speed * dt);
		}
		if (pInputState->isDown)
		{
			pCharacterBody->ApplyForce(-normal * speed * dt);
		}

		std::vector<Actor*> enemies;
		pScene->GetActorsOfType(MASK_ENEMY, enemies);

		for (size_t j = 0; j < enemies.size(); ++j)
		{
			RigidBody* pEnemyBody = enemies[j]->GetRigidBody();

			const glm::vec3& position = pEnemyBody->GetPosition();
			const glm::vec3& target = pCharacterBody->GetPosition();

			const glm::vec3& toTarget = target - position;
			float distance = glm::length(toTarget);

			const float maxDistance = 10.0f;

			if (distance > 0.0f && distance < maxDistance)
			{
				glm::vec3 normal = glm::normalize(toTarget);
				glm::vec3 force = normal * 10.0f;

				glm::mat4 lookAt = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f);
				glm::mat3 orientationMatrix = glm::transpose(glm::mat3(lookAt));

				glm::quat orientation = glm::toQuat(orientationMatrix);

				pEnemyBody->ApplyForce(force * dt);
				pEnemyBody->SetOrientation(orientation);
			}
		}
	}
}

Hi,

Neither! Is my suggestion...

Working on larger business applications, and stock systems etc. One of the many things that can make code difficult to read is having at all over the place, If I had an DeliveryNote class in my business application, I would not expect it to be able to print itself, email itself, just I wouldn't expect to print it, have it walk into the warehouse, find what it needs and deliver itself! It can be a touch confusing! I would expect a DeliveryNoteEmailer to know how to do that though...

Anyway... Those first few lines look promising,and then bam! Some very specific logic physics/input code in a "Game Play" class. It looks like your Physics, Scene class are just data structures yet have an update funcion?

I'd say there are a couple of issues with that, and wouldn't do it (unless it's a very small project, or not causing you any difficulty, premature optimization and all that.) In a larger project.

1) You spend a month on graphics, come back, where would you expect to find Player "Jumping" code? I'd generally want to find some kind of "physics" manager. The input for the jump would be in some kind of "player input" manager etc... It could be a bit hard to find, especially if GamePlay::Update() get's much bigger

2) It's going to be a lot harder to profile your code, fix bugs etc if it's all lumped together.

3) It's hard to maintain.

It seems you have half the solution though!

A very common pattern in game programming is a "Manager" pattern. So what we do is have a GamePlay class, that has many (I have 40/50 in my current project) Managers. (Little C# snippet of just a few declerations)


public NetworkManager NetworkManager { get; set; }
        public EventManager EventManager { get; set; }
        public GraphicsManager GraphicsManager { get; set; }
        public WindowManager WindowManager { get; set; }
        public GUIManager GUIManager { get; set; }
        public StateManager StateManager { get; set; }
        public LoginManager LoginManager { get; set; }
        public ChatManager ChatManager { get; set; }
        public PlayerManager PlayerManager { get; set; }
        public ShardManager ShardManager { get; set; }
        public CameraManager CameraManager { get; set; }
        public WorldRenderManager WorldRenderManager { get; set; }

AND A BASIC EXAMPLE OF THIS


class ShardManager : IGameManager, IPlayStateManager
    {
        public Shard Shard { get; set; }

        public ShardManager(GameCore core) : base(core)
        {

        }

        public void HandleNewWorld(IGameNetworkEvent<JoinShardResponse> rep)
        {
              /// So the event that get's passed is fired by some other manager
              /// who has done his logic, but knows maybe a few other managers
              /// need to know what happed. I.E. Play a graphic on player death etc.
        }
    }

Then what you can have is an Event Class, which then specific types of events can inherit from (or you could do this with some kind of Id, the principle is the same.)


class PlayerRequestJumpEvent : IGameEvent
    {
        
    }

What happens then is, out core "game" is responsible only for one thing! Making sure that Managers are updated, and events are passed to them.

The managers handle the logic, which means that we have definite separation of duties. The Physics Manager is responsible for physics.

So to really boil down to how this would work for you,

Out Input Manager would fire a PlayerRequestJumpEvent when the player presses spacebar (or whatever.)

The Physics Manager would get the Request, and perhaps apply a force to the object. That's all it needs to do.

And then in it's regular old Update, it can do collision detection etc....

It's a fairly standard pattern, helps keep larger code bases in check and comes into it's own when you need multi-frame events.

I think with your current pattern, there is a confusion between Data, State and Function. Once you separate these three code become much easier to read, and you tend to write less rambling functions. As I said right at the top, a "Player" to me is a data object, and from just seeing the file Player.h, I'd be thinking that. If I saw PlayerManager.h or PlayerJumpManager.h or PlayerWalking.h even, I'd look there for code pertaining to the "logic" of walking! Not input, or physics just the logic of walking.

As to your actual question: "or should I go to the OOP side"

I don't know, is it a big project? Are you an amazing C programmer, OOP is just one very nice way of handling this separation, there are others though (MVC pattern for one). I would say that, any competent Game Programmer even Business Programmer would understand your manager pattern. But it all comes down to how you need to complete your game.

If you were one of my Junior Developers, you would already have had a NoCommentsExeption object tied to your desk! smile.png

I hope these ramblings helped!

Advertisement

I appreciate your reply but I just adopt the "manager" thing when I need to manage the memory of an object. If creates a object on the heap the manager has to destroy/free all of them.

Another thing: varies very much from the design. I use the Actor/Component model. No Scene Graph at all.

I just solve that refactoring my Character class. Behaviour->Character->PCharacter (PC)->NPCharacter (NPC).

Personally I would like to treat actors like cars. They only have an interface to drive them(press gas to move forward and back, shift gears, open doors, turn wheels). The driver can be you, AI, someone over the internet, or your cat. When I use OOP I really try to mimic the real world so it seems to make more sense to me.

In my system my AI and Physics are completely separate the only thing the physics knows about the the actor is it's location and the vector of direction it wants to go, and what type of physics it is so it can process it as such. Really my physics uses the force and position and apply that to what type of physics it is (Example Physics see that cat wants to run [3,0] or 3 units a second down the X axis. The physics looks at the data in the physics (traction, current speed, slopes, whatever you want) and moves the cat to that new location) only sees if the intercepts anything and sends a message to the AI about what happens. So before this frame that Cat was running across the kitchen floor and at a vector of [-3,0] or pretty much opposite of [3,0]. The Cat AI sees the outside door is close and does a TurnKitty(180degreees); The cats new heading is now [1,0] and since we didn't change the cats speed it's new movement vector is [3,0], Now the cat is trying to run away from the glass door but physics looks up the surfaces of the cat and the floor and see there is little friction so the cats still continues going about [-2.8,0] into the glass door and stops abruptly. Physics sends a message to Cat it hit a hard surface at [-2.8 , 0] the AI processes the data and used MakeKittyHiss(); and the cat hisses in anger.

Usually I keep the physics data in a component in a actor so the AI can access the data but not mix it with AI data which is stored in a separate component. Example would be a spider would have much different physics then a mouse (since the spider can crawl up walls and mice can't) but both still require the same algorithm to find paths to food and other resources. So how I don't have any issues with this, but it might not work for you. Just wanted to help out!

This topic is closed to new replies.

Advertisement