Tải bản đầy đủ - 0 (trang)
Chapter 13.  An Isometric Tile Game

# Chapter 13.  An Isometric Tile Game

Tải bản đầy đủ - 0trang

IsometricTiles

Isometrictilesarethebasisofmanyreal-timestrategy(RTS)

games,wargames,andsimulations(e.g.,CivilizationII,Ageof

Empires,andSimCityvariants),thoughthetilingofthegame

surfaceisusuallyhidden.

Isometrictilesgiveanartificialsenseofdepthasiftheplayer's

viewpointissomewhereupinthesky,lookingdownoverthe

playingarea.Ofcourse,thisviewisartificialsinceno

perspectiveeffectsareapplied;thetilesintherow"nearest"

theviewerarethesamesizeandshapeasthetilesinthemost

"distant"rowatthetopofthescreen.Thisiswheretheterm

isometriccomesfrom:anisometricprojectionisa3Dprojection

thatdoesn'tcorrectfordistance.

Theillusionthateachrowoftilesisfurtherbackinsidethe

gameissupportedbythez-orderingofthings(sprites,objects)

drawnintherows.Anobjectonarownearerthefrontisdrawn

afterthoseonrowsfurtherback,hidinganythingbehindit.This

isthecaseinFigure13-1,wheretheblackandwhitecolumn

partiallyhidesthealienstandingtworowsbehindit.

Therearevariouswaysoflabelingthex-axisandy-axisofa

isometrictilemap.I'llusethestandardstaggered-map

approachillustratedinFigure13-2.

Figure13-2.Astaggeredisometrictilemap

Oddandevenrowsareoffsetfromeachother,whichmeans

thatthetilecoordinatescanbealittletrickytoworkoutasa

spritemovesbetweenrows.

AlienTilesusestilecoordinatestopositionspritesandother

inFigure13-3.

Figure13-3.Thesurface.gifimage

Mostisometricgamesconstructthesurfacefromindividual

tiles,whichallowsthefloorspacetoberenderedincrementally

andtochangedynamicallyovertime.Thedrawbackisthe

increasedcomplexity(andtime)indrawingthetilestothe

screen.Drawingtheindividualtilesinback-to-frontroworderis

necessary,witheachdiamondrepresentedbyarectangularGIF

withtransparentcorners.Thecodingproblemsarelikethe

difficultiesdetailedinChapter12,withpositioningbricks

correctlyonscreenastheJumpingSpritemovedaround.Anddo

youwanttogothroughallofthatagain?

Often,thesurfacewillbeacompositeofseverallayersoftileof

differentsizes.Forexample,theremaybeseverallargegreen

tilesfortheterrain,partiallycoveredoverwithsmallergrass,

dirt,andsandtilestocreatevariety.Fringetilesareemployed

tobreakuptheregularityoftheedgesbetweentwolarge

areas,suchasthelandandthesea.Thegraphiconafringetile

representstheedge(orfringe)ofonekindoftile,andthestart

ofanother.

Movement

AlienTilesoffersfourdirectionsforaspritetofollow:northeast,

southeast,southwest,andnorthwest,asillustratedbyFigure

13-4.

Theuserinterfacemapsthesedirectionstothefourcornersof

thesekeysmakesthespritemoveonesteptothe

north,east,south,andwestmovement.

Figure13-4.Directionsofmovementforasprite

Therangeofdirectionsisdictatedbythetileshape,toalarge

extent,anddiamondsaren'ttheonlypossibility.Forinstance,a

numberofstrategygamesusehexagonstoformaHexmap

(Figure13-5),whichallowssixcompassdirectionsoutofatile.

Figure13-5.Ahexmap

Movementaroundanisometrictilesurfaceisoftenbasedon

singlestepsbetweentiles.It'snotpossibleforaspritetomove

causesasinglestep,andtheusermustholddownthekeyto

makethespritesprintacrossseveraltiles.Akeypresstriggers

amethodcalltoupdatethesprite'sposition,whichisupdated

onscreenat40FPS.

Thisrateisfastenoughtodealwiththeuserkeepingakeyconstantly

helddown.

surfacemovesintheoppositedirection,togetherwiththeother

objectsandsprites.Forinstance,whentheplayermovestothe

northeast,theuser'sspritestaysstillbuttheground

underneathitshiftstothesouthwest.

Thisnonmovementisonlytruefortheuser'ssprite;thealienspritesdo

movefromonetiletoanother.

Aswithaside-scroller,thisapproachkeepstheuser'ssprite

centerstageatalltimes.Incommercialgames,aplayer'ssprite

doessometimesmovetotheedgeofthescreen,usuallyasa

preludetoenteringanewgamelevel.

PlacingaSprite/Object

Caremustbetakenwithobjectplacementsotheillusionofan

objectstandingontopatileismaintained.Figure13-6shows

thatthepositioningofasprite'stop-leftcorner,soplantingits

"feet"onthetile'ssurfacecanbealittlehardtocalculate:

Figure13-6.Placingaspriteontoatile

Thespritecanoccupyscreenspaceabovethetilebutshould

notoverlapthebottomleftandrightedgesofthediamond.Ifit

does,theimagewillseemtobepartlyinthenextrow,

weakeningtheroworderingeffect.I'llimplementthis

placementruleinmycodelater.

TheTileMapSurface

TheAlienTilessurfacecontainsno-goareasthatthesprites

cannotenter.Theseincludetheoceanaroundtheedgesofthe

tiledsurface,alake,apond,andfourredsquares(allvisiblein

Figure13-1).Theno-goareasaredefinedinaconfigurationfile

Thegamesurfacehastwokindsofobjectsrestingonit:blocks

andpickups.Ablockfullyoccupiesatile,preventingasprite

frommovingontoit.Theblockimagecanbeanything;I

employvariouscolumnsandgeometricshapes.Aplayercan

removeapickupfromthesurfacewhenit'sstandingonthe

Blocksandpickupsarehardertoimplementthanno-goareas

sincetheyoccupyspaceonthegamesurface.Thismeansthat

aspritecanmovebehindoneandbepartiallyhidden.Pickups

posemoreproblemsthanblockssincetheycanberemoved

fromatile.

Moresophisticatedgameshaveamuchgreatervarietyof

surfaceobjects.Twocommontypesarewallsandportals

(doors).Awallbetweentwotilespreventsaspritefrommoving

betweenthetiles.Aportalisoftenusedasawayofmoving

betweentilemaps,forexamplewhenmovingtothenextgame

levelorenteringabuildingwithitsownfloorplan.

TheAliens

AlienTilesofferstwotypesofaliens:thosethatactivelychase

aftertheplayer(AlienAStarSpriteobjects)andthosethat

TheAlienAStarSpriteclassusesA*(pronounced"Astar")pathfindingto

chasetheplayer,whichwillbeexplainedlaterinthischapter.

Ingeneral,aliendesignopensthedoortointelligentbehavior

code,oftenbasedonArtificialIntelligence(AI)techniques.

Surprisinglythough,quitebelievablespritebehaviorcanoften

behackedtogetherwiththeuseofafewrandomnumbersand

illustration.

ClassDiagramsforAlienTiles

Figure13-7showsasimplifiedsetofclassdiagramsfor

prunedbackforclarity.

Figure13-7.AlienTilesclassesdiagram

(simplified)

TheAlienTilesJFrameandtheAlienTilesPanelJPanelimplementthe

windowedanimationframeworkintroducedinChapters2and3,

BugRunnerofChapter11,andJumpingJackofChapter12usethesame

technique.

Pausing,resuming,andquittingarecontrolledviaAlienTiles'

windowlistenermethods.Theframerateissetto40FPS,which

isstilltoofastforthealiensprites;theyaresloweddown

WorldDisplaydisplaysthesurfaceimageandtheblocks,pickups,

andspritesrestingonthesurface.Thetilecoordinatesforthe

entitiesarestoredinaWorldItemsobject,usingaTileOccupier

objectforeachone.WorldDisplayactsasacommunicationlayer

betweentheplayerandthealiens.

Figure13-7includesasmallspriteinheritancehierarchy,rooted

atSprite,whichisshownonitsowninFigure13-8.

Figure13-8.ThespritehierarchyinAlienTiles

MostofthemethodsinSpriteareextendedoroverriddenby

tile-relatedmethodsinitssubclasses.Tilecoordinatesare

utilizedbythegamemostofthetime,supportedbymethodsin

TiledSpritethoughasprite'spixellocation(maintainedbySprite)

### Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 13.  An Isometric Tile Game

Tải bản đầy đủ ngay(0 tr)

×