1
Teknologi Game
Modul 5 A. Revisi Kelas Swordman a. Tambahkan method blocked pada kelas Swordman.java private boolean blocked(float x, float y) { return StateManagement.currentMap[(int) x][(int) y] != 0; }
Method blocked akan mengembalikan nilai true bila nilai dari tile pada attribute currentMap sesuai posisi x dan y bernilai tidak sama dengan 0, ini berarti bahwa tile tersebut sudah berisi suatu nilai atau variable. b. Pastikan bahwa method tryMove anda sama dengan kode program berikut private boolean tryMove(float x, float y) { float newx = playerX + x; float newy = playerY + y; if (blocked(newx, newy)) { if (blocked(newx, playerY)) { if (blocked(playerX, newy)) { // can't move at all! return false; } else { playerY = newy; this.y = (int) playerY; return true; } } else { playerX = newx; this.x = (int) playerX; return true; } } else { playerX = newx; playerY = newy; this.x = (int) playerX; this.y = (int) playerY; return true; } }
Method trymove ini akan melakukan pengecekan apakah tile yang akan kita lalui di block atau tidak, bila di blok maka nilai dari x dan y pada kelas Swordman tidak akan diubah dan sebaliknya 1
Teknologi Game | ratnokustiawan.wordpress.com
2
Teknologi Game
c. Samakan method update anda dengan kode program berikut public void update(Input input, long delta) { if (!canControl) { return; } //inisialisasi objek tempDirection dengan nilai null tempDirection = null; //cek inputan oleh user, bila menekan panah kiri if (input.isKeyDown(Input.KEY_LEFT)) { //ubah attribut pressed menjadi true karena user menekan tombol pressed = true; //ubah tempDirection dengan arah WEST tempDirection = Direction.WEST; //ubah akhir dengan aksi berjalan (WALKING) action = Action.WALKING; //panggil method move if (tryMove(-1.0f * delta * 0.003f, 0)) { //update karakter swordman update(delta); } } else if (input.isKeyDown(Input.KEY_RIGHT)) { pressed = true; tempDirection = Direction.EAST; action = Action.WALKING; if (tryMove(1.0f * delta * 0.003f, 0)) { update(delta); } } else if (input.isKeyDown(Input.KEY_DOWN)) { pressed = true; tempDirection = Direction.SOUTH; action = Action.WALKING; if (tryMove(0, 1.0f * delta * 0.003f)) { update(delta); } } else if (input.isKeyDown(Input.KEY_UP)) { pressed = true; tempDirection = Direction.NORTH; action = Action.WALKING; if (tryMove(0, -1.0f * delta * 0.003f)) { update(delta); } } else { //proses ini akan dijalankan apabila tidak ada tombol yang ditekan pressed = false; //jika aksi sebelumnya adalah jalan maka? if (action == Action.WALKING) { //ubah aksi menjadi NOTHING action = Action.NOTHING; //ubah tempDirection juga menjadi NOTHING tempDirection = Direction.NOTHING; } } Selanjutnya masih sama
Kode program yang ditebali adalah method update akan dipanggil ketika hasil dari method dari tryMove bernilai true. 2
Teknologi Game | ratnokustiawan.wordpress.com
3
Teknologi Game
B. Revisi kelas Mission1.java Pastikan urutan dari kode program pada method init sama dengan kode program berikut public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { rpgMap = new RPGMap("rpg/mission1.tmx"); StateManagement.currentMap = new int[rpgMap.getWidth()][rpgMap.getHeight()]; int treeLayer = rpgMap.getLayerIndex("Tree Layer"); int buildingLayer = rpgMap.getLayerIndex("Building Layer"); int value = 0; for (int x = 0; x < rpgMap.getWidth(); x++) { for (int y = 0; y < rpgMap.getHeight(); y++) { if (y > 0) { if (StateManagement.currentMap[x][y - 1] == 0) { StateManagement.currentMap[x][y - 1] = rpgMap.getTileId(x, y, treeLayer); } value = rpgMap.getTileId(x, y, buildingLayer); if (value > 0) { StateManagement.currentMap[x][y] = value; } } } } swordman = new Swordman(); swordman.x = 5; swordman.y = 4; swordman.playerX = 5; swordman.playerY = 4; rpgMap.addSprite(swordman); troll1 = new GreyTroll(); troll1.x = 10; troll1.y = 6; troll1.playerX = 10; troll1.playerY = 6; rpgMap.addSprite(troll1); }
3
Teknologi Game | ratnokustiawan.wordpress.com
4
Teknologi Game
C. Revisi method addSprite pada kelas RPGMap.java public boolean addSprite(Swordman e) { if (e.getClass().equals(Swordman.class)) { this.swordman = e; } StateManagement.currentMap[e.x][e.y] = e.getID(); return sprites.add(e); }
Kode program yang ditebali gunanya untuk mendaftarkan posisi tile di peta D. Kelas Battle.java Tambahkan 1 buah kelas dengan nama Battle.java di paket rpg a. Import import org.newdawn.slick.BigImage; import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; import org.newdawn.slick.state.transition.FadeInTransition; import org.newdawn.slick.state.transition.FadeOutTransition;
b. Tambahkan 1 buah kelas dengan nama Battle.java, kelas ini nantinya akan kita gunakan untuk berpindah dari screen peta ke screen battle. Jadikan kelas ini turunan dari BasicGameState public class Battle extends BasicGameState { }
c. Deklarasikan field dibawah ini private BigImage background; public static int STATE_ID = 2; public static int PARENT = 1; private static Swordman playerUnits, enemyUnits; private static int playerX, playerY, enemyX, enemyY; private static int screenWidth = 0;
i. Field background digunakan untuk menampilkan gambar background yang sudah kita dapatkan dari hasil printscreen ii. STATE_ID Digunakan sebagai penanda atau ID dari state ini, bermanfaat ketika perpindahan antar screen iii. PARENT Digunakan untuk menandai screen sebelum screen ini muncul, bermanfaat untuk kembali ke screen sebelumnya Teknologi Game | ratnokustiawan.wordpress.com
4
5
Teknologi Game
iv. playerUnits Referensi objek untuk menyimpan sprite swordman v. enemyUnits Referensi objek untuk menyimpan sprite dari musuh vi. playerX,player,enemyX,enemy Digunakan untuk menentukan posisi dimana sprite swordman dan grey stroll akan dirender vii. screenWidth Digunakan untuk menyimppan ukuran layar d. Tambahkan 2 buah method static berikut public static void setEnemyUnits(Swordman enemyUnits) { Battle.enemyUnits = enemyUnits; } public static void setPlayerUnits(Swordman playerUnits) { Battle.playerUnits = playerUnits; }
i. Method setEnemyUnits digunakan untuk melakukan pengesetan field enemyUnits ii. Method setPlayerUnits digunakan untuk melakukan pengesetan field playerUnits. e. Override method getID @Override public int getID() { return STATE_ID; }
Digunakan untuk memberikan nilai kembalian dari method getID, dimana nilai kembaliannya diambil dari STATE_ID f. Method Init public void init(GameContainer container, StateBasedGame game) throws SlickException { background = new BigImage("rpg/image/battle/battle1.png"); screenWidth = container.getWidth(); }
Digunakan untuk melakukan load image battle1.png dan membaca ukuran screen kemudian ukuran screen disimpan di screenWidth
5
Teknologi Game | ratnokustiawan.wordpress.com
6
Teknologi Game
g. Method initBattle public static void initBattle() { if (playerUnits != null) { playerX = 50; playerY = 260; } if (enemyUnits != null) { System.out.println(screenWidth); enemyX = screenWidth - 150; enemyY = 260; } }
Mengatur konfigurasi awal posisi dari tiap sprite atau karakter berdasarkan ukuran layar h. Method render public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { background.draw(0, 0); if (playerUnits != null) { playerUnits.draw(playerX, playerY); } if (enemyUnits != null) { enemyUnits.draw(enemyX, enemyY); } }
i.
Digunakan untuk melakukan drawing terhadap playerUnits dan enemyUnits apabila kedua field tersebut tidak bernilai null, posisi berdasarkan nilai X dan Y yang sudah kita dapatkan dari method initBattle Method update public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException { if (container.getInput().isKeyPressed(Input.KEY_ESCAPE)) { playerUnits.x-=1; playerUnits.playerX-=1; enemyUnits = null; playerUnits = null; game.enterState(PARENT, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } }
Membaca inputan keyboard dari user, kemudian memindahkan posisi pemain 1 langkah mundur, kosongkan variable enemyUnits dan playerUnits lalu kembalikan screen ke luar dari battle. Teknologi Game | ratnokustiawan.wordpress.com
6
7
Teknologi Game
E. EventAction.java Tambahkan 1 buah interface dengan nama EventAction dipaket rpg public interface EventAction { public abstract void doAction(Object sender); }
Kelas ini berisi 1 buah abstract method bernama doAction untuk menangkap suatu event yang nantinya akan kita oleh perintahnya sesuai event yang terjadi. F. BlinkMessage.java Kelas blinkmessage di paket rpg digunakan untuk menampilkan pesan yang kedip2. . a. Tambahkan 1 buah kelas dengan nama BlinkMessage dipaket rpg public class BlinkMessage { }
b. Import yang anda butuhkan import java.awt.Point; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame;
c. Contruktor yang harus dibuat public BlinkMessage(String key, String message) { this.message = message; this.key = key; location = new Point(10, 30); } public BlinkMessage(String key, String message, Point location) { this.message = message; this.location = location; this.key = key; } public BlinkMessage(String key, String message, EventAction action) { this(key, message); this.action = action; } public BlinkMessage(String key, String message, Point location, EventAction action) { this(key, message, location); this.action = action; }
Keempat constructor diatas dibuat dengan tujuan agar kelas BlinkMessage dapat digunakan sesuai dengan kebutuhan
Teknologi Game | ratnokustiawan.wordpress.com
7
8
Teknologi Game
d. Tambahkan settet dan getter untuk attribut public void setShow(boolean show) { this.show = show; } public boolean isShow() { return show; } public int getShowMessage() { return showMessage; } public Point getLocation() { return location; } public void setLocation(Point location) { this.location = location; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }
e. Method render public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { if (visible && show) { g.drawString(message, location.x, location.y); } }
Pesan dari kelas blinkMessage akan ditampilkan bila nilai dari visible adalah true dan show adalah true
8
Teknologi Game | ratnokustiawan.wordpress.com
9
Teknologi Game
f. Methd update public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { sumUpdate += i; if (sumUpdate >= showMessage) { sumUpdate = 0; visible = !visible; } if (show && action != null) { action.doAction(key); } }
Pergantian antara proses muncul dan menghilangnya message akan dipengaruhi oleh nilai i yang dijumlahkan ke attribute sumUpdate Pemanggilan method doAction juga akan dilaksanakan di method ini g. Metod hideMessage public void hideMessage() { setShow(false); }
Digunakan untuk menghilangkan atau menyembunyikan message G. Kelas BlinkMessageCollection a. Buat kelas blink message collection yang merupakan turunan dari HashMap di paket rpg public class BlinkMessageCollection extends HashMap<String, BlinkMessage> { }
b. Import yang dibutuhkan import java.util.HashMap; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame;
c. Deklaraasikan attribute berikut private EventAction action;
Digunakan untuk menyimpan objek EventAction
9
Teknologi Game | ratnokustiawan.wordpress.com
10
Teknologi Game
d. Constructor public BlinkMessageCollection() { } public BlinkMessageCollection(EventAction action) { this.action = action; }
Tambahkan 2 buah constructor diatas, konstruktor default dibutuhkan bila action memang tidak akan diisi e. Overload method put dengan 2 buah method put yang baru public BlinkMessage put(String key, String value) { if (action != null) { return super.put(key, new BlinkMessage(key, value, action)); } return super.put(key, new BlinkMessage(key, value)); } public BlinkMessage put(String key, String value, EventAction action) { return super.put(key, new BlinkMessage(key, value, action)); }
Method put pertama digunakan bila action belum didefinisikan atau sudah didefinisikan melalui konstruktor, method put kedua bila action didefinisikan ketika method put ini dipanggil. f. Method render public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { for (BlinkMessage msg : values()) { msg.render(gc, sbg, g); } }
Method ini digunakan untuk merender semua blinkMessage yang terdaftar di kelas BlinkMessageCollection g. Method update public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { for (BlinkMessage msg : values()) { msg.update(gc, sbg, i); } }
Method ini digunakan untuk mengupdate semua blinkMessage yang terdaftar di kelas BlinkMessageCollection
Teknologi Game | ratnokustiawan.wordpress.com
10
11
Teknologi Game
h. Method hideAllMessage public void hideAllMessage() { for (BlinkMessage msg : values()) { msg.hideMessage(); } }
Digunakan untuk menghiden semua message H. Interface Rule.java Interface ini digunakan untuk melakukan standarisasi Rule di game package rpg; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; public interface Rule { public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException; public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException; }
I. Kelas MainMenu Digunakan untuk membuat main menu di kanan atas, buat di paket rpg.ui 1. Kelas Main Menu public class MainMenu implements EventAction { }
Kelas ini mengimplementasikan EventAction 2. Import import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; import rpg.EventAction;
11
Teknologi Game | ratnokustiawan.wordpress.com
12
Teknologi Game
3. Attribut private Image imgMenuUp, imgMenuCenter, imgMenuDown; private Image imgMenuCut; private int x, y; private boolean show = false; private String[] menuItem = {"Unit Info", "Message", "Inventory", "Save", "Load", "Exit"}; private MenuItem[] menuItems;
a. b. c. d. e.
Attribut dengan type image untuk menyimpan image yang hendak di load X dan y adalah posisi dari menu ini Show menandakan apakah item statusnya muncul atau tidak menuItem adalah daftar text yang hendak ditampilkan di menu item menuItems adalah daftar menu item yang nantinya akan dibuat berdasarkan menuItem 4. Constructor public MainMenu(GameContainer gc, int x, int y) throws SlickException { this.x = x; this.y = y; imgMenuCut = new Image("rpg/ui/image/kertas_up_cut.png"); imgMenuUp = new Image("rpg/ui/image/kertas_up.png"); imgMenuCenter = new Image("rpg/ui/image/kertas_center.png"); imgMenuDown = new Image("rpg/ui/image/kertas_down.png"); menuItems = new MenuItem[menuItem.length]; int top = y + imgMenuUp.getHeight(); for (int i = 0; i < menuItem.length; i++) { menuItems[i] = new MenuItem(menuItem[i], x + 20, top, imgMenuUp.getWidth(), 25); menuItems[i].setAction(this); top += 30; } }
Digunakan untuk load image dan inisialisasi objek menuItems berdasarkan judul menu item yang sudah ditentukan diawal
12
Teknologi Game | ratnokustiawan.wordpress.com
13
Teknologi Game
5. Render public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { if (show) { imgMenuUp.draw(x, y); imgMenuCenter.draw(x + 5, y + imgMenuUp.getHeight()); imgMenuDown.draw(x + 5, y + imgMenuUp.getHeight() + imgMenuCenter.getHeight()); for (MenuItem item : menuItems) { item.render(gc, sbg, g); } } else { imgMenuCut.draw(x, y); } g.drawString("Menu (F1)", x + 20, y); }
6. ImageClick protected boolean imageClick(int mouseX, int mouseY) { if (mouseX >= this.x && mouseX <= (this.x + imgMenuUp.getWidth())) { if (mouseY >= this.y && mouseY <= (this.y + imgMenuUp.getHeight())) { return true; } } return false; }
Digunakan untuk mengetahui apakah suatu image sedang di klik 7. Method yang berhubungan dengan Click public void click() { show = !show; } public void mouseClick(int x, int y) { if (imageClick(x, y)) { click(); } } public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { if (show) { for (MenuItem item : menuItems) { item.update(gc, sbg, i); } } }
Teknologi Game | ratnokustiawan.wordpress.com
13
14
Teknologi Game
Proses menangkan event click, mengupdate tampilan ketika sudah di klik dan mendeteksi imageClick 8. DoAction public void doAction(Object sender) { if (sender.toString().trim().equals("Exit")) { System.exit(0); } }
Digunakan untuk menangkap event yang terjadi J. Kelas MenuItem a. Kelas ini digunakan untuk menampilkan menu item, buat di paket rpg.ui public class MenuItem { }
b. Import yang anda butuhkan import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; import rpg.EventAction;
c. Constructor public MenuItem(String text, int x, int y, int width, int height) { this.text = text; this.x = x; this.y = y; this.width = width; this.height = height; }
Konstructor pada kelas menu item meminta isian text, x, y, width,dan height sebagai suatu nilai yang harus sudah ditentukan diawal d. Setter dan getter public void setAction(EventAction action) { this.action = action; } public String getText() { return text; } 14
Teknologi Game | ratnokustiawan.wordpress.com
15
Teknologi Game
e. Method update public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { Input input = gc.getInput(); if (mouseOver(input.getMouseX(), input.getMouseY()) && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && action != null) { action.doAction(text); } }
Digunakan untuk melakukan pengecekan dan menjalankan aksi berdasarkan kondisi yang sudah ditentukan f. Render public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { g.drawString(text, x, y); if (over) { g.drawRect(x - 3, y, width - 40, height); } }
Digunakan untuk menggambar kotak bila mouse berada diatas menu g. MouseOver protected boolean mouseOver(int mouseX, int mouseY) { if (mouseX >= this.x && mouseX <= ((this.x + width))) { if (mouseY >= this.y && mouseY <= ((this.y + height))) { over = true; return over; } } over = false; return over; }
Digunakan untuk melakukan pengecekan apakah mouse berada diatas suatu menu atau tidak
15
Teknologi Game | ratnokustiawan.wordpress.com
16
Teknologi Game
K. Kelas Dialog (paket rpg.ui) Kelas ini digunakan untuk memunculkan dialog yang nantinya dibutuhkan di game a. Kelas Dialog public class Dialog { }
b. Import yang dibutuhkan import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame;
c. Attribut yang harus dideklarasikan private Image image; private Image buttonNormal, buttonfocus; private boolean visible, mouseOver = false; private String title, buttonText = "Close"; protected int x, y; protected int buttonX, buttonY;
i. Attribute dengan type Image digunakan untuk menyimpan image ii. Visible dan mouseOver digunakan untuk menyimpan status iii. Title dan buttonText digunakan untuk menampilkan text iv. X,y, buttonX, buttonY digunakan untuk meyimpan koordinat d. Constructor public Dialog(String title, GameContainer gc) throws SlickException { image = new Image("rpg/ui/image/dialog.png"); buttonNormal = new Image("rpg/ui/image/dialog_button.png"); buttonfocus = new Image("rpg/ui/image/dialog_button_focus.png"); this.title = title; x = (gc.getWidth() - image.getWidth()) / 2; y = (gc.getHeight() - image.getHeight()) / 2; buttonX = x + image.getWidth() - buttonNormal.getWidth() - 40; buttonY = y + image.getHeight() - 50; }
Digunakan untuk inisialisasi objek dan mengukur posisi yang dibutuhkan untuk melektakkan image
16
Teknologi Game | ratnokustiawan.wordpress.com
17
Teknologi Game
e. Setter dan getter public boolean isVisible() { return visible; } public void setVisible(boolean visible) { this.visible = visible; }
f. Method render public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { if (visible) { image.draw(x, y); g.drawString(title, x + 30, y + 30); if (mouseOver) { buttonfocus.draw(buttonX, buttonY); } else { buttonNormal.draw(buttonX, buttonY); } g.drawString(buttonText, buttonX + 5, buttonY + 5); } }
Digunakan untuk menulis judul dari dialog, tombol tutup dan mengganti ganti tombol yang hendak ditampilkan g. imageClick protected boolean imageClick(int mouseX, int mouseY) { if (mouseX >= this.buttonX && mouseX <= (this.buttonX + buttonNormal.getWidth())) { if (mouseY >= this.buttonY && mouseY <= (this.buttonY + buttonNormal.getHeight())) { return true; } } return false; }
Digunakan untuk mendeteksi apakah mouse berada diatas image atau tidak
17
Teknologi Game | ratnokustiawan.wordpress.com
18
Teknologi Game
h. Update public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { if (visible) { mouseOver = imageClick(gc.getInput().getMouseX(), gc.getInput().getMouseY()); if(mouseOver){ if(gc.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)){ setVisible(false); } } } }
Digunakan untuk mengupdate status mouse dan mendeteksi apakah mouse ditekan atau tidak L. Kelas DialogCamp1 Buat di paket rpg.ui import org.newdawn.slick.GameContainer; import org.newdawn.slick.SlickException; public class DialogCamp1 extends Dialog { public DialogCamp1(GameContainer gc) throws SlickException { super("Welcome to Camp1",gc); } }
Digunakan untuk membuat window camp1 M. Kelas Mission1Rule.java Digunakan untuk menyimpan aturan atau rule yang berjalan di dalam game a. Kelas public class Mission1Rule implements Rule, EventAction { }
Kelas ini mengimplementasikan interface Rule dan EventAction b. Import yang dibutuhkan import java.awt.Point; import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; import org.newdawn.slick.state.transition.FadeInTransition; import org.newdawn.slick.state.transition.FadeOutTransition; import rpg.ui.DialogCamp1; 18
Teknologi Game | ratnokustiawan.wordpress.com
19
Teknologi Game
c. Attribut yang harud didefinisikan private Swordman swordman; private BlinkMessageCollection messageCollection; private boolean spacePress = false; private GreyTroll greyTroll1; private DialogCamp1 dialogCamp1;
1. Swordman Digunakan untuk menyimpan objek swordman 2. messageCollection Digunakan untuk menyimpan objek message 3. spacePress Digunakan untuk mengidentifikasikan bahwa spasi ditekan atau tidak 4. greyTroll1 Digunakan untuk menyimpan objek greyTroll1 5. dialogCamp1 Digunakan untuk menyimpan dialog d. Construktor yang dibutuhkan public Mission1Rule(Swordman swordman) throws SlickException { this.swordman = swordman; messageCollection = new BlinkMessageCollection(this); messageCollection.put("msg1", "Press SPACE for entering camp"); messageCollection.put("msg2", "Press SPACE for entering Camp to find resource"); }
Digunakan untuk melakukan inisialisasi objek swordman dan messageCollection serta meregister pesan yang hendak dimunculkan e. Method init public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { dialogCamp1 = new DialogCamp1(gc); }
Digunakan untuk inisialisasi objek dialogCamp1 f. Setter public void setGreyTroll1(GreyTroll greyTroll1) { this.greyTroll1 = greyTroll1; } public void setSpacePress(boolean spacePress) { this.spacePress = spacePress; }
Digunakan sebagai setter objek greyTroll1 dan spacePress 19
Teknologi Game | ratnokustiawan.wordpress.com
20
Teknologi Game
g. Render public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { messageCollection.render(gc, sbg, g); dialogCamp1.render(gc, sbg, g); }
Digunakan untuk merender messageCollection dan dialogCamp1 h. Update public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException { messageCollection.update(gc, sbg, i); dialogCamp1.update(gc, sbg, i); if (swordman.x == 7 && swordman.y == 7) { messageCollection.get("msg1").setShow(true); } else if (swordman.x == 24 && swordman.y == 12) { messageCollection.get("msg2").setShow(true); } else { messageCollection.hideAllMessage(); } if (greyTroll1 != null) { double distance = Point.distance(swordman.x, swordman.y, greyTroll1.x, greyTroll1.y); if (distance < 2) { Battle.PARENT = Mission1.STATE_ID; Battle.setEnemyUnits(greyTroll1); Battle.setPlayerUnits(swordman); Battle.initBattle(); sbg.enterState(Battle.STATE_ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } } }
Digunakan untuk memunculkan pesan, menghilangkan pesan dan melakukan transisi ke battle mode i. doAction public void doAction(Object sender) { if (spacePress) { if (sender.toString().trim().equals("msg1")) { if (!dialogCamp1.isVisible()) { dialogCamp1.setVisible(true); } } } }
Memunculkan dialog diCamp1
Teknologi Game | ratnokustiawan.wordpress.com
20
21
Teknologi Game
N. Kelas Mission1 a. Tambahkan 2 buah attribute berikut private Mission1Rule rule; private MainMenu mainMenu;
b. Tambahakn kode program berikut pada method init rule = new Mission1Rule(swordman); rule.init(gc, sbg); rule.setGreyTroll1(troll1); mainMenu = new MainMenu(gc, gc.getWidth() - 200, 5);
c. Tambahkan kode program berikut pada method render rule.render(gc, sbg, grphcs); mainMenu.render(gc, sbg, grphcs);
d. Tambahkan kode program berikut pada method update rule.update(gc, sbg, i); mainMenu.update(gc, sbg, i);
e. Override method @Override public void keyPressed(int key, char c) { super.keyPressed(key, c); rule.setSpacePress((key == Input.KEY_SPACE)); } @Override public void keyReleased(int key, char c) { super.keyReleased(key, c); if (key == Input.KEY_F1) { mainMenu.click(); } if (key == Input.KEY_SPACE) { rule.setSpacePress(false); } } @Override public void mouseReleased(int button, int x, int y) { super.mouseReleased(button, x, y); if (button == Input.MOUSE_LEFT_BUTTON) { mainMenu.mouseClick(x, y); } }
Digunakan untuk menangkap input
Teknologi Game | ratnokustiawan.wordpress.com
21
22
Teknologi Game
O. Kelas StateManagement @Override public void initStatesList(GameContainer gc) throws SlickException { addState(new Mission1()); addState(new Battle()); }
Menambahkan screen Battle di game
22
Teknologi Game | ratnokustiawan.wordpress.com