カッティング系ゲームのカッティングライン系Version 2


2つ目は、PRKitというライブラリを利用して、このライブラリは私がフォーラムで聞いたものです.
使って、感じはあまり良くなくて、私の需要に合うならば多くの地方を直して、主にテクスチャの座標の位置付けがあまり正確ではありません
主な利点は効率がいいことですが、openglに関するペイントコードは後味があります.
またこのライブラリは他の人が書いた複雑な多角形の三角化アルゴリズムも使用しています.
私のニーズの中では、どんなに複雑な多角形を三角化して細分化する必要はありませんので、使うと少し鶏の肋骨のような感じがします.
最後の案でお話ししますが、実はCCTexture 2 Dのテクスチャフラット特性を利用して、私のニーズを完全に満たすことができます~
BYCutLineEx.h
//
//  BYCutLineEx.h
//  HungryBear
//
//  Created by Bruce Yang on 12-8-26.
//  Copyright (c) 2012  EricGameStudio. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "PRFilledPolygon.h"
#import "SynthesizeSingleton.h"

@interface BYCutLineEx : NSObject {
    
    //     ,       ~
    CCTexture2D* _texBar;
    CCTexture2D* _texSegment;
    
    
    //     ~
    CCSprite* _spBarA;
    CCSprite* _spBarB;
    
    
    //   ,  ,  !
    PRFilledPolygon* _polySegment;
}


+(BYCutLineEx*) getInstance;


-(BYCutLineEx*) addToParent:(CCNode*)nodeParent;


-(void) setPointA:(CGPoint)pointA pointB:(CGPoint)pointB;


-(void) disappear;


@end

BYCutLineEx.mm
//
//  BYCutLineEx.mm
//  HungryBear
//
//  Created by Bruce Yang on 12-8-26.
//  Copyright (c) 2012  EricGameStudio. All rights reserved.
//

#import "BYCutLineEx.h"

@implementation BYCutLineEx

SYNTHESIZE_SINGLETON_FOR_CLASS(BYCutLineEx)

-(id) init {
    if((self = [super init])) {
        // 1。    ~
        CCTextureCache* texCache = [CCTextureCache sharedTextureCache];
        
        _texBar = [texCache addImage:@"img_cut_line_bar.png"];
        _texSegment = [texCache addImage:@"img_cut_line_segment.png"];
        
        
        // 2。          ~
        NSMutableArray* arrPoints = [NSMutableArray arrayWithCapacity:4];
        
        [arrPoints addObject:[NSValue valueWithCGPoint:ccp(-50, -25)]];
        [arrPoints addObject:[NSValue valueWithCGPoint:ccp(50, -25)]];
        [arrPoints addObject:[NSValue valueWithCGPoint:ccp(50, 25)]];
        [arrPoints addObject:[NSValue valueWithCGPoint:ccp(-50, 25)]];
        
        _polySegment = [[PRFilledPolygon alloc] initWithPoints:arrPoints andTexture:_texSegment];
        [_polySegment setVisible:NO];
        
        
        // 3。       ~
        _spBarA = [[CCSprite alloc] initWithTexture:_texBar];
        [_spBarA setVisible:NO];
        
        _spBarB = [[CCSprite alloc] initWithTexture:_texBar];
        [_spBarB setVisible:NO];
    }
    return self;
}


-(void) removeFromParent {
    [_polySegment removeFromParentAndCleanup:NO];
    
    [_spBarA removeFromParentAndCleanup:NO];
    [_spBarB removeFromParentAndCleanup:NO];
}


-(BYCutLineEx*) addToParent:(CCNode*)nodeParent {
    //                ~
    [self removeFromParent];
    
    [nodeParent addChild:_spBarA z:10];
    [nodeParent addChild:_spBarB z:10];
    [nodeParent addChild:_polySegment z:10];
    
    return self;
}


-(void) setPointA:(CGPoint)pointA pointB:(CGPoint)pointB {    
    [_polySegment setVisible:YES];
    
    float fScaleFactor = CC_CONTENT_SCALE_FACTOR();
    float fHalfWidth = ccpDistance(pointA, pointB) / 2.0f * fScaleFactor;
    float fHalfHeight = 16.0f / 2.0f * fScaleFactor;
    NSArray* arrPoints = [NSArray arrayWithObjects:
                          [NSValue valueWithCGPoint:ccp(-fHalfWidth, -fHalfHeight)], 
                          [NSValue valueWithCGPoint:ccp(fHalfWidth, -fHalfHeight)], 
                          [NSValue valueWithCGPoint:ccp(fHalfWidth, fHalfHeight)], 
                          [NSValue valueWithCGPoint:ccp(-fHalfWidth, fHalfHeight)], nil];
    
//    float fWidth = ccpDistance(pointA, pointB) * CC_CONTENT_SCALE_FACTOR();
//    float fHeight = 6.0f * CC_CONTENT_SCALE_FACTOR();
//    NSArray* arrPoints = [NSArray arrayWithObjects:
//                          [NSValue valueWithCGPoint:ccp(0, 0)], 
//                          [NSValue valueWithCGPoint:ccp(fWidth, 0)],
//                          [NSValue valueWithCGPoint:ccp(fWidth, fHeight)],
//                          [NSValue valueWithCGPoint:ccp(0, fHeight)], nil];
    
    [_polySegment setPoints:arrPoints];
	[_polySegment setPosition:ccpMidpoint(pointA, pointB)];
    
    CGPoint stickVector = ccpSub(ccp(pointA.x, pointA.y), ccp(pointB.x, pointB.y));
    float stickAngle = ccpToAngle(stickVector);
    [_polySegment setRotation:-1 * CC_RADIANS_TO_DEGREES(stickAngle)];
    
    [_spBarA setVisible:YES];
    [_spBarB setVisible:YES];
    
    [_spBarA setPosition:pointA];
    [_spBarB setPosition:pointB];
}


-(void) disappear {
    [_polySegment setVisible:NO];
    
    [_spBarA setVisible:NO];
    [_spBarB setVisible:NO];
}


-(void) dealloc {
    
    [_polySegment release];
    
    //     、     ~
    [_spBarA release];
    [_spBarB release];
    
    
    //        ~
    CCTextureCache* texCache = [CCTextureCache sharedTextureCache];
    [texCache removeTexture:_texBar];
    [texCache removeTexture:_texSegment];
    
    [super dealloc];
}


@end