game.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. (function(){
  2. var orientationSet = false;
  3. var info = null;
  4. var ortDiv = null;
  5. var normalDiv = null;
  6. // var mobileType = 0;
  7. FZ.GameBase.MOBILE_IPAD = 10;
  8. FZ.GameBase.MOBILE_IPHONE = 20;
  9. FZ.GameBase.Width = 700;
  10. FZ.GameBase.Height = 500;
  11. FZ.GameBase.Name = "DreamFarmLinkHtml5";
  12. FZ.TARGET_DEF = {};
  13. FZ.TARGET_DEF.PC = "pc";
  14. FZ.TARGET_DEF.MOBILE = "mobile";
  15. FZ.TARGET_DEF.IPHONE = "iPhone";
  16. FZ.TARGET_DEF.IPAD = "iPad";
  17. FZ.TARGET_DEF.ANDRIOD = "Android";
  18. FZ.TargetPort = FZ.TARGET_DEF.PC;// pc, mobile
  19. FZ.TargetMobile = FZ.TARGET_DEF.IPHONE;
  20. FZ.EVENT_DEF = {};
  21. FZ.EVENT_DEF.M_OVER = "mouseover";
  22. FZ.EVENT_DEF.M_OUT = "mouseout";
  23. FZ.EVENT_DEF.M_MOVE = "mousemove";
  24. FZ.EVENT_DEF.M_CLICK = "click";
  25. FZ.EVENT_DEF.T_START = "touchstart";
  26. FZ.EVENT_DEF.T_MOVE = "touchmove";
  27. FZ.EVENT_DEF.T_END = "touchend";
  28. FZ.GameBase.setCss = function(div, info){
  29. div.style.width = info.w + "px";
  30. div.style.height = info.h + "px";
  31. div.style.left = info.x + "px";
  32. div.style.top = info.y + "px";
  33. div.style.position = "absolute";
  34. div.style.backgroundRepeat = "no-repeat";
  35. div.style.backgroundImage = "url(imgs/" + info.fileURL + ")";
  36. };
  37. FZ.PrintLog = function(str){
  38. // var hintDiv = document.getElementById("strHint");
  39. // hintDiv.value = str;
  40. };
  41. var createOrientationDiv = function(){
  42. var img = null;
  43. if (orientationSet) {
  44. return true;
  45. }
  46. img = FZ.ResourceManager.ResourceLib["OrientationWarning"];
  47. if (!img) {
  48. //image not load
  49. return false;
  50. }
  51. info = FZ.getImgInfo("OrientationWarning");
  52. ortDiv.style.backgroundRepeat = "no-repeat";
  53. ortDiv.style.backgroundImage = "url(imgs/" + info.fileURL + ")";
  54. ortDiv.style.backgroundPositionX = "50%";
  55. ortDiv.style.backgroundPositionY = "50%";
  56. ortDiv.style.backgroundColor = "#fff";
  57. orientationSet = true;
  58. return true;
  59. };
  60. var windowOrientationChangeFunction = function(){
  61. if ((90 === window.orientation) || (-90 === window.orientation)) {
  62. // FZ.DivManager.setOverflow("visible");
  63. // FZ.DivManager.addChild(orientationDiv);
  64. normalDiv.style.display = "none";
  65. ortDiv.style.display = "inline";
  66. FZ.Game.GameState.pageHide();
  67. }
  68. else
  69. if (0 === window.orientation) {
  70. normalDiv.style.display = "inline";
  71. ortDiv.style.display = "none";
  72. FZ.Game.GameState.pageShow();
  73. }
  74. var offsetx = 0;
  75. var offsety = 0;
  76. if (FZ.TARGET_DEF.ANDRIOD === FZ.TargetMobile) {
  77. offsety = 1;
  78. }
  79. setTimeout(function(){
  80. // window.scrollTo(offsetx, offsety);
  81. }, 10);
  82. // setTimeout(function () {
  83. // window.scrollTo(0,0);
  84. // }, 10);
  85. // alert(window.orientation);
  86. };
  87. var windowonscroll = function(){
  88. /*var offsetx = 0;
  89. var offsety = 0;
  90. if (FZ.TARGET_DEF.ANDRIOD === FZ.TargetMobile) {
  91. offsety = 1;
  92. }
  93. setTimeout(function(){
  94. window.scrollTo(offsetx, offsety);
  95. }, 1000);*/
  96. };
  97. var windowtouchStart = function(evt){
  98. evt.stopPropagation();
  99. //evt.preventDefault();
  100. return false;
  101. };
  102. var windowInit = function(){
  103. // updateShare(0,0);
  104. var metas = null;
  105. var heads = null;
  106. FZ.GameBase.loadGame();
  107. //FZ.GameBase.SaveObject.m_cur_level = 9;
  108. if (undefined === FZ.GameBase.SaveObject.m_cur_level) {
  109. //create the default game record.
  110. FZ.GameBase.SaveObject.m_cur_level = 0;
  111. FZ.GameBase.SaveObject.pass = false;
  112. FZ.GameBase.SaveObject.score = 0;
  113. FZ.GameBase.SaveObject.m_hint = FZ.GameDefs.LEVEL_HINT_NUMBER[1];
  114. //
  115. FZ.GameBase.SaveObject.m_gamein = false;
  116. FZ.GameBase.SaveObject.m_cur_score = 0;
  117. // FZ.GameBase.SaveObject.m_level_high_score = 0;
  118. FZ.GameBase.SaveObject.m_cur_hintCount = FZ.GameDefs.LEVEL_HINT_NUMBER[1];
  119. FZ.GameBase.SaveObject.m_cur_left_pet = FZ.GameDefs.ALL_PET_NUM;
  120. FZ.GameBase.SaveObject.m_level_usedTime = 0;
  121. FZ.GameBase.SaveObject.m_usedTime = 0;
  122. if (FZ.TargetMobile !== FZ.TARGET_DEF.ANDRIOD) {
  123. FZ.GameBase.SaveObject.m_sound = true;
  124. }
  125. var index = 0;
  126. var tmp = null;
  127. var tmpPetArr = null;
  128. tmpPetArr = [];
  129. for (index = 0; index < FZ.GameDefs.ALL_GENERAL; index++) {
  130. tmp = {};
  131. // tmp.line = 0;
  132. // tmp.col = 0;
  133. tmp.style = -1;
  134. tmp.state = FZ.GameDefs.DREAMPET_INIT;
  135. tmpPetArr.push(tmp);
  136. }
  137. FZ.GameBase.SaveObject.m_data = tmpPetArr;
  138. FZ.GameBase.saveGame();
  139. }
  140. normalDiv = FZ.$("mainDiv");
  141. ortDiv = FZ.$("orientationDiv");
  142. FZ.GameBase.animDiv = FZ.$("animDiv");
  143. // FZ.GameBase.animDiv.style.overflow = "visible";
  144. ortDiv.style.display = "none";
  145. if (/(iPhone|iPad|iPod|Android)/i.test(window.navigator.appVersion)) {
  146. FZ.TargetPort = FZ.TARGET_DEF.MOBILE;
  147. FZ.TargetMobile = FZ.TARGET_DEF.IPHONE;
  148. if (-1 !== navigator.userAgent.indexOf("iPad")) {
  149. FZ.TargetMobile = FZ.TARGET_DEF.IPAD;
  150. }
  151. else
  152. if (-1 !== navigator.userAgent.indexOf("Android")) {
  153. FZ.TargetMobile = FZ.TARGET_DEF.ANDRIOD;
  154. }
  155. }
  156. metas = document.createElement("meta");
  157. heads = document.getElementsByTagName("head");
  158. if (FZ.TARGET_DEF.IPAD === FZ.TargetMobile) {
  159. // mobileType = FZ.GameBase.MOBILE_IPAD;
  160. metas.setAttribute("name", "viewport");
  161. metas.setAttribute("content", "width=device-width, initial-scale=2.0, maximum-scale=2.0,user-scalable=no");
  162. heads[0].appendChild(metas);
  163. // var Metas = document.getElementsByTagName("meta");
  164. // for (var i=0;i<Metas.length;++i){
  165. // if (Metas[i].getAttribute("name")==="viewport") {
  166. // Metas[i].setAttribute("content", "width=320, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes");
  167. // break;
  168. // }
  169. // }
  170. // normalDiv.style.left = "50%";
  171. // normalDiv.style.marginLeft = "-320px";
  172. }
  173. else {
  174. metas.setAttribute("name", "viewport");
  175. metas.setAttribute("content", "width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no");
  176. heads[0].appendChild(metas);
  177. }
  178. //window.scrollTo(0, 0);
  179. // //init the media manager.
  180. // FZ.MusicManager.init();
  181. FZ.DivManager.init();
  182. FZ.DivManager.setParent(normalDiv);
  183. FZ.DivManager.setSize(FZ.GameDefs.SCREEN_W, FZ.GameDefs.SCREEN_H);
  184. FZ.DivManager.setOffset(normalDiv.offsetLeft, normalDiv.offsetTop);
  185. // alert("x = " + normalDiv.offsetLeft + ", y = " + normalDiv.offsetTop);
  186. FZ.GameBase.switchToState(FZ.StateDefs.GAME_STATE_PRELOADING);
  187. if (FZ.TARGET_DEF.MOBILE === FZ.TargetPort) {
  188. //window.onscroll = windowonscroll;
  189. window.addEventListener("orientationchange", windowOrientationChangeFunction, false);
  190. }
  191. document.addEventListener("touchstart", windowtouchStart, false);
  192. // window.ontouchstart = function(event) {
  193. // return false;
  194. // };
  195. windowOrientationChangeFunction();
  196. FZ.PrintLog("test log");
  197. };
  198. FZ.GameBase.ArrayIndexof = function(arr, obj){
  199. var index = 0;
  200. for (index = 0; index < arr.length; index++) {
  201. if (obj === arr[index]) {
  202. return index;
  203. }
  204. }
  205. return -1;
  206. };
  207. window.addEventListener("load", windowInit, false);
  208. FZ.EventHandler = function(obj, target, type, callBack){
  209. if (!target) {
  210. return;
  211. }
  212. switch (type) {
  213. case FZ.EVENT_DEF.M_OVER:{
  214. target.addEventListener(FZ.EVENT_DEF.M_OVER, obj.m_call_mOver = function(evt){
  215. callBack.call(obj, evt);
  216. }, false);
  217. // target.onmouseover = function(evt) { callBack.call(obj, evt); };
  218. break;
  219. }
  220. case FZ.EVENT_DEF.M_OUT:{
  221. target.addEventListener(FZ.EVENT_DEF.M_OUT, obj.m_call_mOut = function(evt){
  222. callBack.call(obj, evt);
  223. }, false);
  224. // target.onmouseout = function(evt) { callBack.call(obj, evt); };
  225. break;
  226. }
  227. case FZ.EVENT_DEF.M_MOVE:{
  228. target.addEventListener(FZ.EVENT_DEF.M_MOVE, obj.m_call_mMove = function(evt){
  229. callBack.call(obj, evt);
  230. }, false);
  231. // target.onmousemove = function(evt) { callBack.call(obj, evt); };
  232. break;
  233. }
  234. case FZ.EVENT_DEF.M_CLICK:{
  235. target.addEventListener(FZ.EVENT_DEF.M_CLICK, obj.m_call_mClick = function(evt){
  236. callBack.call(obj, evt);
  237. }, false);
  238. // target.onclick = function(evt) { callBack.call(obj, evt); };
  239. break;
  240. }
  241. case FZ.EVENT_DEF.T_START:{
  242. target.addEventListener(FZ.EVENT_DEF.T_START, obj.m_call_tStart = function(evt){
  243. callBack.call(obj, evt);
  244. }, false);
  245. // target.ontouchstart = function(evt) { callBack.call(obj, evt); };
  246. break;
  247. }
  248. case FZ.EVENT_DEF.T_MOVE:{
  249. target.addEventListener(FZ.EVENT_DEF.T_MOVE, obj.m_call_tMove = function(evt){
  250. callBack.call(obj, evt);
  251. }, false);
  252. // target.ontouchmove = function(evt) { callBack.call(obj, evt); };
  253. break;
  254. }
  255. case FZ.EVENT_DEF.T_END:{
  256. target.addEventListener(FZ.EVENT_DEF.T_END, obj.m_call_tEnd = function(evt){
  257. callBack.call(obj, evt);
  258. }, false);
  259. // target.ontouchend = function(evt) { callBack.call(obj, evt); };
  260. break;
  261. }
  262. default:
  263. {
  264. break;
  265. }
  266. }
  267. };
  268. FZ.EventRemove = function(obj, target, type, callBack){
  269. if ((!target) || (!callBack)) {
  270. return;
  271. }
  272. switch (type) {
  273. case FZ.EVENT_DEF.M_OVER:{
  274. target.removeEventListener(FZ.EVENT_DEF.M_OVER, callBack, false);
  275. obj.m_call_mOver = null;
  276. // target.onmouseover = function(evt) { callBack.call(obj, evt); };
  277. break;
  278. }
  279. case FZ.EVENT_DEF.M_OUT:{
  280. target.removeEventListener(FZ.EVENT_DEF.M_OUT, callBack, false);
  281. obj.m_call_mOut = null;
  282. // target.onmouseout = function(evt) { callBack.call(obj, evt); };
  283. break;
  284. }
  285. case FZ.EVENT_DEF.M_MOVE:{
  286. target.removeEventListener(FZ.EVENT_DEF.M_MOVE, callBack, false);
  287. obj.m_call_mMove = null;
  288. // target.onmousemove = function(evt) { callBack.call(obj, evt); };
  289. break;
  290. }
  291. case FZ.EVENT_DEF.M_CLICK:{
  292. target.removeEventListener(FZ.EVENT_DEF.M_CLICK, callBack, false);
  293. obj.m_call_mClick = null;
  294. // target.onclick = function(evt) { callBack.call(obj, evt); };
  295. break;
  296. }
  297. case FZ.EVENT_DEF.T_START:{
  298. target.removeEventListener(FZ.EVENT_DEF.T_START, callBack, false);
  299. obj.m_call_tStart = null;
  300. // target.ontouchstart = function(evt) { callBack.call(obj, evt); };
  301. break;
  302. }
  303. case FZ.EVENT_DEF.T_MOVE:{
  304. target.removeEventListener(FZ.EVENT_DEF.T_MOVE, callBack, false);
  305. obj.m_call_tMove = null;
  306. // target.ontouchmove = function(evt) { callBack.call(obj, evt); };
  307. break;
  308. }
  309. case FZ.EVENT_DEF.T_END:{
  310. target.removeEventListener(FZ.EVENT_DEF.T_END, callBack, false);
  311. obj.m_call_tEnd = null;
  312. // target.ontouchend = function(evt) { callBack.call(obj, evt); };
  313. break;
  314. }
  315. default:
  316. {
  317. break;
  318. }
  319. }
  320. };
  321. })();