切割类游戏中的切割线类 Version2

来源:互联网 发布:侠客风云传怎么改数据 编辑:程序博客网 时间:2024/04/28 19:59

第二种方案,利用 PRKit 这个库来做,这个库是我在论坛上面听说到的

用了下,感觉不是很好,要符合我的需求的话要改蛮多地方,主要是纹理坐标的定位不是很精确

主要优点就是效率还不错,其中那块关于 opengl 的绘制代码比较值得回味。

另外这个库也使用了他人写的复杂多边形三角化算法,

我的需求里面不要求对多么复杂的多边形进行三角化细分,所以用起来感觉也有点儿鸡肋。

在最后一种方案的时候我会提到,其实利用 CCTexture2D 的纹理平铺特性,完全可以满足到我的需求~

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 BYCutLineExSYNTHESIZE_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


原创粉丝点击