Rebuilding Starcraft with js
Demo address
Http://www.script8.com/works/ SC /index.htm
Background
For many people, javaScript is a mysterious language, which is parsed by a browser and can implement complicated functions, but is rarely used in reality. In a website system based on relative positioning, js is not essential. The earliest browser does not support js. You only need to use pure static html to build a fully functional website. For page developers, it is estimated that JavaScript is used for checking data validity. In recent years, with the rise of the ajax concept, the use of js has increased, but in addition to a few tool-type websites such as web mail, its role is still auxiliary.
In fact, JavaScript, as the most popular scripting language, is very powerful. I used JavaScript to simulate Starcraft. As we all know, Starcraft is a milestone in the history of real-time strategic games. In the minds of players, it is self-evident that playing Starcraft in various Internet cafes is a part of my life. With just a cool passion, I am using js to develop Starcraft. This is a tough process, full of setbacks, and fun. Finally, I can do it, I did not expect it at the beginning.
The js interstellar source code was first published in the carefree script forum and won praise from many netizens. I was moved by more efforts. Through this development, my js development technology has improved a lot and I have known many friends who like js development. Fortunately! The current storage address of js star is http://www.script8.com/works/ SC /index.htm. interested parties are welcome to participate in the discussion.
Solution
The path finding algorithm is the foundation of the game and the most complex part. I didn't use the * algorithm because it is too costly for the script. Instead, I developed A new algorithm called the intersection method, which is characterized by linear path finding and A relatively small amount of computing, the disadvantage is that the result cannot be the shortest path. The intersection method is described as follows:
1. draw a straight line from the start point to the end point. When encountering an obstacle, the corresponding penetration point and exit point will always appear.
2. From the two directions of the penetrating point to bypass the obstacle at the same time, select the first route to reach the penetrating point as the forward route. In this way, a relatively primitive route is obtained.
3. Optimize the route, determine whether the two points form a path, delete the redundant points, and obtain the final path.
A large number of operations are used to determine whether the obstacle points are used during path search. The general practice is to traverse the array and compare the results to obtain results. I turn the obstacle point sequence into a long string. Through the string inclusion relationship, I can determine whether the current point is an obstacle point, which reduces a lot of computing workload.
The implementation of js Starcraft is equivalent to the StarCraft opening part, namely mining, building manufacturing, and producing soldiers. The combat part is slightly involved. Before coding, we need to have a lot of preparation work. It is very annoying to make pictures and it takes a lot of patience. In addition, I developed a map editor using hta technology to generate map data.
The control panel in the game is relatively simple, and the thumbnail map and selection information are integrated into a small panel in the upper right corner. Other parts belong to the game scenario. Each Mobile Unit has eight directions. Taking gun soldiers as an example, 24 images are required to represent standing and moving posture. These images are integrated into a large image, showing the corresponding part of the picture based on walking or standing conditions. For example, when walking around, you can determine the moving direction and move the gun position at one side, three images are displayed on one side, showing moving.
During Game initialization, map data is loaded. Based on different building attributes, a scenario is generated and a list of obstacles is generated for path searching. By default, there are several miners on the map. After selecting the target, you can click the target to walk independently. According to the game requirements, if you click the ore, you will move back and forth between the mining area and the headquarters, each round trip will increase the amount of deposits.
As to which building can produce troops, how much resources each soldier needs, and the production sequence of the building, it is relatively easy, it is nothing more than doing more image processing and making more logical judgments. It will not be detailed in length.
Experience Sharing
Don't worry too much about the implementation of functions, because js is already very well-developed, you can perform various operations as you like, use the absolute positioning mechanism, you can easily create a interface, and then use the clock to simulate multithreading, moving Images in real time can present animations. The problem mainly lies in speed and performance. As Javascript is a scripting language, its computing performance is undoubtedly inherently inadequate. Meanwhile, the browser does not support Hard acceleration such as directX and openGL, and its animation capabilities cannot be satisfied. The smooth operation of the game can be ensured only by reducing the computing workload and rationally allocating animation resources. Js star development follows this principle everywhere, which can be simplified, saved, and used in a very cost-effective manner.
Do not use the filter technology in the game, especially the dynamic filter. Filter rendering will occupy a large amount of cpu. When the cpu usage reaches 80% or more, the game will feel stuck. This is significantly different from flash's similar programs. Even if the cpu usage is close to 100%, the flash program can still be quite smooth.
In all fairness, the client-side game development should be dominated by flash. In comparison, the advantage of js is that it can be seamlessly connected to the website without installing plug-ins. Of course, as long as it is a fun and practical game, even developed with js, it is not a problem.