GameWorld.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using CommonAI.RTS;
  8. using CommonAI.Zone;
  9. using CommonAI.Zone.Instance;
  10. using CommonAI.RTS.Manhattan;
  11. namespace CommonTest
  12. {
  13. public class GameWorld : GameBase, InstanceZoneListener
  14. {
  15. private ZoneInfo mZoneInfo;
  16. private int mWidth;
  17. private int mHeight;
  18. private int mCellW;
  19. private int mCellH;
  20. private int mGridXCount;
  21. private int mGridYCount;
  22. private SceneLOL zone;
  23. private Dictionary<int, GameObject> units = new Dictionary<int, GameObject>();
  24. private List<DisplayLog> logs = new List<DisplayLog>();
  25. private float timer;
  26. private long mLastUpdateTime;
  27. private int mFPS = 0;
  28. private GameWorld(InitZoneInfo zinf)
  29. {
  30. this.mZoneInfo = zinf.Info;
  31. this.mGridXCount = RTSMath.roundMod(mZoneInfo.TotalWidth, mZoneInfo.GridCellW);
  32. this.mGridYCount = RTSMath.roundMod(mZoneInfo.TotalHeight, mZoneInfo.GridCellH);
  33. this.mWidth = mZoneInfo.TotalWidth;
  34. this.mHeight = mZoneInfo.TotalHeight;
  35. this.mCellW = mZoneInfo.GridCellW;
  36. this.mCellH = mZoneInfo.GridCellH;
  37. this.zone = new SceneLOL(this, mZoneInfo, SpaceDIV);
  38. zinf.initGame(zone);
  39. }
  40. //-----------------------------------------------------------------------------------------------
  41. // instructions
  42. //-----------------------------------------------------------------------------------------------
  43. #region TEST_________________________________
  44. public static GameWorld create3C()
  45. {
  46. InitZoneInfo zinfo = test_InitZoneInfo(null);
  47. test_init3C(zinfo);
  48. GameWorld gw = new GameWorld(zinfo);
  49. return gw;
  50. }
  51. public static GameWorld create4A()
  52. {
  53. InitZoneInfo zinfo = test_InitZoneInfo(null);
  54. test_init4A(zinfo);
  55. GameWorld gw = new GameWorld(zinfo);
  56. return gw;
  57. }
  58. public static GameWorld createLOL(Bitmap mapmask)
  59. {
  60. InitZoneInfo zinfo = test_InitZoneInfo(mapmask);
  61. test_initLOL(zinfo);
  62. GameWorld gw = new GameWorld(zinfo);
  63. return gw;
  64. }
  65. public void test_AddActor(string name)
  66. {
  67. float x = random.Next(0, mWidth);
  68. float y = random.Next(0, mHeight);
  69. x = mWidth / 2;
  70. y = mHeight / 2;
  71. UnitInfo info = test_InitUnitInfo(SceneLOL.UNIT_FORCE_A, name, false, false);
  72. info.type = UnitInfo.UnitType.TYPE_PLAYER;
  73. info.force = SceneLOL.UNIT_FORCE_A;
  74. info.name = name;
  75. info.HealthPoint = 100000000;
  76. info.baseSkillID = SKILL_MELEE_ATTACK_4;
  77. //info.skills.Add(SKILL_ORBIT_CYCLE);
  78. /*
  79. info.skills.Add(SKILL_MELEE_ATTACK_4);
  80. info.skills.Add(SKILL_STRAIGHT);
  81. info.skills.Add(SKILL_AOE);
  82. info.skills.Add(SKILL_LASER);
  83. info.skills.Add(SKILL_BINDING);
  84. info.skills.Add(SKILL_MISSILE);
  85. info.skills.Add(SKILL_ACTOR_SLIP);
  86. info.skills.Add(SKILL_STRAIGHT_CLUSTER);
  87. */
  88. zone.pushAction(new AddUnitAction(info, x, y, 0));
  89. }
  90. public void test_AddUnit(string name)
  91. {
  92. int force = random.Next(0, 100) % 2;
  93. float x = 1;
  94. float y = 1;
  95. UnitInfo info = test_InitUnitInfo(force, name, random.Next()%2==0, false);
  96. info.type = UnitInfo.UnitType.TYPE_NPC;
  97. info.name = name;
  98. if (force == 0)
  99. {
  100. x = random.Next(0, mWidth / 2);
  101. y = random.Next(0, mHeight);
  102. info.force = SceneLOL.UNIT_FORCE_A;
  103. }
  104. else
  105. {
  106. x = random.Next(mWidth / 2, mWidth);
  107. y = random.Next(0, mHeight);
  108. info.force = SceneLOL.UNIT_FORCE_B;
  109. }
  110. zone.pushAction(new AddUnitAction(info, x, y, 0));
  111. }
  112. public void test_UnitMove(int unit_id, float x, float y)
  113. {
  114. zone.pushAction(new UnitMoveAction(unit_id, x, y, false));
  115. }
  116. public void test_UnitSlip(int unit_id, float x, float y)
  117. {
  118. zone.pushAction(new UnitSlipAction(unit_id, x, y));
  119. }
  120. public void test_UnitTransport(int unit_id, float x, float y)
  121. {
  122. zone.pushAction(new UnitTransportAction(unit_id, x, y));
  123. }
  124. public void test_UnitAttack(int unit_id, int skillID)
  125. {
  126. zone.pushAction(new UnitLaunchSkillAction(unit_id, skillID));
  127. }
  128. #endregion
  129. public int Width { get { return mWidth; } }
  130. public int Height { get { return mHeight; } }
  131. public int CellW { get { return mCellW; } }
  132. public int CellH { get { return mCellH; } }
  133. public int UnitCount { get { return units.Count; } }
  134. public int FPS { get { return mFPS; } }
  135. //-----------------------------------------------------------------------------------------------------------------
  136. // windows
  137. #region WINDOW
  138. private RectangleF mWindow = new RectangleF(0, 0, 512, 512);
  139. private float cameraX = 0;
  140. private float cameraY = 0;
  141. private float cameraScale = 1;
  142. public void setWindow(RectangleF window)
  143. {
  144. this.mWindow.X = window.X;
  145. this.mWindow.Y = window.Y;
  146. this.mWindow.Width = window.Width;
  147. this.mWindow.Height = window.Height;
  148. }
  149. public float screenToWorldSize(float s)
  150. {
  151. return s / cameraScale;
  152. }
  153. public float worldToScreenSize(float s)
  154. {
  155. return s * cameraScale;
  156. }
  157. public float screenToWorldX(float x)
  158. {
  159. return (x - mWindow.Width / 2) / cameraScale + cameraX;
  160. }
  161. public float screenToWorldY(float y)
  162. {
  163. return (y - mWindow.Height / 2) / cameraScale + cameraY;
  164. }
  165. public float worldToScreenX(float x)
  166. {
  167. return (x - cameraX) * cameraScale + mWindow.Width / 2;
  168. }
  169. public float worldToScreenY(float y)
  170. {
  171. return (y - cameraY) * cameraScale + mWindow.Height / 2;
  172. }
  173. public void setCamera(float x, float y)
  174. {
  175. this.cameraX = x;
  176. this.cameraY = y;
  177. }
  178. public void setCameraScale(float scale)
  179. {
  180. cameraScale = scale;
  181. cameraScale = Math.Max(cameraScale, 0.01f);
  182. }
  183. public float getCameraScale()
  184. {
  185. return cameraScale;
  186. }
  187. #endregion
  188. //-----------------------------------------------------------------------------------------------------------------
  189. #region RENDER
  190. public void renderWorld(Graphics g)
  191. {
  192. GraphicsState gs = g.Save();
  193. {
  194. g.TranslateTransform(
  195. mWindow.Width / 2,
  196. mWindow.Height / 2);
  197. g.ScaleTransform(
  198. cameraScale,
  199. cameraScale);
  200. g.TranslateTransform(
  201. -cameraX,
  202. -cameraY);
  203. renderWorld(g, new RectangleF(
  204. screenToWorldX(0),
  205. screenToWorldY(0),
  206. screenToWorldSize(mWindow.Width),
  207. screenToWorldSize(mWindow.Height)));
  208. }
  209. g.Restore(gs);
  210. }
  211. public void renderHUD(Graphics g, GameUnit unit)
  212. {
  213. Brush brush = new SolidBrush(Color.White);
  214. GraphicsState gs = g.Save();
  215. {
  216. g.DrawString("FPS=" + FPS + " TotalUnit=" + UnitCount,
  217. global_font, brush, 1, 1, StringFormat.GenericDefault);
  218. renderUnitStatusHUD(g, unit);
  219. }
  220. g.Restore(gs);
  221. }
  222. private void renderUnitStatusHUD(Graphics g, GameUnit unit)
  223. {
  224. if (unit != null)
  225. {
  226. Pen pen_B = new Pen(Color.Gray);
  227. Brush brush_C = new SolidBrush(Color.FromArgb(0xFF, 0x40, 0x40, 0x40));
  228. Brush brush_CD = new SolidBrush(Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF));
  229. Brush brush_T = new SolidBrush(Color.Blue);
  230. float sw = 50;
  231. float sh = 50;
  232. float sx = 0;
  233. float sy = mWindow.Height - sh;
  234. ICollection<GameUnit.SkillState> status = unit.getSkillStatus();
  235. foreach (GameUnit.SkillState ss in status)
  236. {
  237. float x = sx + 2;
  238. float y = sy + 2;
  239. float w = sw - 4;
  240. float h = sh - 4;
  241. g.SetClip(new RectangleF(x, y, w, h));
  242. g.FillRectangle(brush_C, x, y, w, h);
  243. g.DrawRectangle(pen_B, x, y, w-1, h-1);
  244. if (ss.isCD())
  245. {
  246. g.FillPie(brush_CD, x - w, y - h, w * 3, h * 3, -90, 360 * ss.getCDPercent());
  247. }
  248. g.DrawString("" + ss.Data.name, global_font, brush_T, x, y, StringFormat.GenericDefault);
  249. g.ResetClip();
  250. sx += sw;
  251. }
  252. }
  253. }
  254. private void renderWorld(Graphics g, RectangleF rect)
  255. {
  256. renderTerrain(g, rect);
  257. renderSpaceDiv(g, rect);
  258. renderUnits(g, rect);
  259. renderLogs(g);
  260. }
  261. private void renderTerrain(Graphics g, RectangleF rect)
  262. {
  263. int sx = (int)(rect.X / mCellW);
  264. int sy = (int)(rect.Y / mCellH);
  265. int sw = (int)(rect.Width / CellW) + 1;
  266. int sh = (int)(rect.Height / CellH) + 1;
  267. int lw = (int)(rect.Width + CellW * 2);
  268. int lh = (int)(rect.Height + CellH * 2);
  269. Brush bru_0 = new SolidBrush(Color.FromArgb(0xff, 0x20, 0x20, 0x20));
  270. Brush bru_1 = new SolidBrush(Color.FromArgb(0xff, 0x20, 0x40, 0x20));
  271. for (int x = sx + sw; x >= sx; --x)
  272. {
  273. for (int y = sy + sh; y >= sy; --y)
  274. {
  275. if (x < mGridXCount && x >= 0 && y < mGridYCount && y >= 0)
  276. {
  277. if (mZoneInfo.TerrainMatrix[x, y] != 0)
  278. {
  279. g.FillRectangle(bru_1, x * mCellW, y * mCellH, mCellW, mCellH);
  280. }
  281. else
  282. {
  283. g.FillRectangle(bru_0, x * mCellW, y * mCellH, mCellW, mCellH);
  284. }
  285. }
  286. }
  287. }
  288. }
  289. private void renderSpaceDiv(Graphics g, RectangleF rect)
  290. {
  291. int sx = (int)(rect.X / SpaceDIV);
  292. int sy = (int)(rect.Y / SpaceDIV);
  293. int sw = (int)(rect.Width / SpaceDIV) + 1;
  294. int sh = (int)(rect.Height / SpaceDIV) + 1;
  295. int lw = (int)(rect.Width + SpaceDIV * 2);
  296. int lh = (int)(rect.Height + SpaceDIV * 2);
  297. Pen pen = new Pen(Color.FromArgb(0x10, 0xff, 0xff, 0xff));
  298. for (int x = sx + sw; x >= sx; --x)
  299. {
  300. g.DrawLine(pen, x * SpaceDIV, sy * SpaceDIV, x * SpaceDIV, sy * SpaceDIV + lh);
  301. }
  302. for (int y = sy + sh; y >= sy; --y)
  303. {
  304. g.DrawLine(pen, sx * SpaceDIV, y * SpaceDIV, sx * SpaceDIV + lw, y * SpaceDIV);
  305. }
  306. }
  307. private void renderUnits(Graphics g, RectangleF rect)
  308. {
  309. foreach (GameObject u in units.Values)
  310. {
  311. if (rect.IntersectsWith(u.Bounds))
  312. {
  313. GraphicsState state = g.Save();
  314. g.TranslateTransform(u.pos.x, u.pos.y);
  315. u.render(g);
  316. g.Restore(state);
  317. }
  318. }
  319. }
  320. private void renderLogs(Graphics g)
  321. {
  322. for (int i = logs.Count - 1; i >= 0; --i)
  323. {
  324. DisplayLog u = logs[i];
  325. GraphicsState state = g.Save();
  326. g.TranslateTransform(u.pos.x, u.pos.y);
  327. u.render(g);
  328. g.Restore(state);
  329. if (!u.Alive)
  330. {
  331. logs.RemoveAt(i);
  332. }
  333. }
  334. }
  335. #endregion
  336. //-----------------------------------------------------------------------------------------------------------------
  337. public void update()
  338. {
  339. zone.update();
  340. foreach (GameObject u in units.Values)
  341. {
  342. u.update();
  343. }
  344. timer++;
  345. long curTime = CommonLang.CUtils.CurrentTimeMS();
  346. if (curTime != mLastUpdateTime)
  347. {
  348. mFPS = (int)(1000 / (curTime - mLastUpdateTime));
  349. }
  350. mLastUpdateTime = curTime;
  351. }
  352. //-----------------------------------------------------------------------------------------------------------------
  353. //-----------------------------------------------------------------------------------------------------------------
  354. public GameUnit getUnit(int unitID)
  355. {
  356. GameObject go;
  357. if (units.TryGetValue(unitID, out go))
  358. {
  359. if (go is GameUnit)
  360. {
  361. return go as GameUnit;
  362. }
  363. }
  364. return null;
  365. }
  366. public GameUnit getUnitByName(string name)
  367. {
  368. foreach (GameObject u in units.Values)
  369. {
  370. if (u is GameUnit)
  371. {
  372. if (name.Equals((u as GameUnit).Info.name))
  373. {
  374. return u as GameUnit;
  375. }
  376. }
  377. }
  378. return null;
  379. }
  380. //-----------------------------------------------------------------------------------------------
  381. // events
  382. //-----------------------------------------------------------------------------------------------
  383. public void onEventHandler(Event e)
  384. {
  385. if (e is AddUnitEvent)
  386. {
  387. doAddUnitEvent(e as AddUnitEvent);
  388. }
  389. else if (e is AddSpellEvent)
  390. {
  391. doAddSpellEvent(e as AddSpellEvent);
  392. }
  393. else if (e is RemoveObjectEvent)
  394. {
  395. doRemoveObjectEvent(e as RemoveObjectEvent);
  396. }
  397. else if (e is SyncPosEvent)
  398. {
  399. doSyncPosEvent(e as SyncPosEvent);
  400. }
  401. else if (e is ObjectEvent)
  402. {
  403. ObjectEvent oe = e as ObjectEvent;
  404. GameObject gu;
  405. if (units.TryGetValue(oe.object_id, out gu))
  406. {
  407. gu.doEvent(oe);
  408. }
  409. }
  410. }
  411. //-----------------------------------------------------------------------------------------------
  412. private void doAddUnitEvent(AddUnitEvent e)
  413. {
  414. GameUnit gu = null;
  415. if (e.info.force == 0)
  416. {
  417. gu = new GameUnitActor(this, e.info, e.unit_id);
  418. }
  419. else
  420. {
  421. gu = new GameUnitNpc(this, e.info, e.unit_id);
  422. }
  423. gu.pos.x = e.x;
  424. gu.pos.y = e.y;
  425. gu.direction = e.direction;
  426. units[gu.ID] = gu;
  427. }
  428. private void doAddSpellEvent(AddSpellEvent e)
  429. {
  430. SpellTemplate sp = TemplateManager.getInstance().getSpell(e.spell_template_id);
  431. if (sp != null)
  432. {
  433. GameSpellObject gs = new GameSpellObject(
  434. this, sp, e.spell_id,
  435. getUnit(e.launcher_unit_id),
  436. null);
  437. gs.pos.x = e.x;
  438. gs.pos.y = e.y;
  439. gs.direction = e.direction;
  440. units[gs.ID] = gs;
  441. }
  442. }
  443. private void doRemoveObjectEvent(RemoveObjectEvent e)
  444. {
  445. units.Remove(e.object_id);
  446. }
  447. private void doSyncPosEvent(SyncPosEvent e)
  448. {
  449. GameObject gu;
  450. for (int i = 0; i < e.unitsID.Length; ++i)
  451. {
  452. if (units.TryGetValue(e.unitsID[i], out gu))
  453. {
  454. gu.syncPos(e.unitsX[i], e.unitsY[i], e.unitsD[i]);
  455. }
  456. }
  457. }
  458. public void showLog(string text, float x, float y)
  459. {
  460. logs.Add(new DisplayLog(text, x, y));
  461. }
  462. public class DisplayLog
  463. {
  464. static private Pen pen = new Pen(Color.Black);
  465. static private Brush brush = new SolidBrush(Color.White);
  466. public Vector2 pos = new Vector2();
  467. private int time = 20;
  468. private string text = "";
  469. private SizeF size;
  470. public DisplayLog(string text, float x, float y)
  471. {
  472. this.text = text;
  473. this.time = 20;
  474. this.pos.x = x;
  475. this.pos.y = y;
  476. }
  477. public void render(Graphics g)
  478. {
  479. size = g.MeasureString(text, global_font);
  480. g.DrawString(
  481. text,
  482. global_font,
  483. brush,
  484. -size.Width / 2,
  485. -size.Height / 2 - 20 + time,
  486. StringFormat.GenericDefault);
  487. time--;
  488. }
  489. public bool Alive
  490. {
  491. get { return time > 0; }
  492. }
  493. }
  494. }
  495. //-----------------------------------------------------------------------------------------------
  496. }