PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/gameedit/C01_TD/src/game/unit/UnitShoot.java

http://cellengine.googlecode.com/
Java | 413 lines | 257 code | 52 blank | 104 comment | 22 complexity | 6163dcbaee35c7887a10f9d274a9397b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, AGPL-3.0, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0
  1. package game.unit;
  2. import javax.microedition.lcdui.Graphics;
  3. import com.cell.*;
  4. import com.cell.game.*;
  5. import com.cell.game.ai.*;
  6. import com.cell.particle.*;
  7. public class UnitShoot extends Unit {
  8. // ----------------------------------------------------------------------------------------------------
  9. // ??
  10. static final public int TYPE_NONE = -1;
  11. /*TODO ??????????Shoots*/
  12. static final public int TYPE00_ARROW = 0;//
  13. static final public int TYPE01_ARROW = 1;
  14. static final public int TYPE02_ARROW = 2;
  15. static final public int TYPE03_ARROW = 3;
  16. static final public int TYPE11_ICE = 4;
  17. static final public int TYPE12_ICE = 5;
  18. static final public int TYPE13_ICE = 6;
  19. static final public int TYPE21_FIRE = 7;
  20. static final public int TYPE22_FIRE = 8;
  21. static final public int TYPE23_FIRE = 9;
  22. static final public int TYPE30_ARROW = 10;//
  23. static final public int TYPE31_ARROW = 11;
  24. static final public int TYPE32_ARROW = 12;
  25. static final public int TYPE33_ARROW = 13;
  26. static final public int TYPE41_ICE = 14;
  27. static final public int TYPE42_ICE = 15;
  28. static final public int TYPE43_ICE = 16;
  29. static final public int TYPE51_FIRE = 17;
  30. static final public int TYPE52_FIRE = 18;
  31. static final public int TYPE53_FIRE = 19;
  32. /**??*/
  33. private int Type = 0;
  34. private int state = -1;
  35. //public boolean Penetrable = false;
  36. /**??????*/
  37. public boolean Splash = true;
  38. /**????*/
  39. public int SplashRange = 100;
  40. public UnitShoot(CSprite stuff){
  41. super(stuff);
  42. setState(this);
  43. Priority = 1024;
  44. HP = 10+Random.nextInt()%10;
  45. }
  46. public void update(){
  47. // main state machine
  48. switch (state) {
  49. case STATE_MISSILE:
  50. if (!isEndMissile()) {
  51. onMissile();
  52. } else {
  53. startTerminate(X, Y);
  54. }
  55. break;
  56. // case STATE_SLUG:
  57. // if (!isEndSlug()) {
  58. // onSlug();
  59. // } else {
  60. // startTerminate(X, Y);
  61. // }
  62. // break;
  63. case STATE_TERMINATE:
  64. if (!isEndTerminate()) {
  65. onTerminate();
  66. } else {
  67. startNone();
  68. }
  69. break;
  70. default:
  71. startNone();
  72. }
  73. }
  74. // ????????
  75. public boolean getIsCriticalDamage(){
  76. switch(Type){
  77. case TYPE01_ARROW:return Math.abs(Random.nextInt())%100<10;
  78. case TYPE02_ARROW:return Math.abs(Random.nextInt())%100<20;
  79. case TYPE03_ARROW:return Math.abs(Random.nextInt())%100<30;
  80. case TYPE31_ARROW:return Math.abs(Random.nextInt())%100<10;
  81. case TYPE32_ARROW:return Math.abs(Random.nextInt())%100<20;
  82. case TYPE33_ARROW:return Math.abs(Random.nextInt())%100<30;
  83. }
  84. return false;
  85. }
  86. //??????
  87. public int getSlowRate(){
  88. switch(Type){
  89. case TYPE11_ICE:return 20;
  90. case TYPE12_ICE:return 40;
  91. case TYPE13_ICE:return 60;
  92. case TYPE41_ICE:return 20;
  93. case TYPE42_ICE:return 40;
  94. case TYPE43_ICE:return 60;
  95. }
  96. return 0;
  97. }
  98. //??????
  99. public int getSlowTime(){
  100. switch(Type){
  101. case TYPE11_ICE:return 2*AScreen.FrameDelay;
  102. case TYPE12_ICE:return 4*AScreen.FrameDelay;
  103. case TYPE13_ICE:return 8*AScreen.FrameDelay;
  104. case TYPE41_ICE:return 2*AScreen.FrameDelay;
  105. case TYPE42_ICE:return 4*AScreen.FrameDelay;
  106. case TYPE43_ICE:return 8*AScreen.FrameDelay;
  107. }
  108. return 0;
  109. }
  110. //????????
  111. public int getInjureRate(){
  112. switch(Type){
  113. case TYPE21_FIRE:return 5;
  114. case TYPE22_FIRE:return 10;
  115. case TYPE23_FIRE:return 15;
  116. case TYPE51_FIRE:return 5;
  117. case TYPE52_FIRE:return 10;
  118. case TYPE53_FIRE:return 15;
  119. }
  120. return 0;
  121. }
  122. //????????
  123. public int getInjureTime(){
  124. switch(Type){
  125. case TYPE21_FIRE:return 5*AScreen.FrameDelay;
  126. case TYPE22_FIRE:return 10*AScreen.FrameDelay;
  127. case TYPE23_FIRE:return 15*AScreen.FrameDelay;
  128. case TYPE51_FIRE:return 5*AScreen.FrameDelay;
  129. case TYPE52_FIRE:return 10*AScreen.FrameDelay;
  130. case TYPE53_FIRE:return 15*AScreen.FrameDelay;
  131. }
  132. return 0;
  133. }
  134. public void startFire(int type,int sx,int sy,UnitEnemy[] targets,int targetID){
  135. Type = type;
  136. switch(Type){
  137. case TYPE00_ARROW:
  138. case TYPE01_ARROW:
  139. case TYPE02_ARROW:
  140. case TYPE03_ARROW:
  141. case TYPE11_ICE:
  142. case TYPE12_ICE:
  143. case TYPE13_ICE:
  144. case TYPE21_FIRE:
  145. case TYPE22_FIRE:
  146. case TYPE23_FIRE:
  147. startMissile(sx,sy,targets,targetID,Type);
  148. break;
  149. case TYPE30_ARROW:
  150. case TYPE31_ARROW:
  151. case TYPE32_ARROW:
  152. case TYPE33_ARROW:
  153. case TYPE41_ICE:
  154. case TYPE42_ICE:
  155. case TYPE43_ICE:
  156. case TYPE51_FIRE:
  157. case TYPE52_FIRE:
  158. case TYPE53_FIRE:
  159. Splash = true;
  160. SplashRange = 32;
  161. startMissile(sx,sy,targets,targetID,Type);
  162. break;
  163. }
  164. }
  165. // ----------------------------------------------------------------------------------------------------
  166. // ??
  167. final public int STATE_NONE = -1;
  168. void startNone(){
  169. this.Active = false;
  170. this.Visible = false;
  171. state = STATE_NONE;
  172. Splash = false;
  173. }
  174. boolean isEndNone(){
  175. return !Active;
  176. }
  177. void onNone(){
  178. }
  179. // ----------------------------------------------------------------------------------------------------
  180. // ---------------------------------------------------------------------------------------------------------
  181. // ---------------------------------------------------------------------------------------------------------
  182. // ---------------------------------------------------------------------------------------------------------
  183. // ----------------------------------------------------------------------------------------------------
  184. //// ????
  185. // final public int STATE_SLUG = 1;
  186. // int SlugX;
  187. // int SlugY;
  188. // int SlugMaxSpeed = 4;
  189. // void startDst(int sx,int sy,int dx,int dy,int type){
  190. // state = STATE_SLUG;
  191. // Active = true;
  192. // Visible = true;
  193. //
  194. // SlugX = dx;
  195. // SlugY = dy;
  196. //
  197. // SpeedX256 = 0;
  198. // SpeedY256 = 0;
  199. //
  200. // HPos256 = sx * 256 ;
  201. // VPos256 = sy * 256 ;
  202. //
  203. // X = sx;
  204. // Y = sy;
  205. //
  206. // setCurrentFrame(type, 0);
  207. // }
  208. // boolean isEndSlug(){
  209. // if( CCD.cdRectPoint(
  210. // SlugX-SlugMaxSpeed,
  211. // SlugY-SlugMaxSpeed,
  212. // SlugX+SlugMaxSpeed,
  213. // SlugY+SlugMaxSpeed,
  214. // X, Y))
  215. // {
  216. // return true;
  217. // }
  218. // return false;
  219. // }
  220. // void onSlug(){
  221. // int dx = SlugX - X;
  222. // int dy = SlugY - Y;
  223. // int bx = Math.abs(dx)*256/SlugMaxSpeed;
  224. // int by = Math.abs(dy)*256/SlugMaxSpeed;
  225. // int d = Math.max(bx,by);
  226. //
  227. // if(d!=0){
  228. // SpeedX256 = dx * 256 * 256 / d ;
  229. // SpeedY256 = dy * 256 * 256 / d ;
  230. // }
  231. //
  232. // if(Math.abs(dx)>SlugMaxSpeed){
  233. // HPos256 += SpeedX256 ;
  234. // X = HPos256/256;
  235. // }else{
  236. // X = SlugX;
  237. // }
  238. // if(Math.abs(dy)>SlugMaxSpeed){
  239. // VPos256 += SpeedY256 ;
  240. // Y = VPos256/256;
  241. // }else{
  242. // Y = SlugY;
  243. // }
  244. //
  245. //// tryMove(dx-X, dy-Y);
  246. // EffectSpawn(EFFECT_TAIL_SWORD,X,Y,null);
  247. // nextCycFrame();
  248. //
  249. // }
  250. // ---------------------------------------------------------------------------------------------------------
  251. // ????
  252. final public int STATE_MISSILE = 2;
  253. UnitEnemy[] MissileTargets = null;
  254. int MissileTargetID = -1;
  255. int MissileMaxSpeed = 8;
  256. int DstX;
  257. int DstY;
  258. void startMissile(int sx, int sy, UnitEnemy[] targets, int targetID, int type){
  259. state = STATE_MISSILE;
  260. Active = true;
  261. Visible = true;
  262. MissileTargets = targets;
  263. MissileTargetID = targetID;
  264. DstX = MissileTargets[MissileTargetID].X;
  265. DstY = MissileTargets[MissileTargetID].Y;
  266. SpeedX256 = 0;
  267. SpeedY256 = 0;
  268. HPos256 = (sx<<8) ;
  269. VPos256 = (sy<<8) ;
  270. X = sx;
  271. Y = sy;
  272. // tryMove(sx-X, sy-Y);
  273. setCurrentFrame(type, 0);
  274. }
  275. boolean isEndMissile(){
  276. if( !MissileTargets[MissileTargetID].Active ){
  277. if(DstX == X && DstY == Y){
  278. return true;
  279. }
  280. }
  281. if(CCD.cdRectPoint(
  282. X-MissileMaxSpeed,
  283. Y-MissileMaxSpeed,
  284. X+MissileMaxSpeed,
  285. Y+MissileMaxSpeed,
  286. DstX,
  287. DstY))
  288. {
  289. /*TODO : ???????*/
  290. MissileTargets[MissileTargetID].directDamage(this);
  291. /*TODO : ??????*/
  292. if(Splash){
  293. for(int i=MissileTargets.length-1;i>=0;i--){
  294. if(MissileTargets[i].Active && i!=MissileTargetID)
  295. if(CCD.cdRectPoint(
  296. X-SplashRange,
  297. Y-SplashRange,
  298. X+SplashRange,
  299. Y+SplashRange,
  300. MissileTargets[i].X,
  301. MissileTargets[i].Y)){
  302. MissileTargets[i].splashDamage(this);
  303. }
  304. }
  305. }
  306. EffectSpawn(EFFECT_DAMAGE_SWORD,MissileTargets[MissileTargetID].X,MissileTargets[MissileTargetID].Y,null, 0);
  307. return true;
  308. }
  309. return false;
  310. }
  311. void onMissile(){
  312. if( MissileTargets[MissileTargetID]!=null && MissileTargets[MissileTargetID].Active ){
  313. DstX = MissileTargets[MissileTargetID].X;
  314. DstY = MissileTargets[MissileTargetID].Y;
  315. }
  316. int dx = DstX - X;
  317. int dy = DstY - Y;
  318. int bx = (Math.abs(dx)<<8)/MissileMaxSpeed;
  319. int by = (Math.abs(dy)<<8)/MissileMaxSpeed;
  320. int d = Math.max(bx,by);
  321. if(d!=0){
  322. SpeedX256 = (dx<<16) / d ;
  323. SpeedY256 = (dy<<16) / d ;
  324. }
  325. if(Math.abs(dx)>MissileMaxSpeed){
  326. HPos256 += SpeedX256 ;
  327. X = (HPos256>>8);
  328. }else{
  329. X = DstX;
  330. }
  331. if(Math.abs(dy)>MissileMaxSpeed){
  332. VPos256 += SpeedY256 ;
  333. Y = (VPos256>>8);
  334. }else{
  335. Y = DstY;
  336. }
  337. // tryMove(dx-X, dy-Y);
  338. // EffectSpawn(EFFECT_TAIL_FIRE,X,Y,null);
  339. nextCycFrame();
  340. }
  341. // ---------------------------------------------------------------------------------------------------------
  342. // ---------------------------------------------------------------------------------------------------------
  343. // ????
  344. final public int STATE_TERMINATE = 3;
  345. void startTerminate(int sx,int sy){
  346. Type = -1;
  347. state = STATE_TERMINATE;
  348. Active = true;
  349. Visible = true;
  350. X = sx;
  351. Y = sy;
  352. // tryMove(sx-X, sy-Y);
  353. EffectSpawn(EFFECT_ATTACK_FIRE,X,Y,null, 0);
  354. setCurrentFrame(STATE_TERMINATE, 0);
  355. }
  356. void onTerminate(){
  357. }
  358. boolean isEndTerminate(){
  359. return nextFrame() || !OnScreen;
  360. }
  361. }