Commits

Ivan Vučica  committed d9182db

Collisions. Pages are now drawn in correct place under the player. More pages added to assets.

  • Participants
  • Parent commits de8c795

Comments (0)

Files changed (9)

File assets/pages/15-15

+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00 

File assets/pages/15-16

-00 08 09 10 11 12 00 00
-00 16 17 18 19 20 00 00
-00 00 00 01 00 00 00 00
-00 00 00 01 00 00 00 00
-00 00 00 01 00 00 00 00
 00 00 00 00 00 00 00 00
-00 00 02 02 02 02 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 08 10 11 12 00 00 00
+00 16 18 19 20 00 00 00
+00 00 01 00 00 00 00 00
+00 00 02 02 02 02 02 02
 00 00 00 00 00 00 00 00 

File assets/pages/16-15

+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00
+00 00 08 10 12 00 00 00
+00 00 16 18 20 00 00 00
+00 00 00 00 00 00 00 00
+00 00 00 00 00 00 00 00 

File assets/pages/16-16

 00 00 00 01 00 00 00 00
 00 00 00 01 00 00 00 00
 00 00 00 00 00 00 00 00
-00 00 02 02 02 02 00 00
+02 02 02 02 02 02 00 00
 00 00 00 00 00 00 00 00 
                pageY: (int)pageY;
 - (BOOL) loadFromFile: (NSString*) path;
 - (void) draw;
+- (BOOL) isTileTypePassable: (int) tileType;
+- (BOOL) isTilePassableAtX: (int)x y: (int) y;
 @end
                             PAGE_WIDTH * PAGE_HEIGHT
                            ];
 
+static BOOL tileTypePassable[] = {
+  YES, YES, YES, NO,  NO,  NO,  NO,  NO,
+  NO,  NO,  NO,  NO,  NO,  NO,  NO,  NO,
+  NO,  NO,  YES,  NO,  NO,  NO,  NO,  NO
+};
+
 @implementation Page
 @synthesize tileset=_tileset;
 
 }
 - (void) draw
 {
-    glEnable(GL_TEXTURE_2D);
-    glEnableClientState(GL_VERTEX_ARRAY);
-    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+  glEnable(GL_TEXTURE_2D);
+  glEnableClientState(GL_VERTEX_ARRAY);
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   
-    glBindTexture(GL_TEXTURE_2D, [_tileset textureId]);
-    glVertexPointer(2, GL_FLOAT, 0, gridVertices);
-    glTexCoordPointer(2, GL_FLOAT, 0, _textureCoordinates);
-    glDrawArrays(GL_TRIANGLES, 0, 6 * (PAGE_WIDTH * PAGE_HEIGHT));
-    glBindTexture(GL_TEXTURE_2D, 0);
+  glBindTexture(GL_TEXTURE_2D, [_tileset textureId]);
+  glVertexPointer(2, GL_FLOAT, 0, gridVertices);
+  glTexCoordPointer(2, GL_FLOAT, 0, _textureCoordinates);
+  glDrawArrays(GL_TRIANGLES, 0, 6 * (PAGE_WIDTH * PAGE_HEIGHT));
+  glBindTexture(GL_TEXTURE_2D, 0);
     
-    glDisableClientState(GL_VERTEX_ARRAY);
-    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-    glDisable(GL_TEXTURE_2D);
+  glDisableClientState(GL_VERTEX_ARRAY);
+  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+  glDisable(GL_TEXTURE_2D);
+}
+
+- (BOOL) isTileTypePassable: (int) tileType
+{
+  if(!(tileType < sizeof(tileTypePassable) / sizeof(tileTypePassable[0])))
+    return NO;
+
+  return tileTypePassable[tileType];
+}
+- (BOOL) isTilePassableAtX: (int)x y: (int) y
+{
+  LOGI("isTilePassableAtX: %d y: %d -- tiletype is %d", x, y, _tiles[y*PAGE_WIDTH + x]);
+  return [self isTileTypePassable: _tiles[y * PAGE_WIDTH + x]];
 }
 @end
 #import <Foundation/Foundation.h>
 
 @class Character;
+@class Page;
 
 @interface Scene : NSObject
 {
   SEL _direction;
 }
 @property (retain) NSMutableDictionary * pages;
-@property (assign) int playerX;
-@property (assign) int playerY;
+@property (assign, nonatomic) int playerX;
+@property (assign, nonatomic) int playerY;
 @property (retain) Character * player;
 
 @property (assign) SEL direction;
