We'realsogoingtoadd a smartcamera, whichwillfollowtheactionsotheplayercanseewhat's goingonandfinallywillintroduce a statemachine, whichisusedtocoordinateandsequencealloftheeventsthatgoon.
I willturnthisfrombeingjust a playabledemointoanactualgame.
Let's getstarted.
I'm startingoffwiththecodethat's availableonthegetup, anditisthecodethatwecreatedinthefirstvideo, so I'm goingtochangethatonetoit, too.
Atanygiventime, theplayercancontroloneofmultipleunitsonthescreen, sowe'regoingtoneedtostore a pointerfortheobjectthat's undercontrolonthispointwillbeusedtodirectalloftheuserinputtowardsthatparticularobject.
Iftheobjectundercontrolpointerisn't a Noelpointeranditmustbepointingtosomethingandhopefullythat's goingtobe a unit.
Infact, whatwe'regoingtoensurethroughtherestofthegamecodeisthat P objectundercontrolalwayspointsto a wormunit, soitwon't pointto a missileorsomedebris.
I'm goingtousethesaidanexkeystomakethewormjumpin a particulardirectionthatforleftan X forrightandtomaketheunitjump, wejustneedtosetthevelocityvectordirectly.
Sohere I'm settingitwith a positive X component.
Soitjumpstowardstherightandsaid, I'm goingtosetthatto a negative X componentbecauseit's jumping.
We'llhavetosetthewhitecomponenttobenegative.
Soit's workingagainstgravity.
Let's take a look.
So I'vecreatedmypearlynoisemap, and I'm goingtoadd a unittoitnow.
But I'm goingtodisguisetheinadequacyofthephysicsenginebymakingtheplayerjump.
Becausetheplayerthenhastotakesomerisks.
Theyhavetoestimatewheretheirunitwillendup.
If I hadanotherunit, thisisnowtheunitundercontrolnowwormshisgameallaboutangles.
Weusedthemtoworkoutthetrajectorytofireweaponson.
I'm alsogoingtousetheangletoeworkout, a jumpingtrajectorysotheuserhassomecontroloverthejumpbecauserightnow, justbyjumpingbackwardsandforwards, I don't getverymanyoptionsofwherethewormcouldbeontheterrain.
Thisiswherewe'regoingtostarthavingsomepropertiestothewormclass, andinthisinstance, I wanttoadd a shootingangle, whichisfloatingpoint.
I'm goingtoaddsomemoreclausesnowtotheuserinput.
Sowhentheuserpressesthe a key, I wantthecursortorotatecounterclockwise.
Nowagain, assumingthattheobjectundercontrolis a worm, I'm goingtocasttheobjectcontrolpointerto a typeofseatwormisthis, thenallowsmetoaccesstheshootinganglevariable.
Sowhilstthe a keyishelddown, I'm goingtobesubtractingfromtheshootinganglewithreferencetoeverylapsetimejusttomakeitsmooth.
But I'm alsogoingtoaddinsomeprotectionthatifwegobelowminuspi, itwrapsbackaroundtwopluspi.
Instead, what I wanttodoisgettheshootinganglefortheobjectsundercontrol, whichforwillbeforthatwormthatwe'renowcontrollingandusethisangletocreate a vectorinmuchthesameway.
Wejustcreatedthecursor.
However, now I nolongerneed a jumpintheotherdirection.
Everythingisgovernedbythecursor.
SowhentheuserpressestheZedkey, hopefullytheunitwillmoveinthatdirectiontotake a look.
Buttheunitsononif I jump, it's sortofnudgingsideways.
Solet's movethecursorverticallyandjump.
Yeah, wecanseethewormjumpsvertically, andnowtheuserhas a lotmorecontroloverthejumps.
Andwhenweput a timelimitlateronhowmuchtimetheuserhastoplay, itbecomes a littlebitmorepressurebecauseittakestimetomovethiscursor.
I thinkit's morenaturaltohavethecamerafollowtheobjectthat's undercontrol, orindeed, anyotherobjectthatwewantedtofollow, forexample, a missilelateron.
So I'm goingtocauseanotherpointer, whichpointstotheobjectthatwewantthecameratocenteronthispieceofcodefromthefirstvideo, makesurethecameraisalwayswithin a sensiblespacewithintheworldmap, sowedon't gooutofbounds, soit's importantwedoallcameramanipulationbeforeweclampiton.
Weonlywanttomovethecameraautomatically.
Ifthetrackingobjectisnot, nopointit.
Andinthissituation I wanttosetthecameras X coordinatetobeequaltotheobjects.
Weretrackingis X coordinate, offsetbyhalfofthescreen, sotheobjectisalwaysinthemiddleofthescreen.
I don't wanttodoexactlythesameforwhy, inmuchthesamewaywehackedtheobjectundercontrolvariable, I'm goingtohackthecameravariable, too, justfornow, let's take a look.
So I putmyunit's onthescreenandwecouldseethecamerahasalreadystartedtofollowit.
Letmemovethecursorup a littlebit, causingtojump, andwecouldseethecameranowfollows.
Theunitkeepstheunitinthecenterofthescreen.
Infact, weblowtheinitself.
Italwaysstaysinthecenterbutareclamping.
Codeshouldensurethatwhenwegettowardsthemapboundaries, theunitmovesfromthecenterandwedon't have a horriblecrashNowlet's makeittotheuserCanfire a missileintheoriginalWormsgame.
Theideaisthatwespecify a targetcoordinate, whichactsas a guideforwheretheactualcoordinatesshouldbe, butwemakeitsotheactualcoordinateslagbehindthetargetcoordinates.
Onenicesideeffectofcontrollingthecamerainthiswayisthatiftheusermovesthecursortowardstheedgesofthescreen, likewe'vealwaysbeenabletodointhissettle, itcanonlypan a certainamountbeforetheequationsbalanceout.
Someeventscanbeglobal, andinthiscase I'm goingtouse a resetevent, whichmeansthatatanypoint I cangetto a knownstate, I'm goingtobeusingJulian's tocontrolstatetransitions.
Andwhen I'm in a state, I mayperformsomeonetimeactionsorsetsomemorebillionsasoutputs, whichcanbeusedtocontrolmultipleotheractions.
Let's have a thinkaboutourgamestatemachine.
I know I'm goingtowanttostartwith a resetstate, andthisiswhere I'm goingtoinitializeallofthegamevariablesandthenwanttogoto a secondstatetogeneratetheterrain.
So I'm adding a flagthatrepresentstheoverallstabilityofthegame, andwe'llhavetomaintainthisflagnow.
Thereareprobablymoreefficientlocationstoputthis, butforstability, what I'm goingtodoisit a ratethroughalloftheobjectsinmylistofobjectsandseeifthey'restable?
Flagsaresettotrue.
Ifalloftheobjectsarestable, then I'm goingtoassumethegameisstable.
We'vegot a littlebitmorecontroloverthis, so I'm goingtosetthecameratrackingobjectthissetbacktoknow, pointerwhentheobjectnolongerexists, thismakessomesortofsense.
So I'm startingtheapplicationandimmediatelywe'vegeneratedtheterrainandwe'vedeployed a unit.
Wesawtheunitfallin.
I'venogotcontrolovertheunit.
Andif I firetheweapon, thecameraisautomaticallyfollowingthemissilelandsandwe'renowwaitingforthedebristosettlebeforethecamerapansbacktotheunitundercontrol.
Andso I think a statemachinehasmadethisallquitesmoothandeasytobuildasthestatemachinehasallowedustocollectanduse a wholebunchofBooleanflagsin a farmoreelegantwaythanlotsofnestedifstatements, infact, we'venothadtomakeanyalterationstotheunderlyingcodetofacilitatetransitionsfromonemodeofgameplaytothenext.
Have a thinkaboutsubscribingallofthesourcecodeisavailableonGethopeonifyou'veanyproblems, leavesomecommentsintheYouTubecommentsorlogontothediscordserverthat's justgrowinginstrength.
And I justthought a friendlyreminderthatoccasionally I alsostreamontwitch.