tank battle - a simple game powered by jmonkey engine

26
Tank battle Farzad Nozarian - Vahid Ranaei Amirkabir university of technology

Upload: farzad-nozarian

Post on 11-Apr-2017

145 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Tank Battle - A simple game powered by JMonkey engine

Tank battleFarzad Nozarian - Vahid RanaeiAmirkabir university of technology

Page 2: Tank Battle - A simple game powered by JMonkey engine

References

Page 3: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Movement

Android Devices Orientation Sensors

Desktops ‘WASD’ Control Keys

VS.

Page 4: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Movement/Android Devices

public void onSensorChanged(SensorEvent sensorEvent) { if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

float headingAngle = sensorEvent.values[0];float pitchAngle = sensorEvent.values[1];float rollAngle = sensorEvent.values[2];// TODO Apply the orientation changes to your application.

}}

Page 5: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Movement/Desktop Devices

inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_U));inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_J));inputManager.addListener(this, "Lefts");inputManager.addListener(this, "Rights");

Page 6: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Movement/Desktop Devices

onActionhandler

if (binding.equals("Ups")) { if (value) { accelerationValue -= 800; } else { accelerationValue += 800; } player.accelerate(accelerationValue); } else if (binding.equals("Rights")) {

if (value) { steeringValue += -.5f; } else { steeringValue += .5f; } player.steer(steeringValue); }

Page 7: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Movement

Some Abstractions

Fire

Move Forward (Accelerate)

Move Around (Steer)…

Page 8: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Movement

vehicle.steer(steeringValue) vehicle.accelerate(accelerationValue)applyImpulse

resetSuspension

setPhysicsLocation

setPhysicsRotation

setLinearVelocity

Page 9: Tank Battle - A simple game powered by JMonkey engine

Pattern Definition

• Decouple an abstraction from its implementation so that the two can vary independently

• When abstractions and implementations should be extensible through subclassing

Bridge!

Page 10: Tank Battle - A simple game powered by JMonkey engine

Pattern Definition

Page 11: Tank Battle - A simple game powered by JMonkey engine

Pattern SolutionBridge

Page 12: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionTank Shooting

Shooting In Real World !

Burst mode

Selective fire

Automatic firearm

Bump firing

Same ‘Fire’ Different Behaviors ?!

Page 13: Tank Battle - A simple game powered by JMonkey engine

Pattern DefinitionTank Shooting

• Many related classes differ only in their behavior

• We need different variant of algorithm

• We need run-time shooting variationStrategy

!

Page 14: Tank Battle - A simple game powered by JMonkey engine

Pattern SolutionStrategy

Page 15: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionWorld of Complexity!

Geometrypolygon mesh

material

specifying its shape

colortextureopacity/transparency

Page 16: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionWorld of Complexity!

Spatial

Purpose: Abstract data structure that stores user data and transformations(= translation, rotation, scale) of elements of the 3D

Geometry Node

Visibility: visible 3D object invisible "handle" for a group of Spatials

Purpose: represent an object's lookstructure and group Geometries and other Nodes

Examples: Box, sphere, player, building, terrain, vehicle, missiles …

rootNode, audioNode, vehicleNode or shipNode with passengers attached, etc.

Page 17: Tank Battle - A simple game powered by JMonkey engine

Pattern DefinitionWorld of Complexity!

• Compose objects into tree structures to represent part-whole hierarchies

• Clients treat individual objects and compositions of objects uniformly

• We want clients to be able to ignore the difference between compositions of objects and individual objects

Composite

Page 18: Tank Battle - A simple game powered by JMonkey engine

Pattern SolutionWorld of Complexity!

Page 19: Tank Battle - A simple game powered by JMonkey engine

Problem DefinitionFamily of Game Entities

Page 20: Tank Battle - A simple game powered by JMonkey engine

Problem Definitiongame configuration Resolution

Brightness

Full screen

Master volumeVoice Volume

Menu music in-game volume

..move buttons

Action buttons

..

Bunch of configurations !!

:\

Page 21: Tank Battle - A simple game powered by JMonkey engine

Problem Definitiongame configuration

public static class UserBuilder { private final String firstName; private final String lastName; .. public UserBuilder(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public UserBuilder age(int age) { this.age = age; return this; } .. public User build() { return new User(this); } }

}

public class User { private final String firstName; // required private final String lastName; // required private final int age; // optional private final String phone; // optional private final String address; // optional private User(UserBuilder builder) { this.firstName = builder.firstName; this.lastName = builder.lastName; .. } public String getFirstName() { return firstName; } public String getLastName() { return lastName; }

public user getUser(){return new

User.UserBuilder(‘John’,’Doe’)

.age(30)

.phone(‘3235345)

.address(‘’Fake address 33)

.build();

Page 22: Tank Battle - A simple game powered by JMonkey engine

Problem Definitiongame configuration

 public class Configuration {

private int displayResolution;// display resolution

private int displayMode;// full screen? private int soundVolume;//Sound volumeprivate int musicVolume; //Music volume//....

private Configuration(ConfigBuilder builder) { this.displayResolution = builder.displayResolution; this.displayMode = builder.displayMode;

.. }

public int getDisplayResolution() {return displayResolution;

}public int getDisplayMode() {

return displayMode;}..public static class ConfigBuilder{

private int displayResolution=0;// display resolution

private int displayMode=0;// full screen?

private int soundVolume=0;//Sound volume

private int musicVolume=0; //Music volume

//.... 

public ConfigBuilder displayResolution(int dr)

{this.displayResolution =

dr;return this;

}public ConfigBuilder

displayMode(int dm){

this.displayMode = dm;return this;

}..public Configuration build(){

return new Configuration(this);

}}

}

Page 23: Tank Battle - A simple game powered by JMonkey engine

Problem Definitiongame configuration

But it is not all of it ..

Page 24: Tank Battle - A simple game powered by JMonkey engine

Problem Definitiongame configuration

Builderton pattern !? :o

Page 25: Tank Battle - A simple game powered by JMonkey engine

Problem Definitiongame configuration

public class Configuration {private static Configuration c;private int displayResolution; // display resolutionprivate int displayMode; // full screen? private int soundVolume; //Sound volumeprivate int musicVolume; //Music volume//....

private Configuration(ConfigBuilder builder) { this.displayResolution = builder.displayResolution; this.displayMode = builder.displayMode; this.soundVolume = builder.soundVolume; this.musicVolume = builder.soundVolume; }

public int getDisplayResolution() {return displayResolution;

}public int getDisplayMode() {

return displayMode;}…public static class ConfigBuilder{

private int displayResolution=0;private int displayMode=0;

private int soundVolume=0;private int musicVolume=0; //....public ConfigBuilder displayResolution(int dr){

this.displayResolution = dr;return this;

}public ConfigBuilder displayMode(int dm){

this.displayMode = dm;return this;

}…public Configuration build(){

if (c == null){ c=new

Configuration(this);return c;

}else return c;

}}

}

Page 26: Tank Battle - A simple game powered by JMonkey engine

To Be Continue …