void MonsterProcess() { // move the monsters at random // // move the monster towards the player EachObject((m) => { List <Vector2i> foundPath = pf.FindPath(m.positionI, player.positionI, (x, y) => { return(ContainsType(x, y, RLCharacter.RLTypes.MONSTER) || ContainsType(x, y, RLCharacter.RLTypes.STAIRS_DOWN)?10000:1); }, map); // only move the enemy if the path is at least 2 steps long (the start and end positions) if (foundPath.Count >= 2) { Vector2i nextPosition = foundPath[foundPath.Count - 2]; if (map.IsValidTile(nextPosition.x, nextPosition.y) && !ContainsType(nextPosition.x, nextPosition.y, RLCharacter.RLTypes.MONSTER)) { if (player.positionI.x == nextPosition.x && player.positionI.y == nextPosition.y) { currentHealth--; if (currentHealth == 0) { // end game Debug.Log("Game Over"); } if (currentHealth >= 0) { hearts [currentHealth].gameObject.SetActive(false); } } else { m.SetPosition(nextPosition.x, nextPosition.y); } } } }, RLCharacter.RLTypes.MONSTER); // check for monster bullet overlap again OverlapCheck((b, m) => { objects.Remove(m); objects.Remove(b); Destroy(m.gameObject); Destroy(b.gameObject); }, RLCharacter.RLTypes.BULLET, RLCharacter.RLTypes.MONSTER); fsm.PerformTransition(FsmTransitionId.Complete); }
// get the lookup table index for the sprite, based on its neighbors int calculateIndex(int x, int y, Map map) { // calculate the binary index of the layer int i1 = (!map.IsValidTile(x+1, y)||map.IsOpenTile(x+1,y))?0:1; int i2 = (!map.IsValidTile(x,y-1)||map.IsOpenTile(x,y-1))?0:1; int i3 = (!map.IsValidTile(x-1,y)||map.IsOpenTile(x-1,y))?0:1; int i4 = (!map.IsValidTile(x,y+1)||map.IsOpenTile(x,y+1))?0:1; return (i4<<3) | (i3<<2) | (i2<<1) | (i1); }