Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовая ТП Францев Артем БСТ1801.docx
Скачиваний:
64
Добавлен:
12.03.2022
Размер:
1.98 Mб
Скачать

4.4 Класс Bird

ref class Bird : public Fish

{

public:

Bird(Rain^ rain) {

power = 100 + rand() % 20;

xPos = 0; yPos = 25 + rand() % 20;

state = 1 + rand() % 2;

_downY = -1;

rain->EventRainStart += gcnew Rain::Handler(this, &Bird::OnDownMove);

}

~Bird() {};

virtual void OnDownMove() override {

_downY = 0;

speed = 10;

}

virtual void SetPropertiesByPower(int power) override {

level = 1 + power / 25;

weight = 200;

speed = 2;

}

};

4.5 Класс Rain

ref class Rain {

public:

delegate void Handler();

event Handler^ EventRainStart;

Rain() {

RainTickTime = 0;

EventRainStart += gcnew Rain::Handler(this, &Rain::OnRainStart);

};

~Rain() {};

void OnRainStart() {

_rain_tick_time = RAINTICKS;

}

void RainStart() {

EventRainStart();

}

void RainStop() {

RainTickTime = 0;

}

property int RainTickTime {

int get() { return _rain_tick_time; }

void set(int value) { _rain_tick_time = value; }

}

protected:

int _rain_tick_time;

};

4.6 Класс GameEnvironment

ref class GameEnvironment {

public:

System::Collections::Generic::List<Fish^>^ Animals;

System::Collections::Generic::List<Fisher^>^ Fishers;

Rain^ Weather;

delegate void Handler(Fisher^ fisher, Fish^ fish);

Handler^ EventFishCatched;

GameEnvironment() {

Weather = gcnew Rain();

Animals = gcnew System::Collections::Generic::List<Fish^>(0);

Fishers = gcnew System::Collections::Generic::List<Fisher^>(0);

EventFishCatched += gcnew GameEnvironment::Handler(this, &GameEnvironment::OnEventFishCatched);

_time_secs = 450;

};

~GameEnvironment() {};

void FishSpawning() {

while (Animals->Count < FISHES_AT_ONE_TIME) {

Animals->Add(gcnew Fish(Weather, Fishers));

Animals[Animals->Count - 1]->EventDeleteObject +=

gcnew Fish::DeleteHandler(this, &GameEnvironment::FishDeleting);

}

}

void FishDeleting(Object^ sender) {

Animals->Remove((Fish^)sender);

}

void FisherDeleting(Object^ sender) {

Fishers->Remove((Fisher^)sender);

}

void FisherReplace(bool toPirse) {

Fishers->Clear();

if (toPirse) Fishers->Add(gcnew Fisher(Weather));

else Fishers->Add(gcnew Fisherboat(Weather));

Fishers[0]->catching = 1;

Fishers[0]->EventDeleteObject += gcnew Fisher::DeleteHandler(this, &GameEnvironment::FisherDeleting);

}

bool CheckFishcatch(Fisher^ _boat, Fish^ _fish) {

int xdelt = (_boat->xPosPtr + 8) - (_fish->xPos + 10); /* right fishhook side - fish middle (by X) */

int ydelt = (_fish->yPos + 20) - (_boat->yPosPtr + 16 + _boat->ptrLen); /* fish bottom - fishhook bottom */

if (xdelt < 0 || xdelt > 12) return false; /* no collision */

if (ydelt < 0 || ydelt > 16) return false; /* no collision */

if (_boat->lvl != _fish->level) { /* wrong bait */

return false;

}

if (_fish->weight > (rand() % (10000 * 2))) { /* unlucky weight */

return false;

}

return true;

}

void OnEventFishCatched(Fisher^ fisher, Fish^ fish) {

fisher->Catch();

fish->Catched();

Animals->Add(gcnew Bird(Weather));

}

void TimerTickActions(int current_ticks) {

for (int i = 0; i < Fishers->Count; i++) Fishers[i]->Move();

FishSpawning();

int ticks_remain = DEFAULTTICKS;

while (ticks_remain > 0) {

for (int i = 0; i < Animals->Count; i++) Animals[i]->Move();

if (Weather->RainTickTime == 0) {

if ((1 + rand() % 100) > (100 - RAIN_PROBABILITY))

Weather->RainStart();

}

else Weather->RainTickTime -= 1;

_time_secs += 1;

if (_time_secs >= TIMEOFDAY_LEN * 3) _time_secs = 0;

ticks_remain -= current_ticks;

}

}

System::String^ GetStrTime() {

return System::String::Format("{0, 2:D2}:{1, 2:D2}", _time_secs / 60, _time_secs % 60);

}

int GetIntTime() {

return _time_secs;

}

System::String^ GetStrTOD() {

if (_time_secs >= TIMEOFDAY_LEN) {

if (_time_secs >= TIMEOFDAY_LEN * 2) return "noon";

else return "day";

}

return "night";

}

protected:

int _time_secs;

};