+
+- (void) draw;
+- (void) update: (double)dt;
+- (void) movePlayerUp;
+- (void) movePlayerDown;
+- (void) movePlayerLeft;
+- (void) movePlayerRight;
+
+- (Page *) playerPage;
+- (Page *) pageForMapX: (int)mapX
+                  mapY: (int)mapY;
+- (Page *) pageForPageX: (int)pageX
+                  pageY: (int)pageY;
 @end
 
   _pages = [NSMutableDictionary new];
   _player = [[Character alloc] initWithTexturePath: @"player.png"];
 
-  self.playerX = 16*8;
-  self.playerY = 16*8;
+  self.playerX = 16*8 + 3;
+  self.playerY = 16*8 + 4;
 
   return self;
 }
 {
   if(_player.progress < 1) return;
   [_player setDirection: 0];
+  if(![[self pageForMapX: self.playerX mapY: self.playerY - 1] isTilePassableAtX: (self.playerX) % PAGE_WIDTH y: (self.playerY - 1) % PAGE_HEIGHT])
+    return;
   self.playerY--;
 }
 - (void) movePlayerRight
 {
   if(_player.progress < 1) return;
   [_player setDirection: 1];
+  if(![[self pageForMapX: self.playerX + 1 mapY: self.playerY] isTilePassableAtX: (self.playerX + 1) % PAGE_WIDTH y: (self.playerY) % PAGE_HEIGHT])
+    return;
   self.playerX++;
 }
 - (void) movePlayerDown
 {
   if(_player.progress < 1) return;
   [_player setDirection: 2];
+  if(![[self pageForMapX: self.playerX mapY: self.playerY + 1] isTilePassableAtX: (self.playerX) % PAGE_WIDTH y: (self.playerY + 1) % PAGE_HEIGHT])
+    return;
   self.playerY++;
 }
 - (void) movePlayerLeft
 {
   if(_player.progress < 1) return;
   [_player setDirection: 3];
+  if(![[self pageForMapX: self.playerX - 1 mapY: self.playerY] isTilePassableAtX: (self.playerX - 1) % PAGE_WIDTH y: (self.playerY) % PAGE_HEIGHT])
+    return;
   self.playerX--;
 }
 
     {
       int currentPageX = playerPageX + i;
       int currentPageY = playerPageY + j;
-      NSString * currentPage = [NSString stringWithFormat: @"%d-%d", currentPageX, currentPageY];
-      Page * page = [_pages objectForKey: currentPage];
-      if (!page)
-      {
-        page = [[[Page alloc] initWithPageX: currentPageX pageY: currentPageY] autorelease];
-        if(!page)
-          continue;
-        [_pages setObject: page forKey: currentPage];
-        if(![_pages objectForKey: currentPage])
-          LOGI("Caching the page failed?!");
-      }
-      
+      Page * page = [self pageForPageX: currentPageX
+                                 pageY: currentPageY];
+      if(!page)
+        continue;
+
       glPushMatrix();
-      glTranslatef(i * PAGE_WIDTH, -j * PAGE_HEIGHT, 0);
+      glTranslatef(i * PAGE_WIDTH, (-j-1) * PAGE_HEIGHT + 1, 0);
       [page draw];
       glPopMatrix();
     }
   if(_direction)
     [self performSelector: _direction];
 }
+- (Page *) playerPage
+{
+  return [self pageForMapX: _playerX
+                      mapY: _playerY];
+}
+- (Page *) pageForMapX: (int)mapX
+                  mapY: (int)mapY
+{
+  int pageX = mapX / PAGE_WIDTH;
+  int pageY = mapY / PAGE_HEIGHT;
+  return [self pageForPageX: pageX
+                      pageY: pageY];
+}
+- (Page *) pageForPageX: (int)pageX
+                  pageY: (int)pageY
+{
+  NSString * currentPage = [NSString stringWithFormat: @"%d-%d", pageX, pageY];
+  Page * page = [_pages objectForKey: currentPage];
+  if (!page)
+  {
+    page = [[[Page alloc] initWithPageX: pageX
+                                  pageY: pageY] autorelease];
+    if(!page)
+      return nil;
+    [_pages setObject: page forKey: currentPage];
+    if(![_pages objectForKey: currentPage])
+      LOGI("Caching the page failed?!");
+   }
+   return page;
+}
 @end
 

File src/catchdroid.m

 -(void)terminateDisplay
 {
     LOGI("Terminating display");
-    [page release];
-    page = nil;
     [_controls release];
     _controls = nil;
-    [ch release];
-    ch = nil;
     [_scene release];
     _scene = nil;