The monster behavior in the game directly leads to the instability of the protagonist's survival, thus greatly enhancing the playability of the game. If the monster remains static like a mine, the game will be much less risky and irritating, and it will not be very fun.
In this version, the actions of monsters are very simple. It is nothing more than walking through a feasible channel and continuing to find accessible channels when obstacles change direction. Capable children's shoes can write several AI programs for monsters, such as playing the leading role in a certain range (is it very tricky?), traveling with bombs, and other intelligent behaviors. Because I have limited capabilities, I can only think about it. I can only write a few random functions for monsters as their actions. Thank you for your patience!
When a monster encounters a wall or bomb, it cannot go forward. It must change its direction to find a place that can pass through. The move () method has been compiled for monsters in the previous chapter. I will not talk about it here. Assuming that N monsters have been created and distributed to the open space of the map, they are walking around. First, analyze the monster behavior process:
We can see that to implement the actions of monsters, we also need to implement two functions: Determine whether it is feasible and change the moving direction.
To change the direction, you only need to change the direction randomly until the forward direction can pass:
/// <Summary> <br/> // automatically change the direction <br/> /// </Summary> <br/> /// <Param name = "OBJ "> </param> <br/> Public void autochangedirection (activityentity OBJ) <br/>{< br/> int r = random. next (0, 4); <br/> switch (r) <br/>{< br/> case 0: <br/> obj. direction = direction. down; <br/> break; <br/> case 1: <br/> obj. direction = direction. up; <br/> break; <br/> case 2: <br/> obj. direction = direction. right; <br/> break; <br/> case 3: <br/> obj. direction = direction. left; <br/> break; <br/>}< br/>}
Determine whether the front can pass:
/// <Summary> <br/> // whether the traffic can pass forward <br/> /// </Summary> <br/> /// <Param name = "OBJ "> lifecycle objects </param> <br/> // <returns> </returns> <br/> Public bool passable (activityentity OBJ) <br/> {<br/> point newpoint = obj. poi; <br/> // step forward <br/> switch (obj. direction) <br/>{< br/> case direction. down: <br/> newpoint. Y + = obj. speed; <br/> break; <br/> case direction. left: <br/> newpoint. x-= obj. speed; <br/> break; <br/> case direction. right: <br/> newpoint. X + = obj. speed; <br/> break; <br/> case direction. up: <br/> newpoint. y-= obj. speed; <br/> break; <br/>}</P> <p> // critical detection <br/> If (newpoint. x <0 | newpoint. x> (map. cols-1) * utility. gridsize | <br/> newpoint. Y <0 | newpoint. y> (map. rows-1) * utility. gridsize) <br/>{< br/> return false; <br/>}</P> <p> // collision detection <br/> for (INT x = 0; x <map. cols; X ++) <br/>{< br/> for (INT y = 0; y <map. rows; y ++) <br/>{< br/> If (map. grids [x, y] = gridtype. soil | map. grids [x, y] = gridtype. stone | map. grids [x, y] = gridtype. bomb) <br/>{< br/> int posx = x * utility. gridsize; <br/> int posy = y * utility. gridsize; <br/> If (obj is enemy) <br/>{< br/> If (hitcheck. isintersect (posx, Posy, newpoint. x, newpoint. y) <br/>{< br/> return false; <br/>}< br/> else <br/> {<br/> // when a bomb is placed, the main character can use the bomb until the main character leaves the bomb and becomes an obstacle. <br/> If (hitcheck. isintersect (posx, Posy, obj. poi. x, obj. poi. y) <br/>{< br/> continue; <br/>}< br/> If (hitcheck. isintersect (posx, Posy, newpoint. x, newpoint. y) <br/>{< br/> return false; <br/>}< br/> return true; <br/>}
Hitcheck. isintersect (INT X1, int Y1, int X2, int Y2); the method is simple to judge the collision of an object:
/// <Summary> <br/> // collision detection <br/> /// </Summary> <br/> public class hitcheck <br/> {<br /> Public static bool isintersect (INT X1, int Y1, int X2, int Y2) <br/>{< br/> rectangle R1 = new rectangle (x1, Y1, utility. gridsize, utility. gridsize); <br/> rectangle r2 = new rectangle (X2, Y2, utility. gridsize-4, utility. gridsize-4); <br/> If (r1.intersectswith (R2) <br/>{< br/> return true; <br/>}< br/> else <br/>{< br/> return false; <br/>}< br/>}
In this way, when the game status is updated cyclically, monsters can move freely without obstacles:
For (INT I = 0; I <enemys. count; I ++) <br/>{< br/> If (passable (enemys [I]) <br/>{< br/> enemys [I]. move (); <br/> If (10 = random. next (50) <br/>{< br/> autochangedirection (enemys [I]); <br/>}< br/> else <br/> {<br/> autochangedirection (enemys [I]); <br/>}< br/>}