Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Developing a Codeigniter game

I have a pretty reasonable amount of experience in PHP (around ~5 years of experience with some websites made and two browser games). This is the first time I’ve really messed around with a core engine instead of just developing a plugin or module for something like Joomla. I have an extensive amount of experience in other object oriented languages (such as Java).

My question is, I find myself developing the site much like a normal site, but I want it to be more flexible than that and I’m not sure I’m “doing it right”. In a way, I feel like I’m building half a CMS.

For example, I have the main page set up through a controller that controls what views are currently being displayed. The main view page looks like this:

<?php $this->load->view("headerview"); ?>
<?php $this->load->view($contentview); ?>
<?php $this->load->view("footerview"); ?>

in a view file itself (so I can dynamically load the contentview, the others don’t really change). This is my first question, since I do ALL the control through my main control file, is this correct?

My index function sets the mainview as the view to be loaded with variables for the “contentview” and “gameview”. When “contentview” is the game, it has a split pane, one showing the players stats and such, and the other showing whichever page the player is currently on (the merchant shop, in a battle, exploring the world, etc.). What determines what is being shown here is the “gamecontent” variable (also passed in the “main” controller).

<?php $this->load->view("menubar"); ?>
<?php $this->load->view($gamecontent); ?>

In short, all view handling is currently being done in the main controller, i.e. one single controller.

$data['title'] = "Experimental Page";
$data['maincontent'] = "gameview";
if($page == 1) {
    $data['gamecontent'] = "townview";
} else if ($page == 2) {
    $data['gamecontent'] = "merchantview";
} else if ($page == 3) {
    $data['gamecontent'] = "explorationview";
} else if ($page == 4) {
    $data['gamecontent'] = "hospitalview";
} else if ($page == 5) {
    $data['gamecontent'] = "fightview";
} else if ($page == 6) {
    $data['gamecontent'] = "adminview";
}

$this->load->helper(array('form','url'));
$this->load->library('form_validation');
$this->load->view('mainview', $data);

I haven’t started adding the fighting and shopping functions yet, but I can see this controller getting very large, very fast. This seems wrong to me, but somehow I can’t formulate how to properly arrange the core setup.

My goal is to (hopefully) split up the controllers so that each one handles a separate game function, like healing, fighting, exploring and so on, and is separated much like my models are (user, mobs, items, etc.).

Can anyone help me figure out how to properly arrange this project?

like image 802
Organiccat Avatar asked Dec 14 '10 00:12

Organiccat


1 Answers

I use the Symfony Event Dispatcher component to do something like this; the component can be installed without using the Symfony Framework, so you can use it with CodeIgniter, but there might already be something similar available to you; I don't know CodeIgniter.

The Event Dispatcher is a generic implementation of the Observer Pattern.

  • I have a list of modules which register themselves as observers for any actions which they handle
  • Each request passes through an action (e.g. "townview")
  • My controller just creates an Event with whatever the action was as the name; the Event Dispatcher system then finds any modules which claim to be able to handle that action and passes over control to them

This way the main controller doesn't have to change, no matter how many actions you have; it just creates an event and throws it out through the dispatcher; if it's not handled it throws a massive error (in DEV) or a nice one (in PROD).

This is especially useful in our application, where we have lots of optional components that can be switched on or off for different users / clients; no complex logic in the controller at all, I just don't register the modules if they aren't enabled for a client.

like image 123
El Yobo Avatar answered Nov 12 '22 23:11

El Yobo