//
// MainScene.cpp
// ActorsRun
//
// Created by 刘宏敏 on 14-5-26.
//
//
#include "MainScene.h"
#include "CCLuaEngine.h"
USING_NS_CC;
#define JUDGE(__FLOOR__, __LAST__) \
if ( !open.contains(__FLOOR__) && !close.contains(__FLOOR__) && __FLOOR__ -> getLandForm() != FloorRiver ) \
{ \
open.pushBack( __FLOOR__ ); \
__FLOOR__ -> setColor( Color3B(100, 100, 100) ); \
__FLOOR__ -> setLastFloor( __LAST__ ); \
} \
Scene *MainScene::scene()
{
auto scene = Scene::create();
auto layer = MainScene::create();
scene -> addChild( layer );
return scene;
}
bool MainScene::init()
{
if ( Layer::init() )
{
return true;
}
return false;
}
void MainScene::onEnter()
{
Layer::onEnter();
MainScene::createGround();
MainScene::createRobot();
MainScene::robotGo_1();
// MainScene::robotGo_2();
MainScene::back();
}
void MainScene::createGround()
{
for (int y = 0; y < 18; y++)
{
for (int x = 0; x < 28; x++)
{
auto _floor = Floor::create( Color4B(255, 255, 255, 255) );
_floor -> setPosition(x * (_floor -> getContentSize().width + 1), y * (_floor -> getContentSize().height + 1));
_floor -> setX(x);
_floor -> setY(y);
_floor -> setLandForm( FloorGround );
this -> addChild( _floor );
if ( (x == 15 && y < 7) || (x == 3 && y < 10) || (y == 12 && x < 20) || (x == 5 && y > 8) || (x == 13 && y > 1) )
{
_floor -> setColor( Color3B(0, 0, 0) );
_floor -> setLandForm( FloorRiver );
}
ground.pushBack( _floor );
}
}
}
void MainScene::createRobot()
{
end = Floor::create( Color4B(0, 255, 255, 10) );
end -> setPosition( (ground.at( Floor::at(24, 14) )) -> getPosition() );
end -> setX(24);
end -> setY(14);
this -> addChild( end );
origin = Floor::create( Color4B(255, 255, 0, 255) );
origin -> setPosition( (ground.at( Floor::at(0, 0) )) -> getPosition() );
origin -> setX(0);
origin -> setY(0);
origin -> setH( fabs((end -> getX() - origin -> getX())) + fabs( end -> getY() - origin -> getY() ) );
origin -> setG(0);
this -> addChild( origin );
open.pushBack( origin );
}
void MainScene::robotGo_1()
{
while (1)
{
Vector< Floor * >::iterator index = open.begin();
Vector< Floor * >::iterator minF = index;
for (; index != open.end(); index++)
{
if ( (*index) -> getF() < (*minF) -> getF() )
{
minF = index;
}
}
Floor *_origin = (*minF);
handleFloor(_origin, _origin -> getX(), _origin -> getY() - 1);
handleFloor(_origin, _origin -> getX(), _origin -> getY() + 1);
handleFloor(_origin, _origin -> getX() - 1, _origin -> getY());
handleFloor(_origin, _origin -> getX() + 1, _origin -> getY());
open.eraseObject( _origin );
close.pushBack( _origin );
_origin -> setColor( Color3B(0, 255, 0) );
Floor *getEnd = open.at( open.size() - 1 );
if ( getEnd -> getX() == end -> getX() && getEnd -> getY() == end -> getY() )
{
CCLOG("ENDG");
return;
}
}
}
void MainScene::back()
{
Floor *index = open.at( open.size() - 1 );
while ( index -> getLastFloor() != NULL )
{
index -> setColor( Color3B(255, 0, 0) );
index = index -> getLastFloor();
}
}
void MainScene::handleFloor(Floor *_floor, int x, int y)
{
if ( x < 0 || y < 0 )
{
return;
}
Floor *handle = ground.at( Floor::at(x, y) );
handle -> setX(x);
handle -> setY(y);
handle -> setH( fabs((end -> getX() - handle -> getX())) + fabs( end -> getY() - handle -> getY() ) );
handle -> setG( fabs((origin -> getX() - handle -> getX())) + fabs( origin -> getY() - handle -> getY() ) );
JUDGE( handle, _floor );
}
void MainScene::robotGo_2()
{
for (int i = 0; i < 35; i++)
{
Vector< Floor * >::iterator index = open.begin();
Vector< Floor * >::iterator minF = index;
for (; index != open.end(); index++)
{
if ( (*index) -> getF() < (*minF) -> getF() )
{
minF = index;
}
}
Floor *_origin = (*minF);
Floor *_bottom = ground.at( Floor::at(_origin -> getX(), _origin -> getY() - 1) );
Floor *_up = ground.at( Floor::at(_origin -> getX(), _origin -> getY() + 1) );
Floor *_left = ground.at( Floor::at(_origin -> getX() - 1, _origin -> getY()) );
Floor *_right = ground.at( Floor::at(_origin -> getX() + 1, _origin -> getY()) );
_bottom -> setX( _origin -> getX() );
_bottom -> setY( _origin -> getY() - 1 );
_up -> setX( _origin -> getX() );
_up -> setY( _origin -> getY() + 1 );
_left -> setX( _origin -> getX() - 1 );
_left -> setY( _origin -> getY());
_right -> setX( _origin -> getX() + 1 );
_right -> setY( _origin -> getY());
_bottom -> setColor( Color3B(100, 100, 100) );
_up -> setColor( Color3B(100, 100, 100) );
_left -> setColor( Color3B(100, 100, 100) );
_right -> setColor( Color3B(100, 100, 100) );
_bottom -> setH( fabs((end -> getX() - _bottom -> getX())) + fabs( end -> getY() - _bottom -> getY() ) );
_up -> setH( fabs((end -> getX() - _up -> getX())) + fabs( end -> getY() - _up -> getY() ) );
_left -> setH( fabs((end -> getX() - _left -> getX())) + fabs( end -> getY() - _left -> getY() ) );
_right -> setH( fabs((end -> getX() - _right -> getX())) + fabs( end -> getY() - _right -> getY() ) );
_bottom -> setG( fabs((origin -> getX() - _bottom -> getX())) + fabs( origin -> getY() - _bottom -> getY() ) );
_up -> setG( fabs((origin -> getX() - _bottom -> getX())) + fabs( origin -> getY() - _bottom -> getY() ) );
_left -> setG( fabs((origin -> getX() - _bottom -> getX())) + fabs( origin -> getY() - _bottom -> getY() ) );
_right -> setG( fabs((origin -> getX() - _bottom -> getX())) + fabs( origin -> getY() - _bottom -> getY() ) );
_bottom -> setLastFloor( _origin );
_up -> setLastFloor( _origin );
_left -> setLastFloor( _origin );
_right -> setLastFloor( _origin );
// JUDGE(_bottom);
// JUDGE(_up);
// JUDGE(_left);
// JUDGE(_right);
open.eraseObject( _origin );
close.pushBack( _origin );
_origin -> setColor( Color3B(255, 255, 255) );
Vector< Floor * >::iterator getEnd = open.begin();
for (; getEnd != open.end(); getEnd++)
{
if ( (*index) == end )
{
CCLOG("OVER");
return;
}
}
}
}