Java - Dungeon Game example

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 8 μήνες)

346 εμφανίσεις

Dr Matthew Collinson, Computing Science, University of Aberdeen

1

CS3523 Pacticals

Guide


Dr Matthew Collinson, Computing Science, University of Aberdeen

2

Tools you need


Java


IDE (preferably NetBeans)


XML (a tiny bit)


We may use some other software too.

Dr Matthew Collinson, Computing Science, University of Aberdeen

3

Java


Knowledge of the Java programming
language is essential for this course.

»
If you don’t have such knowledge then you
had better be prepared to acquire it rapidly,
on your own.

»
Tutorials

http://docs.oracle.com/javase/tutorial/

»
Documentation

http://docs.oracle.com/javase/6/docs/

Dr Matthew Collinson, Computing Science, University of Aberdeen

4

IDE


Use of an IDE like NetBeans or Eclipse
is strongly recommended for
manipulating the many files used for
particular programs.


I favour NetBeans

»
http://netbeans.org/


Dr Matthew Collinson, Computing Science, University of Aberdeen

5

The Dungeon


Practicals use a “dungeon” game of a
typical kind to help
you learn how to
do


stuff.

»
Game is played on a PC

»
One human player

»
NPCs are mostly hostile monsters

»
Player explores dungeon, finds treasure,
fights monsters.

Dr Matthew Collinson, Computing Science, University of Aberdeen

6

CS3523 Game








Dr Matthew Collinson, Computing Science, University of Aberdeen

7

The Key


Hero is the green circle.


Other creatures are the red circles.


Strength/health potions in blue and green bottles


Gold (in various quantities), keys (associated with
particular doors).


Tiles: rooms, doors, flame traps, pits, non
-
game
space (black), Finish (green).


Doors: open (white), closed (grey)


[Run demo.]


Dr Matthew Collinson, Computing Science, University of Aberdeen

8

The Dungeon World


World model

»
Tiles
: Floor, Door, Flametrap, Pit, Finish


dungeon.model.structure

»
Treasure
: Gold, Potion, Key


dungeon.model.items.treasure

»
Mobs
: Orc, Ogre, Hero


dungeon.model.items.mobs


Dr Matthew Collinson, Computing Science, University of Aberdeen

9

Dungeon


The dungeon program can get info
about tiles, treasure, mob via
methods

of the
game

object
.


Can load/save game to XML file from
GUI (or from API)

Dr Matthew Collinson, Computing Science, University of Aberdeen

10

Dungeon XML file

<Game Name="
simple
">

-

<Map> <Tiles> <Vector>



<Floor Height="
50
" Width="
50
" X="
0.0
" Y="
0.0
" />



<Floor Height="
10
" Width="
10
" X="
-
20
" Y="
0.0
" />



</Vector> </Tiles>


</Map>

-

<Creatures> <Vector>

-

<Orc CurrentEnergy="
10
" CurrentHealth="
10
" Gold="
20
" X="
25
" Y="
25
"
Behaviour="
dungeon.ai.DefaultBehaviour
">

-

<Attacks>
-

<Vector>


<Smash /> </Vector> </Attacks>

-

<Inventory> <Vector />


</Inventory>


</Orc>


</Vector> </Creatures>

-

<Treasure>
-

<Vector>



<Gold Worth="
10
" X="
23
" Y="
17
" />



<Gold Worth="
20
" X="
5
" Y="
37
" />



<Gold Worth="
30
" X="
42
" Y="
11
" />



<Gold Worth="
40
" X="
45
" Y="
29
" />



<Potion Type="
0
" X="
3
" Y="
5
" />



</Vector></Treasure> </Game>

Dr Matthew Collinson, Computing Science, University of Aberdeen

11

Creatures (NPC)


Have parameters

»
Location, Size, Strength, Health, …

»
Goal: location NPC is moving towards

»
Behaviour
: class which defines how NPC
acts (ie, the AI behind NPC)

Dr Matthew Collinson, Computing Science, University of Aberdeen

12

Behaviour class


Implements
Behaviour

interface


Specifies there must be
onTick

method

»
Called every tick of game clock

»
Updates NPC’s position, status, goals, etc
according to AI logic


DefaultBehaviour

class

Dr Matthew Collinson, Computing Science, University of Aberdeen

13

3 steps to Defining new behaviour


Create new class that
implements

Behaviour

»
Can copy
-
and
-
edit DefaultBehaviour

»
Specify behaviour


Example: delete goal stuff in DefaultBehaviour


Change XML file to specify new
behaviour used by NPC.


Start dungeon, load XML file, run

Dr Matthew Collinson, Computing Science, University of Aberdeen

14

API


You
must

familiarise yourself with this
as soon as possible.


[Walk through API].


Go and read the Dungeon XML Primer
(on the course Information page).


Play with the game (on the Info page).


Come to the practical on Thursday.

Dr Matthew Collinson, Computing Science, University of Aberdeen

15

How it works 1: Main


Application enters at
main

in
App
class in
dungeon.App.


Initial random dungeon
fGame
built by
DungeonBuilder
.
createDungeon.



Window created as object
fMainForm
of class

DungeonForm
.


Event
-
handling for user interaction and game
state updates:
fTimerTick
and
fKeyDispatcher.

Dr Matthew Collinson, Computing Science, University of Aberdeen

16

How it works 2: Game State


Game state (world) held in an object
fgame

of
Game
class.


New initial game state can be loaded
from XML from here (implements
Persistent

class).


XML file gives the data for the initial
game state.

Dr Matthew Collinson, Computing Science, University of Aberdeen

17

How it works 3: Updating



fGame
has a method
tick()
that advances the game
by one round. Each round has the following stages:

»
Each
map

tile is informed about the new round.

»
Each
treasure

item informed about new round.

»
For each
creature

in turn, and then for the
hero
:


The tile they stand upon is informed of this fact.


The creature is prompted to act.

»
Dead creatures are removed from the game.


Dr Matthew Collinson, Computing Science, University of Aberdeen

18

How it works 4


Action listener
fTimerTick

hears ticks from
fGame
.tick
and updates game via
fMainForm
.updateGame().


fMainForm
.updateGame() refreshes the User
Interface (UI) after the game tick.


fKeyDispatcher
in
App

intercepts presses by
user on arrow
-
keys, so that hero can be
moved. This changes game state via
fGame
.getHero().getMovement() .





Dr Matthew Collinson, Computing Science, University of Aberdeen

19

How it works 5: Mobs & Behaviour


A
mob

is a mobile item (orc, creature,
hero) which implements the class
Mob.


Each mob has various fields which set
its properties (strength, energy, treasure
list, goals, …, and
fBehaviour
).


The class
Behaviour
and its various
implementations are used to define the
(intelligent) behaviours of mobs.

Dr Matthew Collinson, Computing Science, University of Aberdeen

20

The brain, not the engine


We are focussed on the AI aspects
here.


You won’t have to mess
-
around with
most of the game engine.


You will mostly need to modify
Behaviour classes, and a few others.