您的位置:首页 > 编程语言

photon游戏引擎代码例子2

2009-01-17 15:15 295 查看
该篇是介绍photon引擎中的Task类与其管理器类

代码如下:
//This file is part of g lib
#ifndef G_GOAL_TASK_HPP
#define G_GOAL_TASK_HPP
#ifndef _G_LIB_
#error please defie _G_LIB_
#endif
#include <string>
#include <boost/shared_ptr.hpp>
namespace g
{
namespace goal
{

// Title: Task
// Enum: PriorityLevel
// Enumeration defining priority of a Task.
enum PriorityLevel
{
PRI_HIGHEST = 5,
PRI_HIGH = 4,
PRI_NORMAL = 3,
PRI_LOW = 2,
PRI_LOWEST = 1
};
//  Class: Task
//  Abstract class for tasks
//  When writing a task, only update() needs to be overloaded.
class Task
{
public:
// Function: Task
//  Constructor, every task needs a name and priority.
Task(const std::string& name, PriorityLevel priority=PRI_NORMAL);
virtual ~Task();
public:
//  Function: update
//  Pure virtual, every child task must overload it's own update(), when a
//  task is active this is called every 'frame.'
virtual void update(float timeDelta)=0;

// Function: onStart
// Virtual function, overload to define behavior when the task is started.
virtual void onStart();

// Function: onKill
// Virtual function, overload to define behavior when the task is killed.
virtual void onKill();

// Function: onPause
// Virtual function, overload to define behavior every time that the
// task is paused.
//
// Note:
// Children of onPause should call Task::onPause to let the task know it's
// been paused.
virtual void onPause();

// Function: onUnpause
//  Virtual function, overload to define behavior every time that the
//  task is unpaused.
//
// Note:
//  Children of onUnpause should call Task::onUnpause to let the task know
//  it's been paused.
virtual void onUnpause();

// Function: kill
//  Sets state of task to dead, dead tasks remove themselves from the
//  <TaskManager>'s task pool.
void kill();
public:
// Function: getName
//  Get the name of the task.
std::string getName() const;
// Function: getPriority
//  Get the priority of the task.
short getPriority() const;
// Function: isAlive
//  Check if task is alive or not.
bool isAlive() const;
// Function: isPaused
//  Check if task is paused or not.
bool isPaused() const;
// data members
private:
std::string name_;          // all tasks need a unique name
PriorityLevel priority_;    // priority determines ordering of tasks
bool alive_;                // if false, task will be pruned
bool paused_;               // if false  task won't be executed
};
// Type: TaskPtr
//  Pointer to a task, used since Task is abstract and will always be accessed
//  via a pointer.
typedef boost::shared_ptr<Task> TaskPtr;
}
}
#endif  //G_GOAL_TASK_HPP


接下来是其管理器类了

如下

//This file is part of g lib
#ifndef G_GAOL_TASKMANAGER_HPP
#define G_GAOL_TASKMANAGER_HPP
#ifndef _G_LIB_
#error please add g lib
#endif
#include <list>
#include <algorithm>
#include "gsingleton.hpp"
#include "GTask.hpp"
namespace g
{
namespace goal
{
// Class: TaskManager
//  TaskManager class, maintains a list of <Tasks> and manages their status,
//  handles adding, deleting, pausing, and unpausing tasks.
//
//  To use TaskManager:
//      - (1) Add any tasks (should be derived from <Task>)
//      - (2) Call step() every frame when task should update.
class TaskManager
{
//typedef std::list<TaskPtr>::iterator it
public:
~TaskManager();
// Group: Running
public:
// Function: step
//  Steps the task manager, calling each active task once.
void step(float timeDelta);
// Group: Task Management
public:
// Function: addTask
//  Add a new <Task> to the TaskManager's list.
void pushTask(TaskPtr task);

// Function: killTask
//  Kill a task in the TaskManager task list.
void killTask(const std::string& taskName);

// Function: pauseTask
//  Pause a task in the TaskManager task list.
void pauseTask(const std::string& taskName);

// Function: unpauseTask
//  Unpause a task in the TaskManager task list.
void unpauseTask(const std::string& taskName);
// Function: killAllTasks
void killAllTasks();
// data members
private:
TaskManager();
//singleton pattern.

DEFINE_SINGLETON_CLASS(TaskManager);
//stored list of tasks (stored in order of priority highest to lowest)
std::list<TaskPtr> tasks_;

//predicate for search
class TaskNameEq : public std::binary_function<TaskPtr, std::string, bool>
{
public:
bool operator()(const TaskPtr& lhs, const std::string& rhs) const;
};
};
typedef g::design_pattern::Singleton<TaskManager> GTaskManager;
}
}
#endif  //G_GOAL_TASKMANAGER_HPP
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