Cocos2d-x lightning war (6) intelligent enemy machine AI attack-Flight Path Algorithm Design and Implementation (below), cocos2dx

Source: Internet
Author: User

Cocos2d-x lightning war (6) intelligent enemy machine AI attack-Flight Path Algorithm Design and Implementation (below), cocos2dx

Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka

I have been busy recently, so the game updates are very slow. Some netizens have been urging me, but there is no way. Today, we have time to update the game.

This article connects the Cocos2d-x "lightning war" (6) intelligent enemy machine AI strikes-Flight Path Algorithm Design and Implementation (on), or to the game of the enemy machine path design and implementation. Here, I have implemented two enemy routes. They are as follows:

(1) The enemy plane is flying towards the hero plane

(2) The two groups of planes pass through, in fact, a large group of planes fly from left to right and from right to left.

Effect:



Cocos2d-x version: 3.4

Project Environment: VS30213

I. Enemy planes fly to hero planes

First, let's talk about the flight of the enemy plane to the hero plane. This is relatively simple. As long as you get the location of the hero plane and set the initial location of the enemy plane, then the flight path of the enemy plane will come out. The author draws a picture below, which can be viewed as follows:

Or



Here, we can know the location of the hero aircraft, and calculate the values of a and B, and obtain the angle mDegree. Here mDegree is mainly used to rotate the enemy plane. If the enemy plane is not rotated, the effect will not look very good.

Now that you know the principle, write the code to implement it:

