1. Ivan Vučica
  2. CatchDroid

Commits

Ivan Vučica  committed 6fc3a99

Teleport action.

  • Participants
  • Parent commits d9182db
  • Branches default

Comments (0)

Files changed (5)

File assets/pages/16-16.actions

View file
+{
+  3-1 = {
+    type = "teleport";
+    destinationX = 127;
+    destinationY = 127;
+  };
+}

File src/Page.h

View file
 
   int _tiles[PAGE_WIDTH*PAGE_HEIGHT];
   GLfloat _textureCoordinates[2 * 6 * PAGE_WIDTH * PAGE_HEIGHT];
+  NSDictionary * _actions;
 }
 @property (nonatomic, retain) Texture * tileset;
 - (id) initWithPageX: (int)pageX 
 - (BOOL) loadFromFile: (NSString*) path;
 - (void) draw;
 - (BOOL) isTileTypePassable: (int) tileType;
-- (BOOL) isTilePassableAtX: (int)x y: (int) y;
+- (BOOL) isTilePassableAtX: (int)x y: (int)y;
+- (NSDictionary*) actionForX: (int)x y: (int)y;
 @end

File src/Page.m

View file
       return nil;
     }
   }
+  
+  success = [self loadActionsFromFile: [NSString stringWithFormat: @"pages/%d-%d.actions", pageX, pageY]];
+  if (!success)
+  {
+    // ignore
+  }
 
   return self;
 }
 - (void) dealloc
 {
+  [_actions release];
   [_tileset release];
   [super dealloc];
 }
   [self setTileset: [Texture textureWithPath: @"tiles1.png"]]; 
   return YES;
 }
+- (BOOL) loadActionsFromFile: (NSString *)path
+{
+  Asset * asset = [Asset assetWithPath: path];
+  if (!asset)
+  {
+    return NO;
+  }
+
+  return [self loadActionsFromString: [asset string]];
+}
+- (BOOL) loadActionsFromString: (NSString *)source
+{
+  NSData* plistData = [source dataUsingEncoding:NSUTF8StringEncoding];
+  NSString *error = nil;
+  NSPropertyListFormat format = 0;
+  NSDictionary* plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
+  NSLog(@"Loading actions from %@: %@", source, plist);
+  if (error)
+  {
+    NSLog(@"Error loading actions: %@", error);
+    return NO;
+  }
+  _actions = [plist retain];
+  return YES;
+}
 - (void) draw
 {
   glEnable(GL_TEXTURE_2D);
 
   return tileTypePassable[tileType];
 }
-- (BOOL) isTilePassableAtX: (int)x y: (int) y
+- (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]];
 }
+- (NSDictionary*) actionForX: (int)x y: (int)y
+{
+  NSString * actionKey = [NSString stringWithFormat: @"%d-%d", x, y];
+  NSDictionary * action = [_actions objectForKey: actionKey];
+  return action;
+}
 @end

File src/Scene.h

View file
   Character * _player;
 
   SEL _direction;
+
+  float _fadeProgress;
 }
 @property (retain) NSMutableDictionary * pages;
 @property (assign, nonatomic) int playerX;

File src/Scene.m

View file
   // others would have: [ch translateWithMultiplier: 1];
   // (to apply 'progress' animation)
   [_player draw];
+
+  if (_fadeProgress < 1)
+  {
+    glPushMatrix();
+    glDisable(GL_TEXTURE_2D);
+    glEnableClientState(GL_VERTEX_ARRAY);
+    glEnableClientState(GL_COLOR_ARRAY);
+    GLfloat colors[] = {
+      0, 0, 0, 1.-fabs(_fadeProgress),
+      0, 0, 0, 1.-fabs(_fadeProgress),
+      0, 0, 0, 1.-fabs(_fadeProgress),
+      0, 0, 0, 1.-fabs(_fadeProgress),
+      0, 0, 0, 1.-fabs(_fadeProgress),
+      0, 0, 0, 1.-fabs(_fadeProgress),
+    };
+    NSLog(@"%g", 1.-fabs(_fadeProgress));
+    GLfloat vertices[] = {
+      -0.5, -0.5,
+       0.5, -0.5,
+       0.5, 0.5,
+
+       0.5, 0.5,
+      -0.5, 0.5,
+      -0.5, -0.5,
+    };
+    glVertexPointer(2, GL_FLOAT, 0, vertices);
+    glColorPointer(4, GL_FLOAT, 0, colors);
+    glScalef(16,16,1);
+    glDrawArrays(GL_TRIANGLES, 0, 6);
+    glDisableClientState(GL_COLOR_ARRAY);
+    glDisableClientState(GL_VERTEX_ARRAY);
+    glPopMatrix();
+  }
+
 }
 
 - (void) update: (double)dt
 {
+  GLfloat oldProgress = [_player progress];
   [_player update: dt];
+  if (oldProgress < 1 && [_player progress] > 1)
+  {
+    // just finished walking.
+    NSDictionary * action = [[self playerPage] actionForX: _playerX % PAGE_WIDTH y: _playerY % PAGE_HEIGHT]; 
+    if(action)
+    {
+      if([[action objectForKey: @"type"] isEqual: @"teleport"])
+      {
+        _fadeProgress = 0;
+        self.playerX = [[action objectForKey: @"destinationX"] intValue];
+        self.playerY = [[action objectForKey: @"destinationY"] intValue];
+      }
+    }
+  }
+  _fadeProgress += dt;
+
   if(_direction)
     [self performSelector: _direction];
 }