From 355d831f70eef0db65e980aa2b52bd78b77a7d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ruben=20Mu=CC=88ller?= Date: Sun, 21 Aug 2011 14:31:28 +0200 Subject: [PATCH] Collision detection! --- src/Events.pbi | 18 +++++++++++++----- src/Functions.pbi | 31 ++++++++++++++++++++++++++++--- src/Globals.pbi | 9 ++++++++- src/Structs.pbi | 5 +++++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/Events.pbi b/src/Events.pbi index c7620c0..ffa6e4b 100644 --- a/src/Events.pbi +++ b/src/Events.pbi @@ -48,7 +48,9 @@ Procedure HandleEventRunningGame() ; Player movement If KeyboardReleased(#PB_Key_Left) And Player\X > 0 - Player\X-1 + If TileIsType(#TILESET_TYPE_FLOOR, Player\X-1, Player\Y) Or TileIsType(#TILESET_TYPE_GRASS, Player\X-1, Player\Y) + Player\X-1 + EndIf ; Camera positioning If Player\X < (Cam\X + Cam\Padding) And Cam\X > 0 @@ -57,8 +59,10 @@ Procedure HandleEventRunningGame() EndIf If KeyboardReleased(#PB_Key_Right) And Player\X < #MAP_WIDTH-1 - Player\X+1 - + If TileIsType(#TILESET_TYPE_FLOOR, Player\X+1, Player\Y) Or TileIsType(#TILESET_TYPE_GRASS, Player\X+1, Player\Y) + Player\X+1 + EndIf + ; Camera positioning If Player\X > (Cam\X + Cam\Width - Cam\Padding) And (Cam\X + Cam\Width) < #MAP_WIDTH-1 Cam\X + 1 @@ -66,7 +70,9 @@ Procedure HandleEventRunningGame() EndIf If KeyboardReleased(#PB_Key_Up) And Player\Y > 0 - Player\Y-1 + If TileIsType(#TILESET_TYPE_FLOOR, Player\X, Player\Y-1) Or TileIsType(#TILESET_TYPE_GRASS, Player\X, Player\Y-1) + Player\Y-1 + EndIf ; Camera positioning If Player\Y< (Cam\Y + Cam\Padding) And Cam\Y > 0 @@ -75,7 +81,9 @@ Procedure HandleEventRunningGame() EndIf If KeyboardReleased(#PB_Key_Down) And Player\Y < #MAP_HEIGHT-1 - Player\Y+1 + If TileIsType(#TILESET_TYPE_FLOOR, Player\X, Player\Y+1) Or TileIsType(#TILESET_TYPE_GRASS, Player\X, Player\Y+1) + Player\Y+1 + EndIf ; Camera positioning If Player\Y > (Cam\Y + Cam\Height - Cam\Padding) And (Cam\Y + Cam\Height) < #MAP_HEIGHT-1 diff --git a/src/Functions.pbi b/src/Functions.pbi index f29de92..03e1f36 100644 --- a/src/Functions.pbi +++ b/src/Functions.pbi @@ -15,8 +15,21 @@ Procedure InitTileset() For i=0 To 5 For j=0 To 5 - Tileset(count) = CopySprite(GFXTileset, #PB_Any) - ClipSprite(Tileset(count), #TILE_SIZE*j, #TILE_SIZE*i, #TILE_SIZE, #TILE_SIZE) + Tileset(count)\ID = CopySprite(GFXTileset, #PB_Any) + ClipSprite(Tileset(count)\ID, #TILE_SIZE*j, #TILE_SIZE*i, #TILE_SIZE, #TILE_SIZE) + + Tileset(count)\Type = #TILESET_TYPE_FLOOR + Select count + Case 0 + Tileset(count)\Type = #TILESET_TYPE_PLAYER + Case 2 + Tileset(count)\Type = #TILESET_TYPE_WALL + Case 3 + Tileset(count)\Type = #TILESET_TYPE_SOLID + Case 4 + Tileset(count)\Type = #TILESET_TYPE_GRASS + EndSelect + count+1 Next Next @@ -112,7 +125,7 @@ EndProcedure ; Map itself Procedure DrawTile(number.i, x.i, y.i) - DisplaySprite(Tileset(number), #TILE_SIZE*x, #TILE_SIZE*y) + DisplaySprite(Tileset(number)\ID, #TILE_SIZE*x, #TILE_SIZE*y) EndProcedure Procedure DrawMap() @@ -125,6 +138,18 @@ Procedure DrawMap() Next EndProcedure +Procedure TileIsType(type.i, X.i, Y.i) + Define istype.b + + istype = #False + + If Tileset(TileMap(X, Y)\TileNumber)\Type = type + istype = #True + EndIf + + ProcedureReturn istype +EndProcedure + ; The player Procedure DrawPlayer() DrawTile(Player\TileNumber, Player\X-Cam\X, Player\Y-Cam\Y) diff --git a/src/Globals.pbi b/src/Globals.pbi index cb2b7f5..5c2d433 100644 --- a/src/Globals.pbi +++ b/src/Globals.pbi @@ -6,6 +6,13 @@ #MAP_HEIGHT = 120 ; TODO: Aus der XML lesen +; Tileset entries +#TILESET_TYPE_PLAYER = 0 +#TILESET_TYPE_FLOOR = 1 +#TILESET_TYPE_WALL = 2 +#TILESET_TYPE_SOLID = 3 +#TILESET_TYPE_GRASS = 4 + ; Global variables Global Fullscreen = 0 Global Title.s = "ESCape - Build " + Str(#PB_Editor_BuildCount) @@ -37,7 +44,7 @@ Global Font_H3 = LoadFont(3, "Verdana", 10) ; Graphics Global GFXLogo.i Global GFXTileset.i -Global Dim Tileset.i(36) +Global Dim Tileset.TilsetEntry(36) ; Sounds Global MenuSoundStarted = 0 diff --git a/src/Structs.pbi b/src/Structs.pbi index f4e3f70..d8031c0 100644 --- a/src/Structs.pbi +++ b/src/Structs.pbi @@ -31,6 +31,11 @@ Structure GameMenu List Entries.GameMenuItem() EndStructure +Structure TilsetEntry + ID.i + Type.i +EndStructure + Structure Tile X.i Y.i