Void GameMain: enemyBuild3 (float dt) {Size winSize = Director: getInstance ()-> getWinSize (); auto spritePlane = Sprite: create ("air2.png "); // get the sprite width and height float height = spritePlane-> getContentSize (). height; float width = spritePlane-> getContentSize (). width; // set whether the enemy server is in spritePlane-> setPosition (Vec2 (winSize. width + width/2, winSize. height + height/2); spritePlane-> setScale (0.25); this-> addChild (spritePlane ); // calculate the float x = HeroPlane: getInstance ()-> getPlane ()-> getPosition (). x; float y = HeroPlane: getInstance ()-> getPlane ()-> getPosition (). y; float a = winSize. width-x; float B = winSize. height-y; // float radians = atanf (a/B); float mDegree = CC_RADIANS_TO_DEGREES (radians); spritePlane-> setRotation (180 + mDegree ); // calculate the final position of the enemy's machine float endX = winSize. width-(a/B) * winSize. height; float endY = 0; // calculate the flight time float flyVelocity = 200; // The running speed, which can be controlled by yourself. float flyLen = sqrt (winSize. width-endX) * (winSize. width-endX) + (winSize. height-endY) * (winSize. height-endY); float realFlyDuration = flyLen/flyVelocity; // the actual flight time // The distance and time of the bullet run, run from the plane to the bottom of the screen: auto actionMove = MoveTo: create (realFlyDuration, Point (endX, endY); // call back the function after the bullet completes the action, call the remove bullet function auto actionDone = CallFuncN: create (CC_CALLBACK_1 (GameMain: enemyRemove, this); // continuous action Sequence * sequence = Sequence: create (actionMove, actionDone, NULL); // The plane starts to run spritePlane-> runAction (sequence );}
Note that the image here is still not optimized, and there is no separate class for the enemy machine class. Here, it is simply implemented.

Then, enable a timer to periodically execute this operation.

// Call schedule (schedule_selector (GameMain: enemyBuild3), 0.5f) Every 0.5S );

Now let's take a look at the results:


The enemy plane can hit the hero plane and change its perspective in real time, of course. This is a good calculation.

2. Enemy planes flying between the left and right sides

The left and right groups fly in a row of planes on the left and right, and then move to the left or right at the same time. Aircraft paths are not a problem. The most important thing is to set their starting positions so that they can all be side by side. The schematic diagram is as follows:


Note that the offset of the enemy image must be added when setting the location of the enemy machine. In Coco2dx, when setting the genie location, the image center is used as the origin by default. Therefore, the offset must be added. In addition, they are moving at the same distance. Therefore, MoveBy can be used to achieve this. the Y axis direction is 0, and the X axis direction is the screen width + the enemy plane width. The amount of exercise on the X axis of the left and right enemy planes is different. Remember! In addition, the MoveBy action can only be used by one enemy machine. if the other is the same action, you can use the clone () function instead of re-creating a MoveBy action.

The overall code is as follows:

Void GameMain: enemyBuild4 (float dt) {Size winSize = Director: getInstance ()-> getWinSize (); Point origin = Director: getInstance ()-> getVisibleOrigin (); // generate auto spritePlane1 = Sprite: create ("air5.png"); auto spritePlane2 = Sprite: create ("air5.png"); auto spritePlane3 = Sprite :: create ("air5.png"); // generate edge planes auto spritePlane4 = Sprite: create ("air5.png"); auto spritePlane5 = Sprite: create ("air5.png "); auto spritePlane6 = Sprite: create ("air5.png"); // Rotation Angle spritePlane1-> setRotation (90); spritePlane2-> setRotation (90 ); spritePlane3-> setRotation (90); spritePlane4-> setRotation (-90); spritePlane5-> setRotation (-90); spritePlane6-> setRotation (-90 ); // set scaling // spritePlane1-> setScale (0.3); // spritePlane2-> setScale (0.3); // spritePlane3-> setScale (0.3 ); // get the sprite width and height float height = spritePlane1-> getContentSize (). height; float width = spritePlane1-> getContentSize (). width; // spritePlane1-> setPosition (Vec2 (-width/2, winSize. height-height/2-10); spritePlane2-> setPosition (Vec2 (-width/2, spritePlane1-> getPosition (). y-2 * height-10); spritePlane3-> setPosition (Vec2 (-width/2, spritePlane2-> getPosition (). y-2 * height-10); spritePlane4-> setPosition (Vec2 (winSize. width + width/2, spritePlane1-> getPosition (). y-height-10); spritePlane5-> setPosition (Vec2 (winSize. width + width/2, spritePlane4-> getPosition (). y-2 * height-10); spritePlane6-> setPosition (Vec2 (winSize. width + width/2, spritePlane5-> getPosition (). y-2 * height-10); // Add the Genie this-> addChild (spritePlane1) to the layer; this-> addChild (spritePlane2); this-> addChild (spritePlane3 ); this-> addChild (spritePlane4); this-> addChild (spritePlane5); // calculates the flight time float flyVelocity = 200; // you can control the running speed by yourself, float flyLen = winSize. width + width; float realFlyDuration = flyLen/flyVelocity; // actual flight time // The distance and time of bullet run, starting from the plane to the bottom of the screen auto actionMove1 = MoveBy :: create (realFlyDuration, Point (flyLen, 0); auto actionMove2 = MoveBy: create (realFlyDuration, Point (-flyLen, 0 )); // call back the function after the bullet is executed. Call the function auto actionDone = CallFuncN: create (CC_CALLBACK_1 (GameMain: enemyRemove, this) to remove the bullet )); // continuous action Sequence * sequence1 = Sequence: create (actionMove1, actionDone, NULL); Sequence * sequence2 = Sequence: create (actionMove1-> clone (), actionDone, NULL); Sequence * sequene3 = Sequence: create (actionMove1-> clone (), actionDone, NULL); Sequence * sequence4 = Sequence: create (actionMove2, actionDone, NULL ); sequence * sequence5 = Sequence: create (actionMove2-> clone (), actionDone, NULL); // The plane starts to run spritePlane1-> runAction (sequence1 ); spritePlane2-> runAction (sequence2); spritePlane3-> runAction (sequene3); spritePlane4-> runAction (sequence4); spritePlane5-> runAction (sequence5 );}

Then, in the same principle, open a timer to see the effect:

// Call schedule once every 0.5 (schedule_selector (GameMain: enemyBuild4), 0.5f );
 

Two planes come together:



Iii. Summary

Here, four aircraft paths are designed. Because the optimization has not yet been made, the memory usage will be a little more. In the future, I will put all the enemy planes in a Plist, in this way, the memory will be smaller. In fact, it is in flight games. The intelligent AI Design of the BOSS and BOSS machines is also very fun. Of course, the enemy and aircraft bullets are also very important, and this part of content will be discussed after the design of the enemy and aircraft class is complete. In the next lecture, We will encapsulate our enemy and aircraft in the future.

Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.