A simple two player java-based game powered by JMonkey game engine which implements many standard object oriented design patterns such as Singleton, Composite, Strategy and etc.
4. Problem Definition
Tank 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.
}
}
5. Problem Definition
Tank 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");
6. Problem Definition
Tank Movement/Desktop Devices
onAction
handler
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);
}
9. 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!
13. Pattern Definition
Tank Shooting
• Many related classes differ only in their behavior
• We need different variant of algorithm
• We need run-time shooting variation
15. Problem Definition
World of Complexity!
Geometry
polygon mesh
material
specifying its shape
color
texture
opacity/transparency
16. Problem Definition
World 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 look
structure and group Geometries
and other Nodes
Examples:
Box, sphere, player, building, terrain,
vehicle, missiles …
rootNode, audioNode, vehicleNode
or shipNode with passengers
attached, etc.
17. Pattern Definition
World 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
21. Problem Definition
game 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();
22. Problem Definition
game configuration
public class Configuration {
private int displayResolution;// display resolution
private int displayMode;// full screen?
private int soundVolume;//Sound volume
private 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);
}
}
}