Android Open Source - clash_of_balls Game Player






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/*w w  w .  ja  v  a2s .  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.game;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import android.graphics.Color;
import android.opengl.GLES20;
import android.util.Log;

import com.sapos_aplastados.game.clash_of_balls.R;
import com.sapos_aplastados.game.clash_of_balls.Font2D;
import com.sapos_aplastados.game.clash_of_balls.FontNumbers;
import com.sapos_aplastados.game.clash_of_balls.Texture;
import com.sapos_aplastados.game.clash_of_balls.TextureManager;
import com.sapos_aplastados.game.clash_of_balls.VertexBufferFloat;
import com.sapos_aplastados.game.clash_of_balls.ShaderManager.ShaderType;
import com.sapos_aplastados.game.clash_of_balls.game.GameItem.ItemType;
import com.sapos_aplastados.game.clash_of_balls.game.event.EventGameInfo.PlayerInfo;

/**
 * this represents a player of the game. ie a ball 
 *
 */
public class GamePlayer extends DynamicGameObject {
  
  private float m_max_speed = 7.f;

  private float m_color[]=new float[4]; //RGBA
  
  private float m_scaling=1.f; //for drawing, used for dying effect
  private float m_scaling_speed;
  
  
  public float m_radius;
  public float m_radius_dest; //m_radius should change to this value
  private static final int SMALL_RADIUS = 0;
  private static final int NORMAL_RADIUS = 1;
  private static final int LARGE_RADIUS = 2;
  private static final float m_normal_radius = 0.5f;
  private static final float m_radiuses[] = new float[] {
    m_normal_radius / 2.f, m_normal_radius, m_normal_radius*1.3f 
    };
  private FixtureDef m_radius_fixtures[]=new FixtureDef[m_radiuses.length];
  private Fixture m_cur_fixture;
  
  private static final float normal_restitution = 1.0f;
  private static final float high_restitution = 2.0f;
  
  //item
  private float m_item_timeout = 0.f;
  public ItemType m_item_type = ItemType.None;
  private GameItem m_overlay_item = null;
  public static final float overlay_item_height = 0.09f;
  private FontNumbers m_overlay_font_numbers;
  
  public float[] color() { return m_color; }
  
  private Vector m_acceleration = new Vector();
  private float m_sensor_scaling = 8.f; //influences the acceleration
  public Vector acceleration() { return m_acceleration; }

  public void applySensorVector(Vector v) {
    if(!m_bIs_dead) {
      m_acceleration.set(v);
      if(m_item_type == ItemType.InvertControls)
        m_acceleration.mul(-1.f);
      m_acceleration.mul(m_sensor_scaling);
    }
  }
  
  protected Texture m_overlay_texture;
  protected Texture m_glow_texture;
  protected Texture m_shadow_texture;
  private static final float min_glow_scaling = 0.8f;
  private static final float max_glow_scaling = 1.5f;
  private float m_glow_scaling=min_glow_scaling;
  private float m_texture_scaling = 1.f;

  public GamePlayer(GameBase owner, short id, Vector position
      , int color, TextureManager tex_manager
      , FontNumbers overlay_font_numbers, World world, BodyDef body_def) {
    super(owner, id, Type.Player,
      tex_manager!=null ? tex_manager.get(R.raw.texture_ball_base) : null);
    initTextures(tex_manager);
    RenderHelper.initColorArray(color, m_color);
    m_overlay_font_numbers = overlay_font_numbers;
    initPlayerBody(world, position, body_def);
  }
  
  public GamePlayer(PlayerInfo info, GameBase owner, TextureManager tex_manager
      , FontNumbers overlay_font_numbers, World world
      , BodyDef body_def) {
    super(owner, info.id, Type.Player
      , tex_manager!=null ? tex_manager.get(R.raw.texture_ball_base) : null);
    initTextures(tex_manager);
    RenderHelper.initColorArray(info.color, m_color);
    m_overlay_font_numbers = overlay_font_numbers;
    initPlayerBody(world, new Vector(info.pos_x, info.pos_y), body_def);
  }
  private void initTextures(TextureManager tex_manager) {
    if(tex_manager!=null) {
      m_overlay_texture = tex_manager.get(R.raw.texture_ball_up);
      m_glow_texture = tex_manager.get(R.raw.texture_ball_hover);
      m_shadow_texture = tex_manager.get(R.raw.texture_ball_shadow);
    }
  }
  
  private void initPlayerBody(World world, Vector position, BodyDef body_def) {
    body_def.type = BodyType.DYNAMIC;
    body_def.position.set(position.x, position.y);
    body_def.fixedRotation = true;
    body_def.linearDamping = 2.0f; //speed friction
    body_def.angle = 0.f;
    body_def.userData = this;
    m_body = world.createBody(body_def);
    
    for(int i=0; i<m_radiuses.length; ++i) {
      m_radius_fixtures[i] = createCircleFixtureDef(1.0f, 0.0f, normal_restitution, 
          0.f, 0.f, m_radiuses[i]);
      m_radius_fixtures[i].filter.categoryBits = COLLISION_GROUP_NORMAL;
      m_radius_fixtures[i].filter.maskBits = COLLISION_GROUP_NORMAL;
    }
    
    //apply normal radius
    m_cur_fixture = m_body.createFixture(m_radius_fixtures[NORMAL_RADIUS]);
    m_radius = m_radius_dest = m_radiuses[NORMAL_RADIUS];
    
    //hole collisions: use a point fixture (really small circle)
    FixtureDef fixture_def = createCircleFixtureDef(1.0f, 0.0f, 0.0f, 
        0.f, 0.f, 0.01f);
    fixture_def.filter.categoryBits = COLLISION_GROUP_NORMAL;
    fixture_def.filter.maskBits = COLLISION_GROUP_HOLE;
    m_body.createFixture(fixture_def);
  }
  
  private Vector m_tmp_speed = new Vector();
  private Vec2 m_tmp_vec = new Vec2();
  
  public void move(float dsec) {
    super.move(dsec);
    
    
    //update speed
    m_tmp_speed.set(m_body.getLinearVelocity().x, m_body.getLinearVelocity().y);
    
    m_tmp_speed.x += dsec * m_acceleration.x;
    m_tmp_speed.y += dsec * m_acceleration.y;
    
    float speed = m_tmp_speed.length();
    if(speed > m_max_speed) m_tmp_speed.mul(m_max_speed / speed);
    
    m_tmp_vec.set(m_tmp_speed.x, m_tmp_speed.y);
    m_body.setLinearVelocity(m_tmp_vec);
    
    //current item
    if(m_item_type != ItemType.None) {
      if(m_item_timeout - dsec <= 0.f) {
        disableItem();
      } else {
        m_item_timeout-=dsec;
      }
    }
    //move item if needed ...
    if(m_item_type == ItemType.DontFall) {
      if(m_glow_scaling < max_glow_scaling) {
        m_glow_scaling += dsec * 0.5f;
        if(m_glow_scaling > max_glow_scaling)
          m_glow_scaling = max_glow_scaling;
      }
    } else {
      if(m_glow_scaling > min_glow_scaling) {
        m_glow_scaling -= dsec * 0.5f;
        if(m_glow_scaling < min_glow_scaling) 
          m_glow_scaling = min_glow_scaling;
      }
    }
  }
  
  
  public void moveClient(float dsec) {
    super.moveClient(dsec);
    
    if(m_bIs_dying) {
      m_scaling -= (m_scaling_speed + 1.5f*dsec) * dsec;
      m_scaling_speed += dsec * 2.f*1.5f;
      if(m_scaling < 0.01f) {
        m_bIs_dying = false;
        m_scaling = 0.01f;
      }
    } else {
      //is radius changing?
      if(m_radius != m_radius_dest) {
        if(m_radius < m_radius_dest) {
          m_radius += dsec * 0.5f;
          if(m_radius > m_radius_dest) m_radius = m_radius_dest;
        } else {
          m_radius -= dsec * 0.5f;
          if(m_radius < m_radius_dest) m_radius = m_radius_dest;
        }
      }
    }
    
  }
  
  public void handleImpact(StaticGameObject other, Vector normal) {
    super.handleImpact(other, normal);
    switch(other.type) {
    case Hole:
      if(m_owner.is_server && m_item_type != ItemType.DontFall) {
        die();
        m_body.setLinearDamping(0.5f);
        m_tmp_vec.set(normal.x, normal.y);
        m_body.setLinearVelocity(m_tmp_vec);
      }
      break;
    case Item: applyItem((GameItem) other);
      break;
    default:
    }
  }
  
  public void applyItem(GameItem item) {
    float new_duration = GameItem.item_effect_duration;
    //if we already have this item, we accumulate the time
    if(item.itemType() == m_item_type) new_duration += m_item_timeout;
    if(new_duration > GameItem.item_effect_duration*5.f)
      new_duration = GameItem.item_effect_duration*5.f;
    
    //we only allow one item at a time
    disableItem();
    
    m_item_type = item.itemType();
    switch(item.itemType()) {
    case IncreaseMaxSpeed: 
      m_max_speed *= 1.6f;
      m_body.setLinearDamping(m_body.getLinearDamping() / 2.f);
      break;
    case InvertControls:
      break;
    case InvisibleToOthers:
      break;
    case IncreaseMassAndSize:
      setRadius(LARGE_RADIUS);
      break;
    case IncreaseRestitution:
      setRestitution(high_restitution);
      break;
    case DecreaseMassAndSize:
      setRadius(SMALL_RADIUS);
      break;
    case DontFall:
      break;
    }
    m_item_timeout = new_duration;
    Vector position=new Vector(0.f, 0.f);
    m_overlay_item = m_owner.createItem((short)-1, item.itemType(), position);
    m_overlay_item.setIsStatic(true);
  }
  
  private void disableItem() {
    if(m_item_type != ItemType.None) {
      switch(m_item_type) {
      case IncreaseMaxSpeed: 
        m_max_speed /= 1.6f;
        m_body.setLinearDamping(m_body.getLinearDamping() * 2.f);
        break;
      case InvertControls:
        break;
      case InvisibleToOthers:
        break;
      case IncreaseMassAndSize:
        setRadius(NORMAL_RADIUS);
        break;
      case IncreaseRestitution:
        setRestitution(normal_restitution);
        break;
      case DecreaseMassAndSize:
        setRadius(NORMAL_RADIUS);
        break;
      case DontFall:
        break;
      }
      
      m_item_timeout = 0.f;
      m_item_type = ItemType.None;
      m_overlay_item=null;
    }
    
  }
  
  private void setRestitution(float restitution) {
    m_cur_fixture.setRestitution(restitution);
  }
  
  private void setRadius(int which_radius) {
    m_radius_dest = m_radiuses[which_radius];
    final float restitution = m_cur_fixture.getRestitution();
    m_body.destroyFixture(m_cur_fixture);
    if(!m_bIs_dead) {
      m_cur_fixture = m_body.createFixture(m_radius_fixtures[which_radius]);
      m_cur_fixture.setRestitution(restitution);
    }
  }
  
  
  private boolean isInvisible() {
    return m_item_type == ItemType.InvisibleToOthers
        && m_owner.ownPlayer()!=this;
  }
  
  
  public void die() {
    if(!m_bIs_dead) {
      disableItem();
      m_acceleration.set(0.f, 0.f);
      m_scaling = 1.f;
      m_scaling_speed = 0.f;
      m_bIs_dead = true;
      m_bIs_dying = true;
      m_owner.handleObjectDied(this);
    }
  }
  
  //draw game overlay information
  //this is called using screen coordinates
  public void drawOverlay(RenderHelper renderer) {
    if(m_overlay_item!=null) {
      final float offset = renderer.m_screen_width*0.02f;
      final float item_size = renderer.m_screen_height*overlay_item_height;
      
      renderer.pushModelMat();
      renderer.modelMatTranslate(offset+item_size*0.5f, offset+item_size*0.5f, 0.f);
      renderer.modelMatScale(item_size, item_size, 0.f);
      m_overlay_item.draw(renderer);
      renderer.popModelMat();
      
      if(m_overlay_font_numbers!=null) {
        m_overlay_font_numbers.draw(renderer, Math.max((int)m_item_timeout + 1, 0)
            , offset*2.f + item_size, offset, item_size);
      }
    }
  }
  
  public void drawBackground(RenderHelper renderer) {
    if(!isReallyDead() && !isInvisible()) {
      
      if(m_shadow_texture != null) {
        m_texture_scaling = 1.15f;
        doModelTransformation(renderer, -0.15f, -0.1f);
        
        renderer.shaderManager().activateTexture(0);
        m_shadow_texture.useTexture(renderer);
        
        renderer.apply();
        
            // Draw
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);     
            
            undoModelTransformation(renderer);
            m_texture_scaling = 1.f;
      }
      
      if(m_glow_texture!=null && (m_item_type == ItemType.DontFall 
          || m_glow_scaling > min_glow_scaling)) {
        
        m_texture_scaling = m_glow_scaling;
        doModelTransformation(renderer);
        
        renderer.shaderManager().useShader(ShaderType.TypeWarp);
        Game.applyDefaultPosAndColor(renderer);
        
        renderer.shaderManager().activateTexture(0);
        m_glow_texture.useTexture(renderer);
        
        renderer.apply();
        
            // Draw
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        
        undoModelTransformation(renderer);
        renderer.shaderManager().useShader(ShaderType.TypeDefault);
        Game.applyDefaultPosAndColor(renderer);
        m_texture_scaling = 1.f;
      }
    }
  }
  
  public void draw(RenderHelper renderer) {
    if(!isReallyDead() && !isInvisible()) {
      
      doModelTransformation(renderer);
      
      //colored texture: m_texture
      
      if(m_texture != null) {
        renderer.shaderManager().activateTexture(0);
        m_texture.useTexture(renderer);
        
            // color
        int color_handle = renderer.shaderManager().u_Color_handle;
        if(color_handle != -1)
          GLES20.glUniform4fv(color_handle, 1, m_color, 0);
        
        renderer.apply();
        
            // Draw
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);     
            
      }
      
          
      //overlay texture
      
      if(m_overlay_texture != null) {
        Game.applyDefaultPosAndColor(renderer);
        
        renderer.shaderManager().activateTexture(0);
        m_overlay_texture.useTexture(renderer);
        
        renderer.apply();
        
            // Draw
            GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
      }
          
          undoModelTransformation(renderer);
      
    }
  }
  
  protected void doModelTransformation(RenderHelper renderer) {
    //scale & translate
    renderer.pushModelMat();
    renderer.modelMatTranslate(m_body.getPosition().x, m_body.getPosition().y, 0.f);
    renderer.modelMatScale(m_texture_scaling * m_scaling*m_radius*2.f
        , m_texture_scaling * m_scaling*m_radius*2.f, 0.f);
    renderer.modelMatTranslate(-0.5f, -0.5f, 0.f);
  }
  protected void doModelTransformation(RenderHelper renderer
      , float offset_x, float offset_y) {
    //scale & translate
    renderer.pushModelMat();
    renderer.modelMatTranslate(m_body.getPosition().x, m_body.getPosition().y, 0.f);
    renderer.modelMatScale(m_texture_scaling * m_scaling*m_radius*2.f
        , m_texture_scaling * m_scaling*m_radius*2.f, 0.f);
    renderer.modelMatTranslate(-0.5f+offset_x, -0.5f+offset_y, 0.f);
  }
  protected void undoModelTransformation(RenderHelper renderer) {
    renderer.popModelMat();
  }

}




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