How to implement a game mechanic - matan1905/GameMechanicManager GitHub Wiki
In this guide we will learn how to implement the Fishing mechanic we made in the Previous page
If you haven't read it, don't worry, all you need to know is what the mechanic does and what are the events, notification and settings that this mechanics uses, which is listed after the title: "Fishing mechanic events and notifications"
So let's begin! first, we need to instantiate the game mechanic manager.
public class Game {
private final GameMechanicManager gmm;
public Game(){
this.gmm = new GameMechanicManager(null, new MechanicNotificationReceiver() {
@Override
public void receive(String s, Object... objects) {
//Receive and handle any notification here
}
});
}
//main game loop
public void Render(){
gmm.tick();
drawStuff();
}
private void drawStuff() {
//Drawing stuff will happen here
}
}
Ok! now, let's expose some methods to use when the player does some action
public void startFishing(){
gmm.fireEvent("start_fishing",null,null);//no callback or arguments, so just null these
}
public void cancelFishing(){
gmm.fireEvent("cancel_fishing",null,null);//no callback or arguments, so just null these
}
public void pollFishingRod(){
gmm.fireEvent("pull_rod", new EventReceiver.Callback() {
@Override
public void callback(Object... objects) {
//The results of the player action
//The first(and only) argument is the boolean that tells us if the player got a fish,
//So we cast it and check it
if((Boolean)objects[0]) getFish();
}
}, null);//no arguments, so it's null.
}
Now, let's get the notification for when a fish has bitten so we could show the player some movement! for that we will rewrite the Game constructor a little bit:
public Game(){
this.gmm = new GameMechanicManager(null, new MechanicNotificationReceiver() {
@Override
public void receive(String s, Object... objects) {
//Receive and handle any notification here
if(s.equals("fish_bite")){
showFishBiteIndication();
}
}
});
}
private void showFishBiteIndication() {
//... move the fishing rod or something
}
Now let's say you made a very hard to see indication and you want to give your player more time to pull the rod just add:
gmm.getSettings().changeSetting("fish_rod_window_time","15");//changes from 10 to 15
and there you have it!
Full code:
import com.opedea.GameMechanicManager;
import com.opedea.mechanics.EventReceiver;
import com.opedea.mechanics.MechanicNotificationReceiver;
public class Game {//sample game class, it won't actually work if you use it
private final GameMechanicManager gmm;
public Game(){
this.gmm = new GameMechanicManager(null, new MechanicNotificationReceiver() {
@Override
public void receive(String s, Object... objects) {
//Receive and handle any notification here
if(s.equals("fish_bite")){
showFishBiteIndication();
}
}
});
gmm.getSettings().changeSetting("fish_rod_window_time","15");
}
private void showFishBiteIndication() {
// :)
}
//main game loop
public void Render(){
gmm.tick();
drawStuff();
}
public void startFishing(){
gmm.fireEvent("start_fishing",null,null);//no callback or arguments, so just null these
}
public void cancelFishing(){
gmm.fireEvent("cancel_fishing",null,null);//no callback or arguments, so just null these
}
public void pollFishingRod(){
gmm.fireEvent("pull_rod", new EventReceiver.Callback() {
@Override
public void callback(Object... objects) {
//The results of the player action
//The first(and only) argument is the boolean that tells us if the player got a fish,
//So we cast it and check it
if((Boolean)objects[0]) getFish();
}
}, null);//no arguments, so it's null.
}
public void getFish(){}
private void drawStuff() {
}
}