51
LECTURE 1 Announcements

Lecture 1

  • Upload
    ludwig

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 1. Announcements. Tic is over!. Collaboration policies!. Design checks. How was the signup process? Were they helpful ? Would you rather have more TA hours instead?. You can run demos!. cs195n_demo tac2 zdavis. cs195n_demo tic zdavis. Tic first impressions. - PowerPoint PPT Presentation

Citation preview

Lecture 1Announcements1Tic is over!

2Collaboration policies!

3Design checksHow was the signup process?

Were they helpful?

Would you rather have more TA hours instead?

4You can run demos!cs195n_demo tac2 zdaviscs195n_demo tic zdavis

5Tic first impressionsLots of carrying around separate x, yThis leads to copy-pasting xs code to write ys codeWhich leads to forgetting to replace an x with a yUse Vec2i and Vec2f instead!Lots of mouse usage, better than TAs:-D6BGD plugThe Brown Game Developers group is meeting on Friday, 4:30p-5:30p in 345

Can be a useful source of advice, within the bounds of collab policyQuestions?AnnouncementsLecture 1ViewportsMotivationViewportsSometimes screen space is hardTheoretically everything can be done in screen spaceBut some things can be very hardMost notably, when the entire game isnt visible at one timeVideo time!110,01,02,00,11,12,10,21,22,20,01,02,00,11,12,10,21,22,2Game-spaceGame space vs. Screen spaceIn nearly all games, it makes sense to think of the game as existing in its own spaceThe UI in screen space has a view into the game world which is just like any other UI elementScreenUIUIUIUI0,01,02,02,10,21,22,2The mathScale = screen size / game size(in pixels per game coordinate unit)Game point to screen:Minus game upper leftMultiply by scaleAdd screen upper leftScreen point to game:Do the OPPOSITE of the steps IN REVERSE1.5, 2.00.5, 0.8120, 20Scale: 100 px/unit1.5, 2.01.0, 1.2100, 120220, 140

220, 14013ImplementationViewports14Implementing viewports(Optional) Set the clip (g.clipRect() )You will draw out of bounds otherwiseSet the transformDraw the game-space in its own coordinatesRestore the transformRestore the clip (if you set it)15The transformFor many of you, this is as simple as:When drawing a viewport, make a note inside your Graphics2D wrapper that you need to transform game->screen coordinates whenever a shape is drawnUnmark when finished drawing the viewportFor cs123 students who want to be fancy, use AffineTransform and the setTransform() and getTransform() of Graphics2D AffineTransform used like OpenGL16Questions?ViewportsLecture 1PathfindingMotivationPathfinding19Why is pathfinding important?NPCs need to navigate an environment that has obstructionsRepresented as a graphGoal: find minimum cost path from A to BCost includes factors such as distance, terrain, position of enemies.

20Dijkstras AlgorithmPathfinding21DijkstrasBasic idea:Process nodes in order of shortest distance from startTo process a node, update cost to each neighbor and add to PriorityQueue, then never process this node againEach node keeps track of shortest distance and pointer to previous nodeWhen its time to process the end node, youre done

22Why Dijkstras can be gross

23A*Pathfinding24General ideaDijkstras assumes its impossible to predict costThis is overly pessimisticIn pathfinding, we at least know the general direction we want to goA* is a graph traversal algorithm that takes advantage of this

How does it work?Uses a heuristic to guess the cost from any given node to the destination nodeHeuristic passed by the callerIn addition to tracking distance from start, track heuristic value for each nodePrioritize in PriorityQueue based on distance+heuristicThis can be as simple as the Euclidean distance between the given and destination node, but can also try to take other factors Just be sure to NEVER OVERESTIMATE (suboptimal results)Questions?PathfindingLecture 1Content Management I28What is content?Content Management I29ContentTypes of contentSprites, images, texturesMusic, sound effectsLevel/map filesScriptsDialogueA single logical piece of content is called an asset

Why not hardcode ASSETS?Content Management I31HardcodingExtreme executable bloatLarge games cannot fit in memoryHave to recompile entire program every time an asset changesStill okay sometimes (e.g. the program icon in Windows)

Your RAM: 8GB

Dragon Age: Origins: 20GB

Solution: break into filesEngine/game can load and unload assets as necessary or desiredIts like JIT for your game content!Non-programmers dont need to compileLevel designers only need to touch map filesArtists only need to touch image filesProgrammers compile builds for everyone elseMore maintainable in generalQuestions?Content Management ILecture 1Tips for Tac IFile ParsingTips for Tac IFile parsing!Good news: game-sideBad news: So many things can go wrong!Map file cant be openedMap file is emptyMap file is a directoryMap file is a JPEGIs a map file, but has inconsistent data

37Parse safelyRead in a line, then parse it, repeatAt least you can report the line count where an error happenedRecommended classes: BufferedReader (for reading lines) Scanner+StringReader (for parsing each line)Catch, wrap, and rethrow exceptionsLevel parsing should only throw a LevelParseException that is caused by other exceptions38If possible, report errors nicelyGood errors:Line 1: , is not a valid map sizeLine 7: Row 3 was 24 tiles wide, map is 25 tiles wideLine 2: Expected boolean, got FLSE insteadBad errors:java.util.InputMismatchException (null)The map could not be parsed. Goodbye.(program crashes with giant stack trace)If this happens you will get an incomplete

39Unit MovementTips for Tac IUnit movementTiles best stored in an array (discrete indices)But game space is continuous!Define tile x,y to take up space [x,x+1) and [y,y+1)Then: Vec2i.fromFloored()Move unit centers with Vec2f.lerpTo()

0,01,02,00,11,12,10,21,22,23.0, 0.00.7, 0.61.5, 2.1Unit exclusionUnit sitting on tile is obviousBut what about while moving?When is it no longer on the first tile?When does it officially reach the second tile?Can it briefly monopolize two tiles at once?

??????

???

???

Miscellaneous TipsTips for Tac IMiscellaneous TipsZooming is multiplicative, not additiveRolling mouse wheel should * or / the scale factor

Watch out for weird behavior when changing a units path while its movingE.g. units moving diagonallyJava Tip of the WeekTips for Tac IGenerics are cool!Youve used generics before but have you ever written them?Its as easy as:

public class SimpleContainer {private T object;public void setObject(T ob) { object = ob; }public T getObject() { return object; }}

Generics are cool!Can use extends and super to bound the type

public class AnimalHouse {private A animal;public void houseAnimal(A a) { animal = a; }public void feedAnimal() { animal.eat(); }}

AnimalHouse kennel; // okayAnimalHouse mountain; // compile errorBounds can be confusingEver used enums in Java? (if not, you will soon!)This is the declaration of the Enum superclass:

public class Enum

Starts to make sense once you realize it doesnt infinitely expandYou will have to do something similar if you want to have a generified GraphNode objectWant to know more?Effective Java by Joshua Bloch has an excellent chapter on genericsGives examples of where advanced generics are usefulCan be found in the back of the Sun Lab

49Questions?Tips for Tac ITic playtesting!YAY!