-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathRenderableElement.java
More file actions
101 lines (85 loc) · 3.84 KB
/
RenderableElement.java
File metadata and controls
101 lines (85 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2020 The Terasology Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.destinationsol.rendering;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import org.destinationsol.common.SolMath;
import org.destinationsol.game.drawables.DrawableLevel;
import org.destinationsol.size.components.Size;
import org.terasology.gestalt.module.sandbox.API;
/**
* Contains a {@link TextureAtlas.AtlasRegion} (an image), along with information about how it should be drawn.
*/
@API
public class RenderableElement {
/** Represents the image of this element. */
public TextureAtlas.AtlasRegion texture;
/** The width of the texture when drawn. */
private float width;
/** The height of the texture when drawn. */
private float height;
/** Represents the depth at which this element renders, as well as its logical grouping. */
public DrawableLevel drawableLevel;
/** Represents the spatial offset of this element from the entity that it is associated with. */
public Vector2 relativePosition;
/** Represents the rotational offset of this element from the entity that it is associated with. */
public float relativeAngle;
/** The tint that the texture should be given. */
public Color tint;
/**
* The amount that the sprite should be moved to line up accurately with the mesh. This should be scaled according
* to the {@link Size}. This is different from the relativePosition, because this is a practical adjustment to align
* the mesh with the sprite, as opposed to moving the sprite relative to the base entity. To draw the sprite
* accurately, it needs to be drawn from the bottom-left of the actual image, not the bottom left of the .png file,
* so this contains the information for calculating the actual start of the sprite.
* <p>
* Modification of this can create mesh misalignments, so only change this if you know what you're doing.
*/
public Vector2 graphicsOffset;
//TODO this should be automatically called when the Size component is changed, e.g. the entity shrinks or grows
/**
* Resizes the renderable element to the given size. The larger dimension of the texture is set to the size, and the
* smaller one is scaled down proportionally.
*/
public void setSize(float size) {
size /= drawableLevel.depth; // Scales the texture size for objects that are in the background
float dimensionsRatio = (float) texture.getRegionWidth() / texture.getRegionHeight();
if (dimensionsRatio > 1) {
width = size;
height = size / dimensionsRatio;
} else {
width = size / dimensionsRatio;
height = size;
}
}
public void copy(RenderableElement other) {
this.texture = new TextureAtlas.AtlasRegion(other.texture);
this.drawableLevel = other.drawableLevel;
this.relativePosition = other.relativePosition.cpy();
this.relativeAngle = other.relativeAngle;
this.width = other.getWidth();
this.height = other.getHeight();
this.tint = other.tint.cpy();
this.graphicsOffset = other.graphicsOffset.cpy();
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}