📄 marsbug.h
字号:
// MarsBug.h: interface for the CMarsBug class.
//
// 本程序是《疯狂的火星虫—面向对象状态机实践指南》的演示程序
//
// 版权所有 (C) 2004 王咏武
// http://www.contextfree.net/wangyw/
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_MARSBUG_H__D79E1B90_672B_4076_A170_387F84343642__INCLUDED_)
#define AFX_MARSBUG_H__D79E1B90_672B_4076_A170_387F84343642__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "FSM.h"
// 定义状态类
DEFINE_STATE_CLASS(MarsBug)
DEFINE_STATE_CLASS(Alive)
DEFINE_STATE_CLASS(Baby)
DEFINE_STATE_CLASS(Mature)
DEFINE_STATE_CLASS(Strong)
DEFINE_STATE_CLASS(Normal)
DEFINE_STATE_CLASS(Weak)
DEFINE_STATE_CLASS(Old)
DEFINE_STATE_CLASS(Idle)
DEFINE_STATE_CLASS(Attack1)
DEFINE_STATE_CLASS(Attack2)
DEFINE_STATE_CLASS(Attack3)
DEFINE_STATE_CLASS(Attack4)
DEFINE_STATE_CLASS(Attack5)
DEFINE_STATE_CLASS(Die)
class CMarsBug : public CFSM
{
public:
// 定义事件
enum ENUM_EVENT
{
EVENT_IDLE,
EVENT_CAN_SEE,
EVENT_CAN_ATTACK
};
static const int RADIUS;
static const int MAX_LIFE;
static const int MAX_BABY_LIFE;
static const int MAX_MAX_LIFE;
static const int DISTANCE_CAN_SEE;
static const int DISTANCE_CAN_ATTACK;
static const int MAX_DISTANCE;
static const int CHASE_STEP;
static const int ESCAPE_STEP;
static const int WALK_STEP;
static const int ATTACK_DEC_LIFE;
static const int EAT_DEC_LIFE;
public:
CMarsBug(int x, int y, COLORREF c);
virtual ~CMarsBug() {}
void Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
CFSM::Serialize(ar);
ar << m_iMaxLife;
ar << m_iLife;
ar << m_iDieLife;
ar << m_iX;
ar << m_iY;
ar << m_Color;
}
else
{
// TODO: add loading code here
CFSM::Serialize(ar);
ar >> m_iMaxLife;
ar >> m_iLife;
ar >> m_iDieLife;
ar >> m_iX;
ar >> m_iY;
ar >> m_Color;
}
}
void Invalidate();
void OnDraw(CDC* pDC);
int GetX() const { return m_iX; }
int GetY() const { return m_iY; }
void SetX(int x) { m_iX = x; }
void SetY(int y) { m_iY = y; }
int GetLife() const { return m_iLife; }
void DecLife() { m_iLife -= ATTACK_DEC_LIFE; if (m_iLife < 0) m_iLife = 0; }
void EatDecLife() { m_iDieLife -= EAT_DEC_LIFE; if (m_iDieLife < 0) m_iDieLife = 0; }
bool True(WORD wParam, DWORD lParam) { return true; }
bool Void(WORD wParam, DWORD lParam) { return true; }
bool IsMature(WORD wParam, DWORD lParam);
bool IsOld(WORD wParam, DWORD lParam);
bool IsDie(WORD wParam, DWORD lParam);
bool IsCorpse_WantEat(WORD wParam, DWORD lParam);
bool IsCorpse_NotWantEat(WORD wParam, DWORD lParam);
bool IsEnemyDie(WORD wParam, DWORD lParam);
bool IsEnemyAlive(WORD wParam, DWORD lParam);
bool IsHealth(WORD wParam, DWORD lParam);
bool IsIll(WORD wParam, DWORD lParam);
bool IsStrongThanMe(WORD wParam, DWORD lParam);
bool IsWeakThanMe(WORD wParam, DWORD lParam);
bool IsOver(WORD wParam, DWORD lParam);
bool IsStrong(WORD wParam, DWORD lParam);
bool IsNormal(WORD wParam, DWORD lParam);
bool IsWeak(WORD wParam, DWORD lParam);
bool BabyEntryAction(WORD wParam, DWORD lParam);
bool DieEntryAction(WORD wParam, DWORD lParam);
bool Cure(WORD wParam, DWORD lParam);
bool Escape(WORD wParam, DWORD lParam);
bool Chase(WORD wParam, DWORD lParam);
bool Walk(WORD wParam, DWORD lParam);
bool Attack(WORD wParam, DWORD lParam);
bool Eat(WORD wParam, DWORD lParam);
bool Remove(WORD wParam, DWORD lParam);
bool WantSpawn(WORD wParam, DWORD lParam);
bool Spawn(WORD wParam, DWORD lParam);
protected:
int m_iMaxLife; // 最大生命值
int m_iLife; // 当前生命值
int m_iDieLife; // 死后的生命值
int m_iX; // 坐标
int m_iY; // 坐标
COLORREF m_Color; // 颜色
};
#endif // !defined(AFX_MARSBUG_H__D79E1B90_672B_4076_A170_387F84343642__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -