Exemplo n.º 1
0
        //Handles individual tiles at individual layers
        //Some things shouldn't be done by the loop such as exiting the game - so use the calledByLoop to differentiate between the two
        //if this gets messy could split this function into two for each use
        void tileHandler(CCPoint world, CCTileMapLayer layer, character oldUser, Boolean calledByLoop=false)
        {
            CCTileMapCoordinates tileAtXy = layer.ClosestTileCoordAtNodePosition(world); // get tile coordinates corresponding to our touch location
            CCTileGidAndFlags info = layer.TileGIDAndFlags(tileAtXy.Column, tileAtXy.Row);
            //Tilemaps made in the "tiled" program can hold properties - get the properties of the tile
            Dictionary<string, string> properties = TilePropertiesForGID(info.Gid); 

            //Clicking a walkable tile
            if (!calledByLoop && properties != null && properties.ContainsKey("walkable") && properties["walkable"] == "true")
            {
                //if tile has an enemy and user is near it
                if(isTileOccupied(tileAtXy) && isUserNear(tileAtXy))
                {
                    character attackedEnemy = getEnemyAt(tileAtXy);
                    if (attackedEnemy.attacked(user.weapon.attack))
                        enemyDeath(attackedEnemy);

                    foreach (character enemy in enemiesList)
                    {
                        CCTileMapCoordinates positionAsTile = LayerNamed("Map").ClosestTileCoordAtNodePosition(enemy.Position);
                        if (!isUserNear(positionAsTile))
                            enemy.moveOneRandom();
                        else
                        {
                                if (user.attacked(enemy.weapon.attack) && !ifdied)
                                {
                                    userDeath();
                                    break;
                                }
                            
                        }
                    }
                }
                //TODO - if tile has an item, pick it up
                if(!isTileOccupied(tileAtXy))
                    userMoves = user.move(world); //get new pathfind
            }
            //Spawning in the user - only happens once - not called by user
            if (user == null && calledByLoop && properties != null && properties.ContainsKey("name") && properties["name"] == "exit")
            {
                if(oldUser==null)
                {
                    user = new character("userChar", 20, world, availableWeapons[0]);
                    layer.AddChild(user);
                }
                else
                {
                    user = new character(oldUser, world);
                    layer.AddChild(user);                }
            }
        }