object.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:16k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "global.h"
  2. #include "object.h"
  3. //-----------------------------------------------------------------------------
  4. // Name: 
  5. // Desc: 
  6. //-----------------------------------------------------------------------------
  7. CObject::CObject() {
  8. m_dwObjType = OBJTYPE_NONE;
  9. m_dwObjStat = OBJSTAT_UNUSED;
  10. m_nStatCounter = 0;
  11. m_nWidth = 0;
  12. m_nHeight = 0;
  13. m_nLeft = 0;
  14. m_nTop = 0;
  15. m_dwFrameType = 0;
  16. m_nTotalFrame = 0;
  17. m_nCurrentFrame = 0;
  18. m_dwPersistTime = 0;
  19. m_dwLastTime = 0;
  20. }
  21. //-----------------------------------------------------------------------------
  22. // Name: 
  23. // Desc: 
  24. //-----------------------------------------------------------------------------
  25. CObject::~CObject() {
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Name: 
  29. // Desc: 
  30. //-----------------------------------------------------------------------------
  31. RECT CObject::GetRect() {
  32. RECT rc;
  33. SetRect( &rc, m_nLeft, m_nTop, m_nLeft+m_nWidth, m_nTop+m_nHeight );
  34. return rc;
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Name: 
  38. // Desc: 
  39. //-----------------------------------------------------------------------------
  40. void CObject::NextFrame( DWORD nowtime ) {
  41. if ( nowtime > m_dwPersistTime + m_dwLastTime ) {
  42. INCSCOPE( m_nCurrentFrame, 0, m_nTotalFrame );
  43. m_dwLastTime = nowtime;
  44. }
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Name: 
  48. // Desc: 
  49. //-----------------------------------------------------------------------------
  50. CMobileObject::CMobileObject() {
  51. m_uiDirection = DIR_NONE;
  52. m_nMoveStep = 0;
  53. m_uiTeamType = TEAM_NEUTRAL;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Name: 
  57. // Desc: 
  58. //-----------------------------------------------------------------------------
  59. CMobileObject::~CMobileObject() {
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Name: 
  63. // Desc: 
  64. //-----------------------------------------------------------------------------
  65. RECT CMobileObject::GetNextRect() {
  66. RECT rc = this->GetRect();
  67. POINT pt = this->GetDirectionUnit();
  68. rc.left = m_nLeft + m_nMoveStep * pt.x;
  69. rc.top = m_nTop + m_nMoveStep * pt.y;
  70. rc.right = rc.left + m_nWidth;
  71. rc.bottom = rc.top + m_nHeight;
  72. return rc;
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Name: 
  76. // Desc: 
  77. //-----------------------------------------------------------------------------
  78. RECT CMobileObject::GetNextRectStep( int step ) {
  79. RECT rc = this->GetRect();
  80. POINT pt = this->GetDirectionUnit();
  81. rc.left = m_nLeft + step * pt.x;
  82. rc.top = m_nTop + step * pt.y;
  83. rc.right = rc.left + m_nWidth;
  84. rc.bottom = rc.top + m_nHeight;
  85. return rc;
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Name: 
  89. // Desc: 
  90. //-----------------------------------------------------------------------------
  91. POINT CMobileObject::GetDirectionUnit() {
  92. POINT pt = {0,0};
  93. if ( m_uiDirection == DIR_RIGHT ) // right 
  94. pt.x = 1;
  95. else if ( m_uiDirection == DIR_LEFT ) // left
  96. pt.x = -1;
  97. else if ( m_uiDirection == DIR_UP )  // up
  98. pt.y = -1;
  99. else if ( m_uiDirection == DIR_DOWN )  // down
  100. pt.y = 1;
  101. return pt;
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Name: 
  105. // Desc: 
  106. //-----------------------------------------------------------------------------
  107. CObjTank::CObjTank() {
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Name: 
  111. // Desc: 
  112. //-----------------------------------------------------------------------------
  113. CObjTank::~CObjTank() {
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Name: 
  117. // Desc: 
  118. //-----------------------------------------------------------------------------
  119. void CObjTank::CreateTank( int left, int top, bool bHome ) {
  120. m_dwObjType = OBJTYPE_TANK;
  121. m_dwObjStat = OBJSTAT_LIVE;
  122. m_dwEquip = 0;
  123. m_nWidth = 32;
  124. m_nHeight = 32;
  125. m_nLeft = left;
  126. m_nTop = top;
  127. m_dwFrameType = LOC_OBJECT_TANK0;
  128. m_nTotalFrame = 2;
  129. m_nCurrentFrame = 0;
  130. m_dwPersistTime = 0;
  131. m_uiDirection = DIR_UP;
  132. m_nMoveStep = DEFTANKSPEED;
  133. m_bMoving = false;
  134. m_bOnIce = false;
  135. m_nIceStep = 0;
  136. m_uiIceDir = DIR_NONE;
  137. m_nIndex = 0;
  138. m_bHome = bHome; 
  139. if ( m_bHome )
  140. m_uiTeamType = TEAM_HOME;
  141. else
  142. m_uiTeamType = TEAM_ENEMY;
  143. m_bBoom = false;
  144. m_bHaveBullet = false;
  145. m_prtBlood.CreateProperty( 30, 50, 0 );
  146. m_nAttack = 10;
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Name: 
  150. // Desc: 
  151. //-----------------------------------------------------------------------------
  152. void CObjTank::Hited( int attack ) {
  153. if ( !m_prtBlood.Minus( attack ) ) {
  154. m_nStatCounter = 2;
  155. m_dwObjStat = OBJSTAT_DIE;
  156. m_nTotalFrame = 2;
  157. m_nCurrentFrame = 0;
  158. // m_nPersistTime = 100;
  159. }
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Name: 
  163. // Desc: 
  164. //-----------------------------------------------------------------------------
  165. void CObjTank::NextStat() {
  166. if ( m_dwObjStat == OBJSTAT_DIE ) {
  167. if ( --m_nStatCounter == 0 )
  168. m_dwObjStat = OBJSTAT_CADAVER;
  169. }
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Name: 
  173. // Desc: 
  174. //-----------------------------------------------------------------------------
  175. RECT CObjTank::GetNextRectOnIce() {
  176. RECT rc = this->GetRect();
  177. POINT pt = this->GetDirUnitOnIce();
  178. rc.left = m_nLeft + m_nIceStep * pt.x;
  179. rc.top = m_nTop + m_nIceStep * pt.y;
  180. rc.right = rc.left + m_nWidth;
  181. rc.bottom = rc.top + m_nHeight;
  182. return rc;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Name: 
  186. // Desc: 
  187. //-----------------------------------------------------------------------------
  188. POINT CObjTank::GetDirUnitOnIce() {
  189. POINT pt = {0,0};
  190. if ( m_uiIceDir == DIR_RIGHT ) // right 
  191. pt.x = 1;
  192. else if ( m_uiIceDir == DIR_LEFT ) // left
  193. pt.x = -1;
  194. else if ( m_uiIceDir == DIR_UP )  // up
  195. pt.y = -1;
  196. else if ( m_uiIceDir == DIR_DOWN )  // down
  197. pt.y = 1;
  198. return pt;
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Name: 
  202. // Desc: 
  203. //-----------------------------------------------------------------------------
  204. CObjBullet::CObjBullet() {
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Name: 
  208. // Desc: 
  209. //-----------------------------------------------------------------------------
  210. CObjBullet::~CObjBullet() {
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Name: 
  214. // Desc: 
  215. //-----------------------------------------------------------------------------
  216. void CObjBullet::CreateBullet( CObjTank *ptk )
  217. {
  218. m_dwFrameType = 0;
  219. m_nTotalFrame = 1;
  220. m_nCurrentFrame = 0;
  221. m_dwPersistTime = 0;
  222. m_nMoveStep = DEFBULLETSPEED;
  223. if ( ptk && ( ptk->GetStat() & OBJSTAT_LIVE )) {
  224. m_dwObjStat = OBJSTAT_LIVE;
  225. m_nLeft = ptk->GetLeft()+8+(ptk->GetDirectionUnit().x)*8;
  226. m_nTop = ptk->GetTop()+8+(ptk->GetDirectionUnit().y)*8;
  227. m_uiDirection = ptk->m_uiDirection;
  228. m_bHome = ptk->IsHomeTeam();
  229. m_uiTeamType = ptk->m_uiTeamType;
  230. m_obkTank = ptk;
  231. ptk->SetBullet();
  232. m_nAttack = ptk->m_nAttack;
  233. }
  234. else {
  235. m_dwObjType = OBJTYPE_BULLET;
  236. m_nWidth = DEFBULLETWIDTH;
  237. m_nHeight = DEFBULLETHEIGHT;
  238. m_dwObjStat = OBJSTAT_CADAVER;
  239. m_nLeft = 0;
  240. m_nTop = 0;
  241. m_uiDirection = DIR_NONE;
  242. }
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Name: 
  246. // Desc: 
  247. //-----------------------------------------------------------------------------
  248. void CObjBullet::NextStat() {
  249. if ( m_dwObjStat & OBJSTAT_LIVE ) {
  250. m_nStatCounter = 2;
  251. m_dwObjStat = OBJSTAT_DIE;
  252. m_nTotalFrame = 2;
  253. m_nCurrentFrame = 0;
  254. // m_nPersistTime = 100;
  255. if ( m_obkTank->GetStat() & OBJSTAT_LIVE )
  256. m_obkTank->RemoveBullet();
  257. } else if ( m_dwObjStat == OBJSTAT_DIE ) {
  258. if ( --m_nStatCounter == 0 )
  259. m_dwObjStat = OBJSTAT_CADAVER;
  260. }
  261. }
  262. //-----------------------------------------------------------------------------
  263. // Name: 
  264. // Desc: 
  265. //-----------------------------------------------------------------------------
  266. void CObjBullet::Hit( CObjTank *ptk ) {
  267. NextStat();
  268. ptk->Hited( GetAttack() );
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Name: 
  272. // Desc: 
  273. //-----------------------------------------------------------------------------
  274. void CObjBullet::Hit( CObjBullet *pblt ) {
  275. NextStat();
  276. pblt->NextStat();
  277. }
  278. //-----------------------------------------------------------------------------
  279. // Name: 
  280. // Desc: 
  281. //-----------------------------------------------------------------------------
  282. CObjEffect::CObjEffect() {
  283. m_dwEffect = EFFECT_NONE;
  284. m_nAttack = 0;
  285. }
  286. //-----------------------------------------------------------------------------
  287. // Name: 
  288. // Desc: 
  289. //-----------------------------------------------------------------------------
  290. CObjEffect::~CObjEffect() {
  291. }
  292. //-----------------------------------------------------------------------------
  293. // Name: 
  294. // Desc: 
  295. //-----------------------------------------------------------------------------
  296. void CObjEffect::CreateEffect( CObject *pobj ) {
  297. DWORD type = pobj->GetType();
  298. m_dwObjStat = OBJSTAT_LIVE;
  299. if ( type == OBJTYPE_TANK ) {
  300. m_dwEffect = EFFECT_TANKBOOM;
  301. m_dwObjType = OBJTYPE_EFFECT;
  302. m_nLeft = pobj->GetLeft();
  303. m_nTop = pobj->GetTop();
  304. m_nWidth = g_FrameSmall;
  305. m_nHeight = g_FrameSmall;
  306. m_nTotalFrame = 3;
  307. m_nCurrentFrame = 0;
  308. m_dwPersistTime = 100;
  309. }
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Name: 
  313. // Desc: 
  314. //-----------------------------------------------------------------------------
  315. CObjBase::CObjBase() {
  316. }
  317. //-----------------------------------------------------------------------------
  318. // Name: 
  319. // Desc: 
  320. //-----------------------------------------------------------------------------
  321. CObjBase::~CObjBase() {
  322. }
  323. //-----------------------------------------------------------------------------
  324. // Name: 
  325. // Desc: 
  326. //-----------------------------------------------------------------------------
  327. void CObjBase::CreateBase( int x, int y, bool bHome ) {
  328. m_bHome = bHome;
  329. if ( m_bHome )
  330. m_uiTeamType = TEAM_HOME;
  331. else
  332. m_uiTeamType = TEAM_ENEMY;
  333. m_dwObjType = OBJTYPE_BASE;
  334. m_dwObjStat = OBJSTAT_LIVE;
  335. m_nWidth = 32;
  336. m_nHeight = 32;
  337. m_nLeft = x;
  338. m_nTop = y;
  339. m_nMoveStep = 0;
  340. m_uiDirection = DIR_NONE;
  341. m_ptyBlood.CreateProperty( 40, 40, 0 );
  342. }
  343. //-----------------------------------------------------------------------------
  344. // Name: 
  345. // Desc: 
  346. //-----------------------------------------------------------------------------
  347. RECT CObjBase::GetSurroundRect() {
  348. RECT rt = GetRect();
  349. rt.left -= 16;
  350. rt.right += 16;
  351. rt.top -= 16;
  352. rt.bottom += 16;
  353. if ( rt.top < 0 )
  354. rt.top = 0;
  355. if ( rt.bottom > g_FrameHeight )
  356. rt.bottom = g_FrameHeight;
  357. return rt;
  358. }
  359. //-----------------------------------------------------------------------------
  360. // Name: 
  361. // Desc: 
  362. //-----------------------------------------------------------------------------
  363. CObjBlock::CObjBlock() {
  364. m_nbx = -1;
  365. m_nby = -1;
  366. m_nLand = LAND_NONE;
  367. }
  368. //-----------------------------------------------------------------------------
  369. // Name: 
  370. // Desc: 
  371. //-----------------------------------------------------------------------------
  372. void CObjBlock::CreateObjBlock( int x, int y, int land )
  373. {
  374. m_nbx = x;
  375. m_nby = y;
  376. m_nLand = land;
  377. m_dwObjType = OBJTYPE_BLOCK;
  378. m_dwObjStat = OBJSTAT_LIVE;
  379. m_nTotalFrame = 3;
  380. m_nCurrentFrame = 0;
  381. m_dwPersistTime = 200;
  382. m_nWidth = 32;
  383. m_nHeight = 32;
  384. m_nLeft = x<<5;
  385. m_nTop = y<<5;
  386. }
  387. //-----------------------------------------------------------------------------
  388. // Name: 
  389. // Desc: 
  390. //-----------------------------------------------------------------------------
  391. CObjBlock::~CObjBlock() {
  392. }
  393. //-----------------------------------------------------------------------------
  394. // Name: 
  395. // Desc: 
  396. //-----------------------------------------------------------------------------
  397. CObjFood::CObjFood() {
  398. m_dwObjType = OBJTYPE_FOOD;
  399. m_dwObjStat = OBJSTAT_LIVE;
  400. m_nTotalFrame = 0;
  401. m_nCurrentFrame = 0;
  402. m_dwPersistTime = 0;
  403. m_nWidth = 32;
  404. m_nHeight = 32;
  405. m_bPresent = false;
  406. m_bExsit = false;
  407. }
  408. //-----------------------------------------------------------------------------
  409. // Name: 
  410. // Desc: 
  411. //-----------------------------------------------------------------------------
  412. CObjFood::~CObjFood() {
  413. }
  414. //-----------------------------------------------------------------------------
  415. // Name: 
  416. // Desc: 
  417. //-----------------------------------------------------------------------------
  418. void CObjFood::CreateFood( DWORD type, int left, int top ) {
  419. m_bExsit = true;
  420. m_bPresent = true;
  421. m_dwType = type;
  422. m_nLeft = left;
  423. m_nTop = top;
  424. }
  425. //-----------------------------------------------------------------------------
  426. // Name: 
  427. // Desc: 
  428. //-----------------------------------------------------------------------------
  429. RECT CObjFood::GetPermitRect() {
  430. RECT rc;
  431. SetRect( &rc, m_nLeft+8, m_nTop+8, m_nLeft+m_nWidth-8, m_nTop+m_nHeight-8 );
  432. return rc;
  433. }
  434. //-----------------------------------------------------------------------------
  435. // Name: 
  436. // Desc: 
  437. //-----------------------------------------------------------------------------
  438. void CObjFood::Benefit( CObjTank *ptk ) {
  439. switch ( m_dwType ) {
  440. case FOOD_SHIP:
  441. ptk->m_dwEquip |= EQUIP_SHIP;
  442. break;
  443. case FOOD_POWERUP:
  444. ptk->m_nAttack += 10;
  445. break;
  446. case FOOD_BLOODUP:
  447. ptk->m_prtBlood.Add(10);
  448. break;
  449. case FOOD_SPEEDUP:
  450. ptk->SetStep( MIN( ptk->GetStep()+2, MAXTANKSPEED ) );
  451. break;
  452. }
  453. }
  454. //-----------------------------------------------------------------------------
  455. // Name: 
  456. // Desc: 
  457. //-----------------------------------------------------------------------------
  458. CProperty::CProperty() {
  459. m_nDefaultValue = 0;
  460. m_nCurrentValue = 0;
  461. m_nMaxValue = 0;
  462. m_nMinValue = 0;
  463. }
  464. //-----------------------------------------------------------------------------
  465. // Name: 
  466. // Desc: 
  467. //-----------------------------------------------------------------------------
  468. CProperty::~CProperty() {
  469. }
  470. //-----------------------------------------------------------------------------
  471. // Name: 
  472. // Desc: 
  473. //-----------------------------------------------------------------------------
  474. void CProperty::CreateProperty( int def, int max, int min ) {
  475. m_nDefaultValue = def;
  476. m_nCurrentValue = def;
  477. m_nMaxValue = max;
  478. m_nMinValue = min;
  479. }
  480. //-----------------------------------------------------------------------------
  481. // Name: 
  482. // Desc: 
  483. //-----------------------------------------------------------------------------
  484. void CProperty::Add( int step ) {
  485. m_nCurrentValue += step;
  486. if ( m_nCurrentValue > m_nMaxValue ) 
  487. m_nCurrentValue = m_nMaxValue;
  488. }
  489. //-----------------------------------------------------------------------------
  490. // Name: 
  491. // Desc: 
  492. //-----------------------------------------------------------------------------
  493. bool CProperty::Minus( int step ) {
  494. m_nCurrentValue -= step;
  495. if ( m_nCurrentValue <= m_nMinValue ) {
  496. m_nCurrentValue = m_nMinValue;
  497. return false;
  498. }
  499. return true;
  500. }
  501. //-----------------------------------------------------------------------------
  502. // Name: 
  503. // Desc: 
  504. //-----------------------------------------------------------------------------
  505. //-----------------------------------------------------------------------------
  506. // Name: 
  507. // Desc: 
  508. //-----------------------------------------------------------------------------