private GameLayer(character oldUser, int prevLevel) : base("level_" + CCRandom.GetRandomInt(3, numLevels) + ".tmx"/*"level_4.tmx"*/) // get a random level and load it on initialization { //Enemy weapons are stronger based on level foreach (weapon weapon in availableWeapons) { weapon.attack+=prevLevel; } level=prevLevel+1; character.map = this; //touch listener - calles tileHandler to handle tile touches touchListener = new CCEventListenerTouchAllAtOnce(); touchListener.OnTouchesEnded = handleEndTouches; AddEventListener(touchListener); loopTiles(oldUser); //Schedule the main game loop enemiesList = placeEnemiesRandomly(); foreach (character enemy in enemiesList) this.AddChild(enemy); //Add labels to the lower left hand corner //Might be better to have a bar with width based on a percentage of health/maxhealth userInfo = new CCLabel ("Health: " + user.health + "/" + user.maxHealth + " Attack : " + user.weapon.attack, "arial", 12); userInfo.Position = new CCPoint(70, VisibleBoundsWorldspace.UpperRight.Y + 5); userInfo.IsAntialiased = true; this.AddChild(userInfo); //run main game loop - frames happen every 0.25 second Schedule(RunGameLogic, (float)0.25); }
//create new character based on old in new location public character(character oldCharacter, CCPoint setLocation) : base() { myChar = oldCharacter.myChar; //set sprite based on the name of a passed in string this.AddChild(myChar); //connect the sprite to the character this.Position = setLocation; health = oldCharacter.health; maxHealth = oldCharacter.maxHealth; weapon = oldCharacter.weapon; this.AddChild(weapon); AddChild(weapon); }
//Carry user over to next level private static void getNewMap(CCGameView gameView, character user, int level) { CCScene gamePlayScene = new CCScene(gameView); gamePlayScene.AddLayer(new GameLayer(user, level)); gameView.Director.ReplaceScene(gamePlayScene); }
void enemyDeath(character enemy) { //give user enemy weapon if it's better if(enemy.weapon.attack > user.weapon.attack) { user.RemoveChild(user.weapon); user.weapon = enemy.weapon; user.AddChild(user.weapon); } enemiesList.Remove(enemy); this.RemoveChild(enemy); //TODO - drop item }
//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); } } }
//Calls the tileHandler on EVERY map tile void loopTiles(character oldUser) { int tileDimension = (int)TileTexelSize.Width; int numberOfColumns = (int)MapDimensions.Size.Width; int numberOfRows = (int)MapDimensions.Size.Height; CCPointI world = new CCPointI(0, 0); // Tile maps can have multiple layers, so let's loop through all of them: foreach (CCTileMapLayer layer in TileLayersContainer.Children) { // Loop through the columns and rows to find all tiles for (int column = 0; column < numberOfColumns; column++) { // We're going to add tileDimension / 2 to get the position // of the center of the tile - this will help us in // positioning entities, and will eliminate the possibility // of floating point error when calculating the nearest tile: world.X = tileDimension * column + tileDimension / 2; for (int row = 0; row < numberOfRows; row++) { // See above on why we add tileDimension / 2 world.Y = tileDimension * row + tileDimension / 2; tileHandler(world, layer, oldUser, true); } } } }