android and ios achievements.
This commit is contained in:
159
ios/src/java/advent/core/GameCenter.java
Normal file
159
ios/src/java/advent/core/GameCenter.java
Normal file
@@ -0,0 +1,159 @@
|
||||
|
||||
package advent.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.robovm.apple.foundation.NSArray;
|
||||
import org.robovm.apple.foundation.NSError;
|
||||
import org.robovm.apple.foundation.NSErrorUserInfo;
|
||||
import org.robovm.apple.foundation.NSString;
|
||||
import org.robovm.apple.gamekit.GKAchievement;
|
||||
import org.robovm.apple.gamekit.GKAchievementViewController;
|
||||
import org.robovm.apple.gamekit.GKAchievementViewControllerDelegateAdapter;
|
||||
import org.robovm.apple.gamekit.GKGameCenterControllerDelegateAdapter;
|
||||
import org.robovm.apple.gamekit.GKGameCenterViewController;
|
||||
import org.robovm.apple.gamekit.GKGameCenterViewControllerState;
|
||||
import org.robovm.apple.gamekit.GKLeaderboard;
|
||||
import org.robovm.apple.gamekit.GKLeaderboardTimeScope;
|
||||
import org.robovm.apple.gamekit.GKLeaderboardViewController;
|
||||
import org.robovm.apple.gamekit.GKLeaderboardViewControllerDelegateAdapter;
|
||||
import org.robovm.apple.gamekit.GKLocalPlayer;
|
||||
import org.robovm.apple.gamekit.GKScore;
|
||||
import org.robovm.apple.uikit.UIDevice;
|
||||
import org.robovm.apple.uikit.UIViewController;
|
||||
import org.robovm.apple.uikit.UIWindow;
|
||||
import org.robovm.objc.block.VoidBlock1;
|
||||
import org.robovm.objc.block.VoidBlock2;
|
||||
import org.robovm.objc.block.VoidBlock3;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class GameCenter {
|
||||
|
||||
// private final UIWindow keyWindow;
|
||||
//private final GameCenterListener listener;
|
||||
|
||||
/** Constructor.
|
||||
* @param keyWindow KeyWindow can't be accessed from the Delegates sometimes, so we need to save a reference
|
||||
* @param listener */
|
||||
/*
|
||||
public GameCenter (UIWindow keyWindow, GameCenterListener listener) {
|
||||
this.keyWindow = keyWindow;
|
||||
this.listener = listener;
|
||||
}
|
||||
*/
|
||||
|
||||
/** Do the login logic. If the user has never loged, a dialog will be shown. */
|
||||
public void login () {
|
||||
GKLocalPlayer.getLocalPlayer().setAuthenticateHandler(new VoidBlock2<UIViewController, NSError>() {
|
||||
@Override
|
||||
public void invoke (UIViewController viewController, NSError error) {
|
||||
// If the device does not have an authenticated player, show the login dialog
|
||||
if (viewController != null) {
|
||||
System.out.println("Not logged in");
|
||||
|
||||
// keyWindow.getRootViewController().presentViewController(viewController, true, null);
|
||||
}
|
||||
// If the viewController is null and the player is authenticated, the login is completed
|
||||
else if (GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
|
||||
System.out.println("Logged in");
|
||||
// listener.playerLoginCompleted();
|
||||
}
|
||||
// If the viewController is null and the player is not authenticated the login has failed
|
||||
else {
|
||||
System.out.println("login error");
|
||||
// listener.playerLoginFailed(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Report an achievement completed (100 as percentComplete)
|
||||
*
|
||||
* @param identifier */
|
||||
public void reportAchievement (String identifier) {
|
||||
reportAchievement(identifier, 100);
|
||||
}
|
||||
|
||||
/** Report an achievement with a percentComplete
|
||||
*
|
||||
* @param identifier
|
||||
* @param percentComplete */
|
||||
public void reportAchievement (String identifier, double percentComplete) {
|
||||
// If player is not authenticated, do nothing
|
||||
if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
|
||||
System.out.println("not authenticated");
|
||||
//listener.achievementReportFailed(buildUnauthenticatedPlayerError());
|
||||
return;
|
||||
}
|
||||
|
||||
GKAchievement achievement = new GKAchievement(identifier);
|
||||
achievement.setPercentComplete(percentComplete);
|
||||
achievement.setShowsCompletionBanner(true);
|
||||
|
||||
// Create an array with the achievement
|
||||
NSArray<GKAchievement> achievements = new NSArray<GKAchievement>(achievement);
|
||||
|
||||
GKAchievement.reportAchievements(achievements, new VoidBlock1<NSError>() {
|
||||
@Override
|
||||
public void invoke (NSError error) {
|
||||
if (error != null) {
|
||||
System.out.println("ninsuccess achievement " + error.toString());
|
||||
// listener.achievementReportFailed(error);
|
||||
} else {
|
||||
System.out.println("success achievement");
|
||||
// listener.achievementReportCompleted();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
public void loadAchievements () {
|
||||
// If player is not authenticated, do nothing
|
||||
if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
|
||||
listener.achievementsLoadFailed(buildUnauthenticatedPlayerError());
|
||||
return;
|
||||
}
|
||||
|
||||
GKAchievement.loadAchievements(new VoidBlock2<NSArray<GKAchievement>, NSError>() {
|
||||
@Override
|
||||
public void invoke (NSArray<GKAchievement> array, NSError error) {
|
||||
if (error != null) {
|
||||
listener.achievementsLoadFailed(error);
|
||||
} else {
|
||||
ArrayList<GKAchievement> achievements = new ArrayList<GKAchievement>();
|
||||
for (GKAchievement achievement : array) {
|
||||
achievements.add(achievement);
|
||||
}
|
||||
listener.achievementsLoadCompleted(achievements);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
/** Reset the achievements progress for the local player. All the entries for the local player are removed from the server. */
|
||||
/*
|
||||
public void resetAchievements () {
|
||||
// If player is not authenticated, do nothing
|
||||
if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
|
||||
listener.achievementsResetFailed(buildUnauthenticatedPlayerError());
|
||||
return;
|
||||
}
|
||||
|
||||
GKAchievement.resetAchievements(new VoidBlock1<NSError>() {
|
||||
@Override
|
||||
public void invoke (NSError error) {
|
||||
if (error != null) {
|
||||
listener.achievementsResetFailed(error);
|
||||
} else {
|
||||
listener.achievementsResetCompleted();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
@@ -31,6 +31,7 @@ public class IOSLauncher extends IOSApplication.Delegate {
|
||||
|
||||
protected IOSApplication createApplication() {
|
||||
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
|
||||
new GameCenter().login();
|
||||
// config.colorFormat = GLKViewDrawableColorFormat.SRGBA8888;
|
||||
// config.depthFormat = GLKViewDrawableDepthFormat._24;
|
||||
config.orientationPortrait = false;
|
||||
|
||||
Reference in New Issue
Block a user