Skip to content

Commit

Permalink
isogrid
Browse files Browse the repository at this point in the history
  • Loading branch information
heweitykc committed Jul 4, 2014
1 parent d3109c2 commit 2e42862
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 33 deletions.
104 changes: 75 additions & 29 deletions proj/动画资源/cocos/isogrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool IsoGrid::init()
_slider1->loadBarTexture("ui/sliderTrack.png");
_slider1->loadSlidBallTextures("ui/sliderThumb.png", "ui/sliderThumb.png", "");
_slider1->loadProgressBarTexture("ui/sliderProgress.png");
_slider1->setPosition(Vec2(100, 100));
_slider1->setPosition(Vec2(70, 300));
addChild(_slider1);
_slider1->addEventListener(CC_CALLBACK_2(IsoGrid::sliderEvent1, this));
_slider1->setPercent(50);
Expand All @@ -28,18 +28,53 @@ bool IsoGrid::init()
_slider2->loadBarTexture("ui/sliderTrack.png");
_slider2->loadSlidBallTextures("ui/sliderThumb.png", "ui/sliderThumb.png", "");
_slider2->loadProgressBarTexture("ui/sliderProgress.png");
_slider2->setPosition(Vec2(100, 70));
_slider2->setPosition(Vec2(70, 280));
addChild(_slider2);
_slider2->addEventListener(CC_CALLBACK_2(IsoGrid::sliderEvent2, this));
_slider2->setPercent(50);

_label = LabelTTF::create("Hello World", "Arial", 12);
_label->setPosition(110, 200);
_slider3 = Slider::create();
_slider3->loadBarTexture("ui/sliderTrack.png");
_slider3->loadSlidBallTextures("ui/sliderThumb.png", "ui/sliderThumb.png", "");
_slider3->loadProgressBarTexture("ui/sliderProgress.png");
_slider3->setPosition(Vec2(70, 260));
addChild(_slider3);
_slider3->addEventListener(CC_CALLBACK_2(IsoGrid::sliderEvent3, this));
_slider3->setPercent(50);

_label = LabelTTF::create("Hello World", "Arial", 8);
_label->setPosition(100, 310);
addChild(_label);

_items[0] = GridItem::create();
addChild(_items[0]);
_items[1] = GridItem::create();
addChild(_items[1]);
_items[2] = GridItem::create();
addChild(_items[2]);
_items[3] = GridItem::create();
addChild(_items[3]);
_items[4] = GridItem::create();
addChild(_items[4]);
_items[5] = GridItem::create();
addChild(_items[5]);

return true;
}

void IsoGrid::moveItem(int roleIndex, int x, int y, int z)
{
float startx = 100;
float starty = 100;
x = startx + x*_cellSize;
z = starty + z*_cellSize;

float sX = x * _cos_y_angle - z * _sin_y_angle;
float z1 = z * _cos_y_angle + x * _sin_y_angle;
float sY = y * _cos_x_angle - z1 * _sin_x_angle;
_items[roleIndex]->setPosition(sX, sY);
}

void IsoGrid::setXAngle(float xangle)
{
_xangle = xangle;
Expand All @@ -62,27 +97,25 @@ void IsoGrid::setCellSize(float size)

void IsoGrid::drawPoint(float x, float y, float z)
{
float sX = x * cos(_yangle) - z * sin(_yangle);
float z1 = z * cos(_yangle) + x * sin(_yangle);
float sY = y * cos(_xangle) - z1 * sin(_xangle);
float z2 = z1 * cos(_xangle) + y * sin(_xangle);
log("x=%f,y=%f", sX, sY);
_drawNode->drawDot(Vec2(sX, sY), 1, Color4F(1, 1, 1, 1));
}
float startx = 100;
float starty = 100;

void IsoGrid::drawCell(float x, float y, float z)
{
float x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3;

float sX = x * _cos_y_angle - z * _sin_y_angle;
float z1 = z * _cos_y_angle + x * _sin_y_angle;
float sY = y * _cos_x_angle - z1 * _sin_x_angle;
//float z2 = z1 * cos(_xangle) + y * sin(_xangle);
//log("x=%2f,y=%2f", sX, sY);
_drawNode->drawDot(Vec2(sX, sY), 1, Color4F(1, 1, 1, 1));
}

void IsoGrid::sliderEvent1(Ref *pSender, Slider::EventType type)
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED){
int percent = _slider1->getPercent();
int percent = _slider1->getPercent();
_xangle = (percent / 100.0f) * 3.1415926f - 3.1415926f / 2;
}
redraw();
this->unschedule(schedule_selector(IsoGrid::redraw));
this->scheduleOnce(schedule_selector(IsoGrid::redraw), 0.01);
}

