"use strict";
this.name = "HoopyCasinoShip";
this.author = "phkb";
this.copyright = "2026 phkb";
this.license = "CC BY-NC-SA 4.0";

this._max = 6000; // maximum value for any of the components
this._inc = 250; // how fast the color changes
this._rgb = [0, 0, 0];
this._change = [0, 0, 0];

this.shipSpawned = function () {
	if (this.ship.dataKey != "casinoship") return;
	var check1 = Math.random();
	var range = this._max / this._inc;
	if (check1 > 0.67) {
		this._rgb = [0, Math.floor(Math.random() * range) * this._inc, Math.floor(Math.random() * range) * this._inc];
		this._change = [0, this._inc, this._inc * -1];
	} else if (check1 > 0.33) {
		this._rgb = [Math.floor(Math.random() * range) * this._inc, 0, Math.floor(Math.random() * range) * this._inc];
		this._change = [this._inc * -1, 0, this._inc];
	} else {
		this._rgb = [Math.floor(Math.random() * range) * this._inc, Math.floor(Math.random() * range) * this._inc, 0];
		this._change = [this._inc, this._inc * -1, 0];
	}
	this._timer = new Timer(this, this.$changeColor.bind(this), 0.25, 0.25);
}

this.shipDied = function () {
	if (this._timer) this._timer.stop();
}

this.shipRemoved = function () {
	if (this._timer) this._timer.stop();
}

this.$changeColor = function $changeColor() {
	var that = $changeColor;
	var _rgb = (that._rgb = that._rgb || this._rgb);
	var _change = (that._change = that._change || this._change);
	var _max = (that._max = that._max || this._max);
	var _inc = (that._inc = that._inc || this._inc);

	if (!this.ship || this.ship.isValid == false) {
		this._timer.stop();
		return;
	}
	_rgb[0] += _change[0];
	if (_rgb[0] < 0) { _rgb[0] = 0; _change[0] = 0; _change[1] = _inc; }
	if (_rgb[0] > _max) { _rgb[0] = _max; _change[0] = _inc * -1; }
	_rgb[1] += _change[1];
	if (_rgb[1] < 0) { _rgb[1] = 0; _change[1] = 0; _change[2] = _inc; }
	if (_rgb[1] > _max) { _rgb[1] = _max; _change[1] = _inc * -1; }
	_rgb[2] += _change[2];
	if (_rgb[2] < 0) { _rgb[2] = 0; _change[2] = 0; _change[0] = _inc; }
	if (_rgb[2] > _max) { _rgb[2] = _max; _change[2] = _inc * -1; }

	this.ship.setMaterials({
		"hoopyCasino_diffuse.png": {
			diffuse_map: "hoopyCasino_diffuse.png",
			normal_map: "hoopyCasino_normal.png",
			specular_map: "hoopyCasino_specular.png",
			specular_color: [0.1, 0.1, 0.1],
			emission_map: "hoopyCasino_effects.png",
			gloss: 0.7,
			emission_modulate_color: {
				red: _rgb[0],
				green: _rgb[1],
				blue: _rgb[2]
			}
		},
		"hoopyCasino_diffuse2.png": {
			diffuse_map: "hoopyCasino_diffuse2.png",
			normal_map: "hoopyCasino_normal.png",
			specular_map: "hoopyCasino_specular.png",
			specular_color: [0.1, 0.1, 0.1],
			emission_map: "hoopyCasino_effects.png",
			gloss: 0.7,
			emission_modulate_color: {
				red: _rgb[0],
				green: _rgb[1],
				blue: _rgb[2]
			}
		}
	});
}