Android Open Source - clash_of_balls Game Level






From Project

Back to project page clash_of_balls.

License

The source code is released under:

GNU General Public License

If you think the Android project clash_of_balls listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012-2013 Hans Hardmeier <hanshardmeier@gmail.com>
 * Copyright (C) 2012-2013 Andrin Jenal//from   ww  w .  ja  v  a  2s.  co m
 * Copyright (C) 2012-2013 Beat Kng <beat-kueng@gmx.net>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
package com.sapos_aplastados.game.clash_of_balls;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;

import android.content.Context;
import android.util.Log;
import au.com.bytecode.opencsv.CSVReader;

import com.sapos_aplastados.game.clash_of_balls.R;
import com.sapos_aplastados.game.clash_of_balls.helper.RawResourceReader;

/**
 * load a level from a file
 *
 * file format: csv
 * <width>, <height>, <level name>
 * 2*<width> columns with <bg>, <fg>
 * ...
 * <height> rows
 * 
 * 
 * <bg>, <fg>: int values for background & foreground object/texture
 * 
 */
public class GameLevel {
  
  private static final String LOG_TAG = "GameLevel";
  
  private Context m_context;
  
  public String name;
  public int width;
  public int height;
  
  public int player_count;
  
  
  private int[] m_background;
  public int background(int x, int y) { return m_background[y*width+x]; }
  
  private int[] m_foreground;
  public int foreground(int x, int y) { return m_foreground[y*width+x]; }
  
  
  //field specification: all must be <128 !
  //fg
  //when objects are added change GameField to load correct object
  public static final int TYPE_EMPTY = 0;
  public static final int TYPE_PLAYER = 1;
  //Boarder of field
  public static final int TYPE_BOARDER_LEFT = 2;
  public static final int TYPE_BOARDER_RIGHT = 3;
  public static final int TYPE_BOARDER_UP = 4;
  public static final int TYPE_BOARDER_DOWN = 5;
  //Corners of field: these are also holes
  public static final int TYPE_CORNER_UP_RIGHT = 6;
  public static final int TYPE_CORNER_DOWN_RIGHT = 7;
  public static final int TYPE_CORNER_DOWN_LEFT = 8;
  public static final int TYPE_CORNER_UP_LEFT = 9;
  //Holes 
  public static final int TYPE_HOLE = 10; //single round hole, only 1 tile
  public static final int TYPE_HOLE_FULL = 11;
  public static final int TYPE_HOLE_CORNER_UP_RIGHT = 12;
  public static final int TYPE_HOLE_CORNER_DOWN_RIGHT = 13;
  public static final int TYPE_HOLE_CORNER_DOWN_LEFT = 14;
  public static final int TYPE_HOLE_CORNER_UP_LEFT = 15;
  
  public static final int TYPE_HOLE_HEAD_UP = 16;
  public static final int TYPE_HOLE_HEAD_DOWN = 17;
  public static final int TYPE_HOLE_HEAD_LEFT = 18;
  public static final int TYPE_HOLE_HEAD_RIGHT = 19;
  //Wall
  public static final int TYPE_WALL_HOR = 20;
  public static final int TYPE_WALL_VERT = 21;
  public static final int TYPE_WALL_CROSS = 26;
  
  public static final int TYPE_WALL_CORNER_UP_RIGHT = 22;
  public static final int TYPE_WALL_CORNER_DOWN_RIGHT = 23;
  public static final int TYPE_WALL_CORNER_DOWN_LEFT = 24;
  public static final int TYPE_WALL_CORNER_UP_LEFT = 25;
  
  public static final int TYPE_FG_MAX = 26; // max field int value foreground
  
  public static int rawResTexIdFromForeground(int fg_field) {
    switch(fg_field) {
    case TYPE_PLAYER: return -1;
    
    case TYPE_BOARDER_LEFT:;
    case TYPE_BOARDER_RIGHT:;
    case TYPE_BOARDER_DOWN:;
    case TYPE_BOARDER_UP: return R.raw.texture_border;
    
    case TYPE_CORNER_UP_RIGHT:;
    case TYPE_CORNER_DOWN_RIGHT:;
    case TYPE_CORNER_UP_LEFT:;
    case TYPE_CORNER_DOWN_LEFT: return R.raw.texture_corner;
    
    case TYPE_HOLE: return R.raw.texture_hole;
    case TYPE_HOLE_FULL: return R.raw.texture_hole_full;
    
    case TYPE_HOLE_CORNER_DOWN_LEFT:;
    case TYPE_HOLE_CORNER_DOWN_RIGHT:;
    case TYPE_HOLE_CORNER_UP_LEFT:;
    case TYPE_HOLE_CORNER_UP_RIGHT:return R.raw.texture_hole_corner;
    
    case TYPE_HOLE_HEAD_LEFT:;
    case TYPE_HOLE_HEAD_RIGHT:;
    case TYPE_HOLE_HEAD_UP:;
    case TYPE_HOLE_HEAD_DOWN:return R.raw.texture_hole_head;
    
    case TYPE_WALL_HOR:;
    case TYPE_WALL_VERT: return R.raw.texture_wall;
    
    case TYPE_WALL_CROSS: return R.raw.texture_wallcross;
    
    case TYPE_WALL_CORNER_DOWN_LEFT:;
    case TYPE_WALL_CORNER_DOWN_RIGHT:;
    case TYPE_WALL_CORNER_UP_LEFT:;
    case TYPE_WALL_CORNER_UP_RIGHT:return R.raw.texture_wallcorner;
    
    }
    return -1;
  }
  
  //bg
  public static final int TYPE_NORMAL = 0;
  public static final int TYPE_BG_BRICK = 1;
  public static final int TYPE_BG_STONE = 2;
  public static final int TYPE_BG_MAX = 2; // max field int value background
  
  public static int rawResTexIdFromBackground(int bg_field) {
    switch(bg_field) {
    case TYPE_NORMAL: return R.raw.texture_game_bg;
    case TYPE_BG_BRICK: return R.raw.texture_game_bg_brick;
    case TYPE_BG_STONE: return R.raw.texture_game_bg_stone;
    }
    return -1;
  }
  
  public GameLevel(Context context) { 
    m_context = context;
  }
  
  //load from raw resource
  public void loadLevel(int raw_res_id) throws Exception {
    loadLevelImpl(RawResourceReader.readFromRawResource(
        m_context, raw_res_id));
  }
  
  //load from filesystem
  public void loadLevel(String file_name) throws Exception {
    BufferedReader fileReader = new BufferedReader(new FileReader(file_name));
    loadLevelImpl(fileReader);
  }
  
  //load from network buffer
  public void loadLevel(DataInputStream s) throws IOException {
    width = s.readInt();
    height = s.readInt();
    player_count = (int)s.readShort();
    m_background = new int[width*height];
    m_foreground = new int[width*height];
    for(int i=0; i<width*height; ++i) {
      m_background[i] = (int)s.readByte();
      m_foreground[i] = (int)s.readByte();
    }
  }
  //write to network buffer
  public void write(DataOutputStream s) throws IOException {
    s.writeInt(width);
    s.writeInt(height);
    s.writeShort((short)player_count);
    for(int i=0; i<width*height; ++i) {
      s.writeByte(m_background[i]);
      s.writeByte(m_foreground[i]);
    }
  }
  
  private void loadLevelImpl(Reader stream) throws Exception {
    player_count = 0;
    CSVReader reader = new CSVReader(stream);
    try {
      Iterator<String[]> iter = reader.readAll().iterator();
      //read the size
      assertFormat(iter.hasNext());
      String[] line = iter.next();
      assertFormat(line.length >= 3);
      width = Integer.parseInt(line[0]);
      height = Integer.parseInt(line[1]);
      name = line[2];
      
      assertFormat(width > 0 && height > 0);
      
      Log.d(LOG_TAG, "Game Level: w="+width+", h="+height+", name="+name);
      
      m_background = new int[width*height];
      m_foreground = new int[width*height];
      
      for(int y=0; y<height; ++y) {
        assertFormat(iter.hasNext());
        line = iter.next();
        assertFormat(line.length >= 2*width);
        
        String log_line = "";
        for(int x=0; x<width; ++x) {
          //background
          int bg = Integer.parseInt(line[x*2].trim());
          assertFormat(bg >= 0 && bg <= TYPE_BG_MAX);
          m_background[(height-y-1)*width+x] = bg;
          //foreground
          int fg = Integer.parseInt(line[x*2+1].trim());
          assertFormat(fg >= 0 && fg <= TYPE_FG_MAX);
          if(fg==TYPE_PLAYER) ++player_count;
          m_foreground[(height-y-1)*width+x] = fg;
          
          log_line += ""+bg+", "+fg+"; ";
        }
        
        Log.v(LOG_TAG, log_line);
      }
      
      Log.d(LOG_TAG, "Players: "+player_count);
      
      
    } finally {
      reader.close();
    }
  }
  
  private void assertFormat(boolean val) throws Exception {
    if(!val) throw new Exception("level: file format error");
  }
}




Java Source Code List

