maps

37
Maps Tran Minh Triet – Nguyen Khac Hu Faculty of Information Technology University of Science, VNU-HCM

Upload: boybuon205

Post on 17-Nov-2014

1.383 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Maps

MapsMaps

Tran Minh Triet – Nguyen Khac HuyFaculty of Information TechnologyUniversity of Science, VNU-HCM

Page 2: Maps

Scrolling MapScrolling Map

Page 3: Maps

Scrolling mapScrolling map

Page 4: Maps

Scrolling mapScrolling map

Vertical scrollingChicken InvadersSkyforce

Horizontal scrollingMarioContra

How do they do that?

Page 5: Maps

Horizontal Scrolling MapHorizontal Scrolling Map

Source: http://www.xnadevelopment.com/tutorials/scrollinga2dbackground/ScrollingA2DBackground.shtml

Page 6: Maps

Horizontal scrolling mapHorizontal scrolling map

Create Sprite class

Adding the following objects to the top of the Sprite class.

//The size of the Sprite public Rectangle Size;

//Used to size the Sprite up or down from // the original image public float Scale = 1.0f;

Page 7: Maps

Horizontal scrolling mapHorizontal scrolling map

Modify the LoadContent method of the Sprite class

//Load the texture using the Content Pipeline public void LoadContent(ContentManager

theContentManager, string theAssetName) { mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName); Size = new Rectangle(

0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); }

Page 8: Maps

Horizontal scrolling mapHorizontal scrolling map

Change the Draw method

//Draw the sprite to the screen public void Draw(SpriteBatch theSpriteBatch) { theSpriteBatch.Draw(

mSpriteTexture, Position,

new Rectangle(0, 0, SpriteTexture.Width, mSpriteTexture.Height),

Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);

}

Page 9: Maps

Horizontal scrolling mapHorizontal scrolling map

Adding some new Sprites to Game class

Sprite mBackgroundOne; Sprite mBackgroundTwo; Sprite mBackgroundThree;

Page 10: Maps

Horizontal scrolling mapHorizontal scrolling map

Modify the Initialize methodprotected override void Initialize() { // TODO: Add your initialization logic here mBackgroundOne = new Sprite(); mBackgroundOne.Scale = 2.0f;

mBackgroundTwo = new Sprite(); mBackgroundTwo.Scale = 2.0f;

mBackgroundThree = new Sprite(); mBackgroundThree.Scale = 2.0f;

base.Initialize(); }

Page 11: Maps

Horizontal scrolling mapHorizontal scrolling mapLoading the content protected override void LoadContent() {

//…

mBackgroundOne.LoadContent(this.Content,"Background01");

mBackgroundOne.Position = new Vector2(0, 0);

mBackgroundTwo.LoadContent(this.Content, Background02");

mBackgroundTwo.Position = new Vector2( mBackgroundOne.Position.X +

mBackgroundOne.Size.Width, 0);

mBackgroundThree.LoadContent(this.Content,“Background03");

mBackgroundThree.Position = new Vector2( mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width, 0);}

Page 12: Maps

Horizontal scrolling mapHorizontal scrolling map

Moving the background images across the screen in a snake like fashionAdding the code to Update method

if (mBackgroundOne.Position.X < -mBackgroundOne.Size.Width) mBackgroundOne.Position.X = mBackgroundThree.Position.X + mBackgroundThree.Size.Width;

if (mBackgroundTwo.Position.X < -mBackgroundTwo.Size.Width) mBackgroundTwo.Position.X = mBackgroundOne.Position.X + mBackgroundOne.Size.Width;

if (mBackgroundThree.Position.X < -mBackgroundThree.Size.Width) mBackgroundThree.Position.X = mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width;

Page 13: Maps

Horizontal scrolling mapHorizontal scrolling map

Update the positions

Vector2 aDirection = new Vector2(-1, 0); Vector2 aSpeed = new Vector2(160, 0);

mBackgroundOne.Position += aDirection * aSpeed *

(float)gameTime.ElapsedGameTime.TotalSeconds; mBackgroundTwo.Position += aDirection * aSpeed *

(float)gameTime.ElapsedGameTime.TotalSeconds; mBackgroundThree.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

Page 14: Maps

Horizontal scrolling mapHorizontal scrolling map

Drawing the Sprites

protected override void Draw(GameTime gameTime) {

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code herespriteBatch.Begin();mBackgroundOne.Draw(this.spriteBatch);mBackgroundTwo.Draw(this.spriteBatch);mBackgroundThree.Draw(this.spriteBatch);spriteBatch.End();

base.Draw(gameTime); }

Page 15: Maps

Vertical Scrolling MapVertical Scrolling Map

Page 16: Maps

Vertical scrollingVertical scrolling

Can you think of some things you can change?

How hard would it be to make this scroll vertically?

Do you think you know what you might change?

Page 17: Maps

2D Scrolling Map2D Scrolling Map

Page 18: Maps

2D Scrolling Map2D Scrolling Map

Age of Empire II

Age of Wonder

StarCraft

Square map

Rhombus map

Page 19: Maps

Square mapSquare map

Page 20: Maps

Square map - ViewportSquare map - Viewport

A rectangular region shown on your screen

Page 21: Maps

Square map – How to doSquare map – How to do

Load cell map textures

protected override void LoadMapCells(int[,] matrixmap) { this.cells = new MapCell[MAP_SIZE_IN_CELL.Width,

MAP_SIZE_IN_CELL.Height]; for (int i = 0; i < MAP_SIZE_IN_CELL.Width; i++) { for (int j = 0; j < MAP_SIZE_IN_CELL.Height; j++) { Texture2D imgCell =

Game.Content.Load<Texture2D>(SQUARE_MAP_IMG _PATH + “BG0001");

// Create a new map cell this.cells[i, j] = new MapCell(

imgCell, i * CELL_SIZE.Width, j * CELL_SIZE.Height); } }}

Page 22: Maps

Square map – How to doSquare map – How to do

Define two points to draw the viewport

// Get x-axis of the left top cell index of the viewportint i1 = (int)this._currentRootCoordinate.X / CELL_SIZE.Width;

// Get y-axis of the left top cell index of the viewportint j1 = (int)this._currentRootCoordinate.Y / CELL_SIZE.Height;

// Get x-axis of the right-bottom cell index of the viewportint i2 = (int)(this._currentRootCoordinate.X + Game.Window.ClientBounds.Width) / CELL_SIZE.Width;

// Get y-axis of the right-bottom cell index of the viewportint j2 = (int)(this._currentRootCoordinate.Y + Game.Window.ClientBounds.Height) / CELL_SIZE.Height;

Page 23: Maps

Square map – How to doSquare map – How to do

Completing the DrawBackGround methodfor (int i = i1; i <= i2; i++) {

for (int j = j1; j <= j2; j++) { try{

// calculating the coodinate on screen and drawing Rectangle recToDraw = new Rectangle((int)( this.cells[i, j].X -

this._currentRootCoordinate.X), (int)(this.cells[i, j].Y-this._currentRootCoordinate.Y), CELL_SIZE.Width, CELL_SIZE.Height);

spriteBatch.Draw(this.cells[i, j].Background, recToDraw, Color.White);

} catch{ }}

}

Page 24: Maps

Rhombus mapRhombus map

Page 25: Maps

Rhombus mapRhombus map

ViewportWhat’s difference from the square map?

Rhombus mapHow to draw a rhombus map?Find a position of 2D point

Page 26: Maps

Rhombus map - TechniqueRhombus map - Technique

Page 27: Maps

Rhombus map – How to doRhombus map – How to do

Convert square coordinate to rhombus coordinate

Page 28: Maps

Rhombus map – How to doRhombus map – How to do

// Abstract methods to convert coordinate

public abstract Point PointToCell(Point p);public abstract Point CenterToCell(Point p);public abstract Point CellToPoint(Point cell);public abstract Point CellToCenter(Point cell);

Page 29: Maps

More shape mapMore shape map

ViewportWhat’s difference from?

How to change coordinate?Find a position of 2D poin

Page 30: Maps

Demo Rhombus map

Demo Rhombus map

Page 31: Maps

Map EditorMap Editor

Page 32: Maps

Results (1)Results (1)

Page 33: Maps

Results (2)Results (2)

Page 34: Maps

Results (3)Results (3)

Page 35: Maps

Results (4)Results (4)

Page 36: Maps

Q&AQ&A

?

Page 37: Maps

ReferencesReferences

Websitehttp://www.xnadevelopment.com

EbookLearning XNA 3.0 – Aaron Reed