void IsoGrid::sliderEvent2(Ref *pSender, Slider::EventType type)
Expand All @@ -91,37 +124,50 @@ void IsoGrid::sliderEvent2(Ref *pSender, Slider::EventType type)
int percent = _slider2->getPercent();
_yangle = (percent / 100.0f) * 3.1415926f - 3.1415926f/2;
}
redraw();
this->unschedule(schedule_selector(IsoGrid::redraw));
this->scheduleOnce(schedule_selector(IsoGrid::redraw), 0.05);
}

void IsoGrid::sliderEvent3(Ref *pSender, Slider::EventType type)
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED){
int percent = _slider3->getPercent();
_cellSize = percent*0.5;
}
this->unschedule(schedule_selector(IsoGrid::redraw));
this->scheduleOnce(schedule_selector(IsoGrid::redraw), 0.05);
}

void IsoGrid::drawPoints(Vector<cocos2d::Vec3>* vecs)
{
_vecs = vecs;
}

void IsoGrid::redraw()
void IsoGrid::redraw(float dt)
{
/*
for (size_t i = 0; i < _vecs->size(); i++)
{
drawPoint(_vecs->at(i).x, _vecs->at(i).y, _vecs->at(i).z);
}
*/
_sin_x_angle = sin(_xangle);
_cos_x_angle = cos(_xangle);
_sin_y_angle = sin(_yangle);
_cos_y_angle = cos(_yangle);
_drawNode->clear();
float startx = 100;
float starty = 100;
for (int i = 0; i < 18; i++){
for (int j = 0; j < 18; j++){
log("%f %f", _xangle, _yangle);
drawPoint(startx + i*_cellSize, 0, starty+j * _cellSize);
drawPoint(startx + i * _cellSize, 0, starty + (j + 1) * _cellSize);
drawPoint(startx + (i + 1) * _cellSize, 0, starty + j * _cellSize);
drawPoint(startx + (i + 1) * _cellSize, 0, starty + (j + 1) * _cellSize);
log("draw end");
}
}

char str0[256] = {0};
sprintf(str0, "xangle=%f,yangle=%f", _xangle, _yangle);
sprintf(str0, "xangle=%2f,yangle=%2f", _xangle, _yangle);
_label->setString(str0);

moveItem(0, 0, 0, 0);
moveItem(1, 1, 0, 2);
moveItem(2, 2, 0, 2);
moveItem(3, 3, 0, 2);
moveItem(4, 4, 0, 2);
moveItem(5, 5, 0, 2);
}
18 changes: 14 additions & 4 deletions proj/动画资源/cocos/isogrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
#include "roadsearch.h"
#include "item.h"

class IsoGrid : public cocos2d::Layer
{
Expand All @@ -14,23 +16,31 @@ class IsoGrid : public cocos2d::Layer
void setYAngle(float angle);
void setZAngle(float angle);
void setCellSize(float size);
void drawPoint(float x, float y, float z);
void drawPoints(cocos2d::Vector<cocos2d::Vec3>* vecs);
void drawCell(float x, float y, float z);
void redraw();
inline void drawPoint(float x, float y, float z);
void drawPoints(cocos2d::Vector<cocos2d::Vec3>* vecs);
void redraw(float dt);
void sliderEvent1(Ref* pSender, cocos2d::ui::Slider::EventType type);
void sliderEvent2(Ref* pSender, cocos2d::ui::Slider::EventType type);
void sliderEvent3(Ref* pSender, cocos2d::ui::Slider::EventType type);
void moveItem(int roleIndex, int x, int y, int z);
private:
float _xangle;
float _yangle;
float _zangle;
float _cellSize;
float _sin_x_angle;
float _cos_x_angle;
float _sin_y_angle;
float _cos_y_angle;
cocos2d::Vector<cocos2d::Vec3>* _vecs;
RoadSearch _search;

cocos2d::DrawNode* _drawNode;
cocos2d::ui::Slider* _slider1;
cocos2d::ui::Slider* _slider2;
cocos2d::ui::Slider* _slider3;
cocos2d::LabelTTF* _label;
GridItem* _items[6];
};

#endif
11 changes: 11 additions & 0 deletions proj/动画资源/cocos/item.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "item.h"

USING_NS_CC;

bool GridItem::init()
{
_drawNode = DrawNode::create();
_drawNode->drawDot(Vec2(0,0), 2, Color4F(1, 1, 0, 1));
addChild(_drawNode);
return true;
}
17 changes: 17 additions & 0 deletions proj/动画资源/cocos/item.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef __ITEM_H__
#define __ITEM_H__

#include "cocos2d.h"

class GridItem : public cocos2d::Layer
{
public:
virtual bool init();
CREATE_FUNC(GridItem);
int gridx, gridy, gridz;

private:
cocos2d::DrawNode* _drawNode;
};

#endif

0 comments on commit 2e42862

Please sign in to comment.