au.com.bytecode.opencsv.CSVParser.java
au.com.bytecode.opencsv.CSVReader.java
au.com.bytecode.opencsv.CSVWriter.java
au.com.bytecode.opencsv.ResultSetHelperService.java
au.com.bytecode.opencsv.ResultSetHelper.java
com.sapos_aplastados.game.clash_of_balls.FPSCounter.java
com.sapos_aplastados.game.clash_of_balls.Font2D.java
com.sapos_aplastados.game.clash_of_balls.FontNumbers.java
com.sapos_aplastados.game.clash_of_balls.GameLevel.java
com.sapos_aplastados.game.clash_of_balls.GameRenderer.java
com.sapos_aplastados.game.clash_of_balls.GameSettings.java
com.sapos_aplastados.game.clash_of_balls.ITouchInput.java
com.sapos_aplastados.game.clash_of_balls.LevelManager.java
com.sapos_aplastados.game.clash_of_balls.MainActivity.java
com.sapos_aplastados.game.clash_of_balls.Maths.java
com.sapos_aplastados.game.clash_of_balls.NetworkService.java
com.sapos_aplastados.game.clash_of_balls.ShaderManager.java
com.sapos_aplastados.game.clash_of_balls.TextureBase.java
com.sapos_aplastados.game.clash_of_balls.TextureManager.java
com.sapos_aplastados.game.clash_of_balls.Texture.java
com.sapos_aplastados.game.clash_of_balls.UIBase.java
com.sapos_aplastados.game.clash_of_balls.UIHandler.java
com.sapos_aplastados.game.clash_of_balls.VertexBufferFloat.java
com.sapos_aplastados.game.clash_of_balls.game.DynamicGameObject.java
com.sapos_aplastados.game.clash_of_balls.game.GameBase.java
com.sapos_aplastados.game.clash_of_balls.game.GameField.java
com.sapos_aplastados.game.clash_of_balls.game.GameHole.java
com.sapos_aplastados.game.clash_of_balls.game.GameItem.java
com.sapos_aplastados.game.clash_of_balls.game.GameMenuInGame.java
com.sapos_aplastados.game.clash_of_balls.game.GameObject.java
com.sapos_aplastados.game.clash_of_balls.game.GamePlayer.java
com.sapos_aplastados.game.clash_of_balls.game.GameServer.java
com.sapos_aplastados.game.clash_of_balls.game.GameStatistics.java
com.sapos_aplastados.game.clash_of_balls.game.GameView.java
com.sapos_aplastados.game.clash_of_balls.game.GameWall.java
com.sapos_aplastados.game.clash_of_balls.game.Game.java
com.sapos_aplastados.game.clash_of_balls.game.IDrawable.java
com.sapos_aplastados.game.clash_of_balls.game.IMoveable.java
com.sapos_aplastados.game.clash_of_balls.game.RenderHelper.java
com.sapos_aplastados.game.clash_of_balls.game.SensorThread.java
com.sapos_aplastados.game.clash_of_balls.game.StaticGameObjectNoInteraction.java
com.sapos_aplastados.game.clash_of_balls.game.StaticGameObject.java
com.sapos_aplastados.game.clash_of_balls.game.Vector3D.java
com.sapos_aplastados.game.clash_of_balls.game.Vector.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameEnd.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameInfo.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameStartNow.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventImpact.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemAdded.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemRemoved.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemUpdate.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventPool.java
com.sapos_aplastados.game.clash_of_balls.game.event.Event.java
com.sapos_aplastados.game.clash_of_balls.helper.RawResourceReader.java
com.sapos_aplastados.game.clash_of_balls.helper.ShaderHelper.java
com.sapos_aplastados.game.clash_of_balls.helper.TextureHelper.java
com.sapos_aplastados.game.clash_of_balls.menu.CreationMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.GameMenuBase.java
com.sapos_aplastados.game.clash_of_balls.menu.HelpMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.JoinMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.MainMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuBackground.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemArrow.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemButton.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemGreyButton.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemImg.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemKeyboard.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemLevel.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemList.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemResultEntry.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemStringMultiline.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemString.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItem.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupBase.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupCredit.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupGameStart.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupMsg.java
com.sapos_aplastados.game.clash_of_balls.menu.ResultsMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.WaitMenu.java
com.sapos_aplastados.game.clash_of_balls.network.AlljoynInterface.java
com.sapos_aplastados.game.clash_of_balls.network.NetworkClient.java
com.sapos_aplastados.game.clash_of_balls.network.NetworkServer.java
com.sapos_aplastados.game.clash_of_balls.network.Networking.java