Author Topic: rope code work  (Read 2996 times)

lynx1987

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
rope code work
« on: Mon, Jan 21, 2013 »
Hi  flixelers,I am a game coder from asia.I was making rope  a few days ago and I found there is little code or content about rope in this forum.so I decide to show my code.

It is not very cool but it works well.

my game video:
http://v.youku.com/v_show/id_XNTA1MjMwNjA4.html

Code: [Select]
//
//  SkillRope.h
//  Canabalt
//
//  Created by lynx on 12-12-28.
//
//

@class Player;
@class PlayState;
@class GibEmitter;

@interface SkillRope : FlxSprite
{
    FlxTileblock *rope;
    Player *player;
    GibEmitter * emitter;
}

+ (id)initWithPlayer:(Player *)p l:(PlayState *)layer;
- (id)initWithPlayer:(Player *)p l:(PlayState *)layer;

-(void)setRopeVisible:(BOOL)isVisible;
-(BOOL)isRopeVisible;

- (void)addEmitter:(FlxGroup *) layer;

@property float angle;

@end

Code: [Select]
//
//  SkillRope.m
//  Canabalt
//
//  Created by lynx on 12-12-28.
//
//

#import "SkillRope.h"
#import "Player.h"
#import "PlayState.h"
#import "GibEmitter.h"

static NSString * ImgCrane1 = @"rope.png"; //beam
static NSString * ImgEmitter = @"rope_emitter.png";
static BOOL isStart = NO;
static BOOL isSetSpeed;

@implementation SkillRope

+ (id)initWithPlayer:(Player *)p l:(PlayState *)layer;
{
    return [[[self alloc]initWithPlayer:p l:layer] autorelease];
}

- (id)initWithPlayer:(Player *)p l:(PlayState *)layer;
{
    player = p;
   
    rope = [[FlxTileblock alloc] initWithX:0 y:0 width:0 height:0];
    rope.width  = 32;
    rope.height = 1;
    rope.visible = NO;
   
    self.visible = YES;
    self.exists = YES;
   
    [rope loadGraphic:ImgCrane1];
    [layer add:rope];
    [layer add:self];
   
//    emitter = [[GibEmitter gibEmitterWithGibCount:16 graphics:ImgEmitter] retain];
//    emitter.delay = -5;
//    emitter.minParticleSpeed = CGPointMake(-240,
//                                           320);
//    emitter.maxParticleSpeed = CGPointMake(-240,
//                                           240);
//    emitter.minRotation = 0;
//    emitter.maxRotation = 0;
//    emitter.gravity = 30;
//    [emitter setXSpeedWithParam1:-300 param2:800];
//    [emitter setYSpeedWithParam1:-30 param2:80];
//    //[emitter setXSpeedWithParam1:100 param2:200];
//    emitter.height = player.height;
//    emitter.width = player.width;
   
    //[layer add:emitter];
   
    return self;
}

- (void)addEmitter:(FlxGroup *) layer;
{
    //[layer add:emitter];
}

- (void)dealloc
{
    [rope release];
    [emitter release];
    [super dealloc];
}

- (void)update
{
    if ( !rope.visible )
        return;
//    if (rope.width > 480 )
//        return;
    static float startX;
    static float ropeX;
   
    rope.x = player.x;
    rope.y = player.y;
    rope.velocity = player.velocity;
    rope.scrollFactor = player.scrollFactor;
   
//    emitter.x = player.x;
//    emitter.y = player.y;
//    emitter.velocity = player.velocity;
//    emitter.scrollFactor = player.scrollFactor;
   
    if ( rope.width  > 160 && !isStart ){
        isStart = YES;
        startX = player.x;
        ropeX = 0;
        [player handleEvent:PLAYER_EVENT_ROPE];
        player.velocity = CGPointMake( player.velocity.x + 200, 100 );
        //[emitter start:YES];
    }
    else{
        rope.width += 8;
        //rope.width += 8;
        [rope loadGraphic:ImgCrane1];
    }
   
    if ( isStart ){
        if ( player.x - startX < 0 )
            return;
   
        float speedX = player.velocity.x;
        if ( ropeX == 0 )
            ropeX = player.x;

        if ( rope.angle > 270 )
        {
            player.velocity = CGPointMake( speedX + 20, player.velocity.y );
            player.acceleration = CGPointMake( player.acceleration.x, 1000);
        }
        else
        {
            player.velocity = CGPointMake( speedX - 25, player.velocity.y );
            player.acceleration = CGPointMake( player.acceleration.x, -2000);
            rope.y += 40;
        }
       
        rope.angle = 320 - (player.x - ropeX) / 4;
        angle = rope.angle;
    }
   
    if ( rope.angle < 200 || player.velocity.x < 50 )
        [self setRopeVisible:NO];
   
//    if ( isStart ){
//        float speedX = player.velocity.x;
//        float speedY = player.velocity.y;
//       
//        if ( rope.angle > 270 ){
//            player.velocity = CGPointMake( speedX + 20, speedY + 20 );
//        }
//        else{
//            if ( !isSetSpeed )
//            {
//                player.velocity = CGPointMake( player.velocity.x, -300 );
//                isSetSpeed = YES;
//                speedY = player.velocity.y;
//            }
//            player.velocity = CGPointMake( speedX - 20, speedY - 20 );
//        }
//       
//        if ( player.x - startX > 256 ){
//            rope.angle = 320 - (player.x - startX) / 6;
//            angle = rope.angle;
//        }
//    }
    //[super update];
}

-(BOOL)isRopeVisible;
{
    return rope.visible;
}

-(void)setRopeVisible:(BOOL)isVisible;
{
    rope.visible = isVisible;
    rope.width  = 32;
    rope.height = 1;
    rope.angle = 325;
    isStart = NO;
    isSetSpeed = NO;
    [player setState:PLAYER_STATE_ROPE value:NO];
    if ( isVisible == NO ){
        //player.velocity = CGPointMake(300,-1200);
        player.acceleration = CGPointMake( 1, 1200);
        emitter.visible = NO;
        //player.velocity = CGPointMake( player.velocity.x, -1200);
        //[emitter stop];
    }
}

@end

I make the rope with tileblock,and add angle handling in the render function,like this:
Code: [Select]
{
    if (texture == nil)
        return;
    CGPoint _point = [self getScreenXY];
    [[self class] bind:texture.texture];
    glPushMatrix();
    glTranslatef(_point.x, _point.y, 0);
    if ( angle != 0 )
        glRotatef(angle, 0.0f, 0.0f, 1.0f);
   
    glVertexPointer(2, GL_SHORT, sizeof(GLshort)*4, &verticesUVs[0]);
    glTexCoordPointer(2, GL_SHORT, sizeof(GLshort)*4, &verticesUVs[2]);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
    glPopMatrix();
}

and~~ that is all.
« Last Edit: Mon, Jan 21, 2013 by lynx1987 »

Wenzl

  • Member
  • **
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: rope code work
« Reply #1 on: Wed, Jan 23, 2013 »
Nice that you share this. I will check it out :)

Esti

  • Active Member
  • ***
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: rope code work
« Reply #2 on: Wed, Jan 23, 2013 »
Awesome work

Check it out on Mochi!

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: rope code work
« Reply #3 on: Mon, Mar 25, 2013 »
Thanks for sharing!
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart