Class: Tile

SG2D.Tile(properties) → {SG2D.Tile}

Тайл со спрайтами. Базовый класс: SGModel

Constructor

new Tile(properties) → {SG2D.Tile}

Конструктор

Parameters:
Name Type Description
properties object
Properties
Name Type Attributes Default Description
texture object <optional>

Задаётся для основного спрайта

angle object <optional>
0

Задаётся для основного спрайта

anchor object <optional>
0.5

Задаётся для основного спрайта

scale object <optional>
1

Задаётся для основного спрайта

alpha object <optional>
1

Задаётся для основного спрайта

visible object <optional>
true

Задаётся для основного спрайта

zindex object <optional>
0

Задаётся для основного спрайта

layer object <optional>

Задаётся для основного спрайта

Source:
Returns:
Type
SG2D.Tile

Members

(static) alpha :number

Прозрачность основного спрайта.

Type:
  • number
Source:

(static) anchor :number

Смещение основного спрайта.

Type:
  • number
Source:

(static) angle :number

Угол поворота основного спрайта.

Type:
  • number
Source:

(static) animation :SG2D.Tile.AnimationConfig

Описание анимации для основного спрайта

Type:
Source:
Example
// Пример описания анимации для основного спрайта
export class Medikit extends SG2D.Tile {
	static texture = "objects/medikit100_1";
	static layer = "bodies";
	static animation = {
		count: 8,
		sleep: 3,
		running: false,
		basetexture: "objects/medikit100_",
		onComplete: function(sprite) {
			sprite.visible = true; // После выполнения анимации оставляем её видимой
		},
		loop: false
	};
	
	initialize(...args) {
		super.initialize(...args);
		this.scaleDir = 1;
	}
	
	iterateAnimations(...args) {
		this.set("angle", this.properties.angle + 1);
		this.set("scale", this.properties.scale +0.002 * this.scaleDir);
		if (this.properties.scale <= 0.6) this.scaleDir = 1;
		if (this.properties.scale >= 0.8) this.scaleDir = -1;
		
		if (! this.sprite.animation.running && Math.random() < 0.01) {
			this.startAnimation();
		}
		
		super.iterateAnimations(...args);
	}
}

(static) FLAG_ONLY_OPTIONS_SPRITE

Флаг для игнорирования спрайтом сеттера без указания спрайта в options.<sprite|sprites>. Флаг актуален для тайлов состоящих из нескольких спрайтов. Например, для спрайта this.sprites.platform сеттер this.set("angle", 125) не применится, а this.set("angle", 125, { sprite: this.sprites.platform }) применится!

Source:

(static) isBody :boolean

Type:
  • boolean
Source:

(static, readonly) isTile :boolean

Type:
  • boolean
Source:

(static) layer :string

Слой, в котором идёт отрисовка основного спрайта. Если равно undefined, используется слой по умолчанию (PIXI.Container).

Type:
  • string
Source:

(static) noDraw :boolean

Type:
  • boolean
Source:

(static) scale :number

Масштабирование основного спрайта.

Type:
  • number
Source:

(static) sprites :Object.<string, SG2D.Tile.SpriteConfig>

Описание спрайтов и анимаций в тайле в виде дерева.

Type:
Source:
Example
// Базовый класс для тайлов с lifeband-полоской
export default class ObjectBaseLifeBand extends SG2D.TileBody {
	static layer = "bodies";
	static sprites = {
		lifeband_base: {
			texture: "ui/lifeband_base",
			anchor: { x: 0.5, y: -5 },
			zindex: 10,
			angle: 90,
			layer: "animations",
			setter_flag: SG2D.Tile.FLAG_ONLY_OPTIONS_SPRITE
		},
		lifeband_value: {
			textures: { friend: "ui/lifeband_green", enemy: "ui/lifeband_red" },
			texture: "ui/lifeband_green",
			scale: { x: 1, y: 1 },
			anchor: { x: 0.5, y: -5 },
			zindex: 11,
			angle: 90,
			layer: "animations",
			setter_flag: SG2D.Tile.FLAG_ONLY_OPTIONS_SPRITE
		}
	}
	//...
}

// Базовый класс для игроков
export default class Player extends ObjectBaseLifeBand {
	static sprites = SG2D.Model.defaults({
		platform: {
			texture: "objects/player-platform",
			zindex: 0,
			sprites: {
				turret: {
					texture: "objects/player-turret",
					anchor: { x: 0.15, y: 0.5 },
					zindex: 2,
					sprites: {
						smoke_shot: {
							texture: "objects/player-smoke-shot_1",
							layer: "animations",
							anchor: { x: -3.4, y: 0.5 },
							zindex: 1,
							visible: false,
							animation: {
								count: 6,
								sleep: 3,
								running: false,
								basetexture: "objects/player-smoke-shot_"
							},
							setter_flag: SG2D.Tile.FLAG_ONLY_OPTIONS_SPRITE
						}
					}
				}
			}
		},
		accelerator: {
			texture: "animations/accelerator_1",
			layer: "animations",
			anchor: { x: 2.7, y: 0.45 },
			zindex: 0,
			visible: false,
			animation: {
				count: 3,
				sleep: 3,
				running: false,
				basetexture: "animations/accelerator_"
			}
		},
		track_left: {
			texture: "objects/player-track_1",
			anchor: { x: -4, y: 2.7 },
			zindex: 1,
			animation: {
				count: 8,
				sleep: 2,
				running: false,
				basetexture: "objects/player-track_",
				loop: true
			}
		},
		track_right: {
			texture: "objects/player-track_1",
			anchor: { x: -4, y: -1.6 },
			zindex: 1,
			animation: {
				count: 8,
				sleep: 2,
				running: false,
				basetexture: "objects/player-track_",
				loop: true
			}
		}
	}, ObjectBaseLifeBand.sprites);
	//...
 }

(static) texture :string

Текстура основного спрайта.

Type:
  • string
Source:
Example
// Пример описания простейшего тайла, где будет только один спрайт (основной), задействована только одна текстура и не будет анимаций
export class BlockSteel extends SG2D.TileBody {
	static texture = "elements/block-steel";
	static layer = "bodies";
	static zindex = 20;
}

// Пример описание тайла, где будет один спрайт (основной), но текстура задаётся при инициализации
export class Tree extends SG2D.Tile {
	static texture = "elements/trees/tree_";
	static layer = "trees";
	static zindex = 20;
	initialize(...args) {
		super.initialize(...args);
		let n = Math.floor(1 + Math.random() * 41);
		this.set("texture", Tree.texture + (n<10?"0":"") + n);
	}
}

(static) visible :boolean

Видимость основного спрайта.

Type:
  • boolean
Source:

(static) zindex :number

Основной z-index добавляется к z-index спрайтов, кроме спрайта с именем = "main"

Type:
  • number
Source:

Methods

breakAnimation(name_or_spriteopt)

Прервать анимацию

Parameters:
Name Type Attributes Default Description
name_or_sprite string | SG2D.Sprite <optional>
void 0

Если не задано, то берётся спрайт по умолчанию (основной)

Source:

checkTexture(sTexture) → {string}

Parameters:
Name Type Description
sTexture string
Source:
Returns:
Type
string

drawUndraw(spriteopt)

Для движущихся спрайтов проверяет, требуется ли рисование при попадании в камеру.

Parameters:
Name Type Attributes Description
sprite object <optional>
Source:

isInCamera() → {Boolean}

Проверить находится ли тайл в камере

Source:
Returns:
Type
Boolean

(protected) iterateAnimations()

Source:

(protected) removeSprites()

Source:

resumeAnimation(name_or_spriteopt, optionsopt)

Возобновить анимацию

Parameters:
Name Type Attributes Default Description
name_or_sprite string | SG2D.Sprite <optional>
void 0

Если не задано, то берётся спрайт по умолчанию (основной)

options object <optional>
void 0
Properties
Name Type Attributes Default Description
visible boolean <optional>
void 0

Если значение задано, то спрайт отображается/скрывается

Source:

sound(code)

Проиграть звук к тайлу, учитывается 2D-окружение и расстояние

Parameters:
Name Type Description
code string
Source:

startAnimation(name_or_spriteopt)

Начать анимацию

Parameters:
Name Type Attributes Default Description
name_or_sprite string | SG2D.Sprite <optional>
void 0

Если не задано, то берётся спрайт по умолчанию (основной)

Source:

stepAnimation(name_or_spriteopt, countopt)

Одна итерация анимации

Parameters:
Name Type Attributes Default Description
name_or_sprite string | SG2D.Sprite <optional>
void 0

Если не задано, то берётся спрайт по умолчанию (основной)

count number <optional>
1
Source:

stopAnimation(name_or_spriteopt, optionsopt)

Приостановить анимацию

Parameters:
Name Type Attributes Default Description
name_or_sprite string | SG2D.Sprite <optional>
void 0

Если не задано, то берётся спрайт по умолчанию (основной)

options object <optional>
void 0
Properties
Name Type Attributes Default Description
visible boolean <optional>
void 0

Если значение задано, то спрайт отображается/скрывается

Source:

updateSpriteTexture() → {PIXI.Texture}

Source:
Returns:
Type
PIXI.Texture

Type Definitions

AnimationConfig

Конфиг анимации для спрайта.

Type:
  • object
Properties:
Name Type Attributes Default Description
basetexture string
start number <optional>
1
count number
sleep number <optional>
1
running boolean <optional>
false
loop boolean <optional>
false
onComplete function <optional>
void 0
Source:

SpriteConfig

Конфиг спрайта в тайле.

Type:
  • object
Properties:
Name Type Attributes Default Description
texture string
anchor number | object <optional>
0.5
angle number <optional>
0

В градусах

scale number | object <optional>
1
zindex number <optional>
0
visible boolean <optional>
true
layer string <optional>

Код слоя

animation SG2D.Tile.AnimationConfig <optional>
setter_flag number <optional>
0

Флаг. Если задано SG2D.Tile.FLAG_ONLY_OPTIONS_SPRITE, то нужно передавать в options код или сам объект спрайта, например: this.set("angle", a, { sprite: this.sprites.smoke_shot });

Source: