Android Game Development Game level game objects Base

  • Slides: 27
Download presentation
Android Game Development Game level, game objects

Android Game Development Game level, game objects

Base project Download our base project and open it in Net. Beans cg. iit.

Base project Download our base project and open it in Net. Beans cg. iit. bme. hu/gamedev/KIC/11_Android. Development/11 _02_Android_Engine. Basics_Final. zip Change the android sdk location path Per-user properties (local. properties) sdk. dir=change path here Start an emulator Build Run

Game levels The game is built up from game objects (Game. Object class) Game

Game levels The game is built up from game objects (Game. Object class) Game objects are the visible objects, the actors of the game They are unique They will be managed by the Level class Our levels will be generated proceduraly by the Level. Generator class These classes are game specific Place them to the Platform. Game package and not to the Engine

Game. Object Create the Game. Object class public class Game. Object { public enum

Game. Object Create the Game. Object class public class Game. Object { public enum Object. Type{ Unknown, Ground, Pit, Floor 2, Main. Character, Monster, Useful. Things, protected Object. Type type = Object. Type. Unknown; protected String name; public Game. Object(String name){ this. name = name; init. Impl(); } public void set. Type(Object. Type type){ this. type = type; } public Object. Type get. Type(){ return type; } public String get. Name() { return this. name; } public void pre. Update(float t, float dt){} public void update(float t, float dt){} public void post. Update(float t, float dt){} public void destroy(){} protected void init. Impl(){} } Floor 1, Item }

Generic. Game. Object I. Create Generic. Game. Object class public class Generic. Game. Object

Generic. Game. Object I. Create Generic. Game. Object class public class Generic. Game. Object extends Game. Object { protected Mesh. Entity render. O = null; protected Scene. Node render. Node = null; public Generic. Game. Object(String name, Mesh. Entity entity) { super(name); if (entity != null) { this. render. O = entity; this. render. Node = Scene. Manager. get. Singleton(). get. Root. Node(). create. Child(); this. render. Node. attach. Object(this. render. O); } } public void destroy() { if (render. O != null) { render. Node. detach. Object(render. O); render. Node. get. Parent(). remove. Child(render. Node); } }

Generic. Game. Object II. public void update(float t, float dt) { if (this. render.

Generic. Game. Object II. public void update(float t, float dt) { if (this. render. Node != null) { this. render. O. update(t, dt); } } public void set. Position(float x, float y, float z) { if (this. render. Node != null) { this. render. Node. set. Position(x, y, z); } } public void set. Orientation(float yaw, float pitch, float roll) { if (this. render. Node != null) { this. render. Node. set. Orientation(yaw, pitch, roll); } } public Vector 3 get. Position() { return this. render. Node. get. Position(); } public Vector 3 get. Velocity() { return Vector 3. ZERO; } public Scene. Node get. Render. Node() { return render. Node; } }

Level I. Create Level class public class Level { private Hash. Map<String, Game. Object>

Level I. Create Level class public class Level { private Hash. Map<String, Game. Object> game. Object. Map = new Hash. Map(); private Linked. List<Game. Object> game. Objects = new Linked. List(); public void update(float t, float dt) { for (Game. Object o : this. game. Objects) o. update(t, dt); } public void pre. Update(float t, float dt) { for (Game. Object o : this. game. Objects) o. pre. Update(t, dt); } public void post. Update(float t, float dt) { for (Game. Object o : this. game. Objects) o. post. Update(t, dt); }

Level II. public void add. Game. Object(Game. Object o) throws Exception { if (this.

Level II. public void add. Game. Object(Game. Object o) throws Exception { if (this. game. Object. Map. contains. Key(o. get. Name())) { throw new Exception("Unable to add game object. Game object with the same name already exists : " + o. get. Name()); } } this. game. Object. Map. put(o. get. Name(), o); this. game. Objects. add(o); public void remove. Game. Object(String name){ Game. Object go = game. Object. Map. get(name); if(go != null) { this. game. Object. Map. remove(name); this. game. Objects. remove(go); go. destroy(); } } } public Game. Object get. Game. Object(String name) { return (Game. Object)this. game. Object. Map. get(name); }

Level. Generator I. Create Level. Generator class public class Level. Generator { protected Level

Level. Generator I. Create Level. Generator class public class Level. Generator { protected Level level; protected int level. Size = 30; protected float camera. Fov; protected float camera. Dist; protected float camera. Aspect; public Level. Generator(Level level, float camera. Fov, float camera. Aspect, float camera. Distance, Character 2 D main. Character) { this. camera. Fov = max. Camera. Fov; this. camera. Dist = max. Camera. Distance; this. camera. Aspect = camera. Aspect; this. level = level; } public Point get. Player. Start. Pos(){ return new Point(-level. Size + 1, 0); }

Level. Generator II. protected void create. Material(String name, String texture. Name, boolean blended, int

Level. Generator II. protected void create. Material(String name, String texture. Name, boolean blended, int texture. Repeat){ Texture t 1 = new Texture(name); t 1. set. File. Name(texture. Name); t 1. set. Repeat. U(texture. Repeat); t 1. load(); Texture. Manager. get. Singleton(). add(t 1); Material m 1 = new Material(name); m 1. set. Texture(t 1); m 1. load(); if(blended) m 1. set. Alhpa. Blend. Mode(Material. ALPHA_BLEND_MODE_BLEND); Material. Manager. get. Singleton(). add(m 1); } protected void create. Materials(){ create. Material("g_Background", "g_background. jpg", false, 1); create. Material("g_Middleground", "g_middleground. png", true, level. Size / 10); create. Material("g_Ground", "g_ground. jpg", false, 1); create. Material("Platform", "platform 1. png", false, 1); create. Material("g_Item", "g_item. png", false, 1); create. Material("g_Box", "g_box. jpg", false, 1); }

Level. Generator III. protected Generic. Game. Object create. Plane(String name, String material. Name, float

Level. Generator III. protected Generic. Game. Object create. Plane(String name, String material. Name, float scale. X, float scale. Y) throws Exception{ Mesh. Entity me = new Mesh. Entity(name, "Unit. Quad"); me. set. Mesh. Scale(scale. X, scale. Y, 1); me. set. Material(material. Name); Generic. Game. Object go = new Generic. Game. Object(name, me); level. add. Game. Object(go); return go; }

Level. Generator IV. public Level generate() { if (level == null) { level =

Level. Generator IV. public Level generate() { if (level == null) { level = new Level(); } create. Materials(); try { //set up backgrounds float background. Dist 1 = 1000; float background 1 Width = (float) (camera. Aspect * Math. tan(camera. Fov * 0. 5 f) * (background. Dist 1 + camera. Dist) + level. Size); float background 1 Height = (float) (Math. tan(camera. Fov * 0. 5 f) * (background. Dist 1 + camera. Dist)); Generic. Game. Object background 1 = create. Plane("background 1", "g_Background", background 1 Width, background 1 Height); background 1. set. Position(0, 0, -background. Dist 1); float background. Dist 2 = 30; float background 2 Width = (float) (camera. Aspect * Math. tan(camera. Fov * 0. 5 f) * (background. Dist 2 + camera. Dist) + level. Size); float background 2 Height = (float) (Math. tan(camera. Fov * 0. 5 f) * (background. Dist 2 + camera. Dist)); Generic. Game. Object background 2 = create. Plane("background 2", "g_Middleground", background 2 Width, background 2 Height); background 2. set. Position(0, 0, -background. Dist 2); } } catch(Exception e) { android. util. Log. e("Level. Generator", "unable to generate level " + e. to. String()); } return level;

Try our classes Main. Engine: public class Main. Engine { private long time. Start

Try our classes Main. Engine: public class Main. Engine { private long time. Start = 0; private long last. Time = 0; Level level; Camera camera; public void init(float ratio) { GLES 10. gl. Clear. Color(0. 5 F, 1. 0 F, 1. 0 F); GLES 10. gl. Hint(GLES 10. GL_PERSPECTIVE_CORRECTION_HINT, GLES 10. GL_NICEST); try { camera = new Camera("main. Camera"); camera. set. Type(Camera. TYPE_PERSECTIVE); camera. set. Persp. Fov(0. 5 f); camera. set. Near(0. 1 f); camera. set. Far(1100. 0 f); camera. set. Aspect(ratio); Scene. Node cam. Node = Scene. Manager. get. Singleton(). get. Root. Node(). create. Child(); cam. Node. attach. Object(camera); }catch (Exception e){ android. util. Log. e("Main. Engine init", "Unable to create camera ", e); } Level. Generator LG = new Level. Generator(null, 0. 5 f, ratio, 12. 0 f); level = LG. generate(); camera. get. Parent(). set. Position(LG. get. Player. Start. Pos(). x, 0, 12. 0 f); }

Try our classes public void update() { //get elapsed time … level. pre. Update(t,

Try our classes public void update() { //get elapsed time … level. pre. Update(t, dt); //update state level. update(t, dt); //render GLES 10. gl. Clear(GLES 10. GL_COLOR_BUFFER_BIT | GLES 10. GL_DEPTH_BUFFER_BIT); camera. apply(); Scene. Manager. get. Singleton(). render(); } level. post. Update(t, dt);

Compile and run

Compile and run

Level generation I. Level. Generator new member variables: protected Mesh. Entity ground. ME; protected

Level generation I. Level. Generator new member variables: protected Mesh. Entity ground. ME; protected Mesh. Entity floor 1 ME; protected Mesh. Entity floor 2 ME; // protected Mesh. Entity pit. ME;

Level generation II. Level. Generator new method: protected void create. Meshes(){ try{ ground. ME

Level generation II. Level. Generator new method: protected void create. Meshes(){ try{ ground. ME = new Mesh. Entity("ground", "Unit. Quad"); ground. ME. set. Mesh. Scale(1. 0 f, 1, 1); ground. ME. set. Material("g_Ground"); //pit. ME = new Mesh. Entity("pit", "Unit. Quad"); //pit. ME. set. Mesh. Scale(1. 0 f, 1, 1); //pit. ME. set. Material("Pit"); //also create this material!! floor 1 ME = new Mesh. Entity("floor 1", "Unit. Quad"); floor 1 ME. set. Mesh. Scale(1. 0 f, 0. 1 f, 1); floor 1 ME. set. Material("Platform"); floor 2 ME = new Mesh. Entity("floor 2", "Unit. Quad"); floor 2 ME. set. Mesh. Scale(1. 0 f, 0. 1 f, 1); floor 2 ME. set. Material("Platform"); //we can choose an other material too }catch(Exception e){ android. util. Log. e("Level. Generator", "Unable to create mesh entities " + e. get. Message()); } }

Level generation III. Level. Generator. generate() after create. Materials call: create. Meshes(); Level. Generator.

Level generation III. Level. Generator. generate() after create. Materials call: create. Meshes(); Level. Generator. generate() after background creation: … for(int i = 0; i < 3; i++) { Generic. Game. Object ground. L = new Generic. Game. Object("ground_l_" + i, ground. ME); ground. L. set. Position(- level. Size - 2 * i - 1. 0 f, -3. f, 0); level. add. Game. Object(ground. L); Generic. Game. Object ground. R = new Generic. Game. Object("ground_r_" + i, ground. ME); ground. R. set. Position(level. Size + 1. 0 f + 2. 0 f * i, -3. f, 0); level. add. Game. Object(ground. R); }

Compile and run

Compile and run

Level generation IV. protected int ground. Count = 0; protected void create. Ground. Element(float

Level generation IV. protected int ground. Count = 0; protected void create. Ground. Element(float pos. X) throws Exception{ Generic. Game. Object ground = new Generic. Game. Object("ground_" + ground. Count, ground. ME); ground. set. Type(Game. Object. Type. Ground); ground. set. Position(pos. X, -3. f, 0); level. add. Game. Object(ground); ground. Count++; } protected int pit. Count = 0; protected void create. Pit. Element(float pos. X) throws Exception{ Generic. Game. Object pit = new Generic. Game. Object("pit_" + pit. Count, null); // , pit. ME); pit. set. Type(Game. Object. Type. Pit); pit. set. Position(pos. X, -4. 5 f, 0); level. add. Game. Object(pit); pit. Count++; }

Level generation V. protected int floor 1 Count = 0; protected void create. Floor

Level generation V. protected int floor 1 Count = 0; protected void create. Floor 1 Element(float pos. X) throws Exception{ Generic. Game. Object floor 1 = new Generic. Game. Object("floor 1_" + floor 1 Count, floor 1 ME); floor 1. set. Type(Game. Object. Type. Floor 1); floor 1. set. Position(pos. X, -0. 8 f, 0); level. add. Game. Object(floor 1); floor 1 Count++; } protected int floor 2 Count = 0; protected void create. Floor 2 Element(float pos. X) throws Exception{ Generic. Game. Object floor 2 = new Generic. Game. Object("floor 2_" + floor 2 Count, floor 2 ME); floor 2. set. Type(Game. Object. Type. Floor 2); floor 2. set. Position(pos. X, 0. 6 f, 0); level. add. Game. Object(floor 2); floor 2 Count++; }

Level generation VI. protected int monster. Count = 0; protected void generate. Monster(float x,

Level generation VI. protected int monster. Count = 0; protected void generate. Monster(float x, float y)throws Exception{ } protected int item. Count = 0; protected void create. Item(float x, float y) throws Exception{ } protected int box. Count = 0; protected void create. Box(float x, float y) throws Exception{

Level generation VI. generate() after border floors generation: for(int i = 0; i <

Level generation VI. generate() after border floors generation: for(int i = 0; i < level. Size; ++i){ float pos. X = (float) i * 2 - (float) level. Size + 1. 0 f; double r = Math. random(); if(r > 0. 2 f || i < 3){ create. Ground. Element(pos. X); if(i > 3){ double rm = Math. random(); if(rm > 0. 8 f){ generate. Monster(pos. X, -1. 0 f); } double ri = Math. random(); if(ri > 0. 4 f){ create. Item(pos. X, -2. 0 f); }else if(ri > 0. 2 f){ create. Box(pos. X, -2. 0 f); } } } …

Level generation VI. } else{ create. Pit. Element(pos. X); } r = Math. random();

Level generation VI. } else{ create. Pit. Element(pos. X); } r = Math. random(); if(r > 0. 6 f){ create. Floor 1 Element(pos. X); double ri = Math. random(); if(ri > 0. 4 f){ create. Item(pos. X, -0. 8 f); }else if(ri > 0. 2 f){ create. Box(pos. X, -0. 8 f); } r = Math. random(); if(r > 0. 7 f){ create. Floor 2 Element(pos. X); ri = Math. random(); if(ri > 0. 4 f){ create. Item(pos. X, 0. 6 f); }else if(ri > 0. 2 f){ create. Box(pos. X, 0. 6 f); } } }

Explore the level Main. Engine on. Touch. Event: protected float last. X = 0;

Explore the level Main. Engine on. Touch. Event: protected float last. X = 0; public boolean on. Touch. Event(Motion. Event e) { if(e. get. Action() == Motion. Event. ACTION_MOVE){ float x = e. get. X(); float dx = (x - last. X) * 0. 005 f; Vector 3 old. Pos = camera. get. Parent(). get. Position(); camera. get. Parent(). set. Position(old. Pos. x() + dx, old. Pos. y(), old. Pos. z()); } if(e. get. Action() == Motion. Event. ACTION_DOWN){ last. X = e. get. X(); } return true; }

Build and run

Build and run

The End

The End