Codename: Haunted Light – ObjectManager

This week we’ve been working hard on getting an alpha product done for the presentation that will be held in from of the class.

I’ve made some finishing touches on the light engine so that it e.g. no longer have transparent shadows. This was accomplished by using blend modes. I’m quite familiar with using blend-modes because of my previous projects when I used to program a lot inside the game engine Game Maker. But I’ve gotten more and more used to how SFML works now and it feels great!

But enough of that. Today I’m going to write about how Haunted Light handles all it’s different objects.

Haunted Light - ObjectManager

A screencapture of the class; “ObjectManager” which this post will cover.

First off, all objects inside Haunted Light except the player gets an ID assign to itself when it’s added to the ObjectManager class after it’s creation. This ID is a static int which has a starting value of 0. That value’s incremented by one each time it’s used to assign it’s value to an object. This prevents any object from having the same ID as another one.

Next up I’ll go through all the functions, arguments and member variables which the ObjectManager class contains and what each function’s purpose is.

Functions

  • ObjectManager()
  • void Add()
  • int getObjects()
  • bool setActiveDepth()
  • void Draw()
  • void CheckCollision()
  • int atPosition()
  • void destroy()
  • void Cleanup()

Member Variables

  • std::map m_objects
  • int m_max_z
  • int m_max_z
  • static int ID

How it all works

  • The function “ObjectManager()“, is the first function that’s called when the ObjectManager object gets created. It does not have any argument and it’s currenly only a placeholder if I want to have anything happen when the ObjectManager object’s created. This function is called a default contructor and makes it possible for the object to be created. Without it you wouldn’t be able to create an object out of a class.
  • The next funtion “Add()“, takes two arguments. The first one is a pointer named “_object” of the type GameObject which is the oject that you want to add (hence the name). The second one is of the type integer and is named”_depth“, this argument defines at what z-index i.e layer that the object will be drawn on.
  • getObjects()” is a simple function which returns the amount of objects that’s stored in the ObjectManager.
  • The “setActiveDepth()” function is an intresting one. This function takes two arguments of the type integer; “_min” and “_max“. Those two values define the range which the ObjectManager’s allowed to draw objects in. E.g. if you want to only draw the floors of the level which has a depth-value of 5 , then you’d use the “setActiveDepth()” function with the two arguments being “5″.
  • Draw()” is a simple function that only have one task; rendering all the objects which the ObjectManager contains.
  • The “CheckCollision()” function is a leftover from when we didn’t have a collision-manager, which we now do. And it’s no longer in use since all the collisions are handled by the CollisionManager class.
  • This useful function; “atPosition()” have one argument that’s the position on which you want to check if there is an object. If there is, then that objects ID is returned. If there’s no object at that particular position, then the special value; “-1” is returned.
  • If you want to have an object deleted, then this next function; “destroy()” is of great use. This function takes one argument of the type integer. This value is the object you want to delete’s ID. The function performs a linear search alghoritm and finds what object you want to have deleted and deletes it from the manager.
  • Since the ObjectManager is state-specific then you’d want it to clean up when it’s done. That’s what this function’s made for. When it’s called, it goes through the member variable; “m_objects” and deletes each object from memory.

Last week I covered our SpriteManager and now you also know how our object handling works. Next week I’ll cover another part of our development!

~Lead Programmer, Per (Gimmic) Johansson