-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraficas.cs
145 lines (116 loc) · 4.83 KB
/
Graficas.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
namespace Horus
{
class Graficas
{
public Image Sprite;
public Int32 LastX = 0;
public Int32 LastY = 0;
public Int32 TotalFrames = 1;
public Int32 ActualFrame = 1;
public Boolean Visible = true;
public Boolean Loop = true;
public Boolean Smooth = true;
public void Move(Graphics g, Int32 X, Int32 Y, Int32 width = 0, Int32 height = 0)
{
if (LastX == 0)
LastX = X;
if (LastY == 0)
LastY = Y;
if (Visible)
{
FrameDimension dimension = new FrameDimension(Sprite.FrameDimensionsList[0]);
TotalFrames = Sprite.GetFrameCount(dimension);
if (TotalFrames > 1)
{
Sprite.SelectActiveFrame(dimension, ActualFrame - 1);
if (ActualFrame >= TotalFrames - 1 && Loop)
ActualFrame = 0;
else
if (ActualFrame < TotalFrames)
ActualFrame++;
}
if (height > 0)
Sprite = Resize(Sprite, Sprite.Width, height, false);
if (width > 0)
Sprite = Resize(Sprite, width, Sprite.Height, false);
if (Smooth)
{
if (Distance(X, Y, LastX, LastY) > 2)
{
// SI LA DISTANCIA ESTA EN X
if (Math.Abs((LastX - X)) > Math.Abs((LastY - Y)))
{
float m = 0;
if ((LastX - X) != 0)
{
m = (float)(LastY - Y) / (float)(LastX - X);
float b = Y - m * X;
if (X > LastX)
X = LastX + Convert.ToInt32(Math.Round(Distance(X, Y, LastX, LastY) / 1.2));
else
X = LastX - Convert.ToInt32(Math.Round(Distance(X, Y, LastX, LastY) / 1.2));
Y = Convert.ToInt32((m * X) + b);
}
g.DrawImage(Sprite, new Point(X, Y));
}
else
{
float m = 0;
if ((LastY - Y) != 0)
{
m = (float)(LastX - X) / (float)(LastY - Y);
float b = X - m * Y;
if (Y > LastY)
Y = LastY + Convert.ToInt32(Math.Round(Distance(X, Y, LastX, LastY) / 1.2));
else
Y = LastY - Convert.ToInt32(Math.Round(Distance(X, Y, LastX, LastY) / 1.2));
X = Convert.ToInt32((m * Y) + b);
}
g.DrawImage(Sprite, new Point(X, Y));
}
}
else
{
g.DrawImage(Sprite, new Point(X, Y));
}
}
else
{
g.DrawImage(Sprite, new Point(X, Y));
}
}
LastX = X;
LastY = Y;
}
private Double Distance(Int32 X1, Int32 Y1, Int32 X2, Int32 Y2)
{
Double Diff = (X2 - X1) + (Y2 - Y1);
Diff = Math.Sqrt(Math.Pow(Diff, 2));
return Diff;
}
public Image SpriteFromFile(String Archivo)
{
return (Bitmap)Image.FromFile(Archivo, true);
}
private Image Resize(Image image, int newWidth, int maxHeight, bool onlyResizeIfWider)
{
if (onlyResizeIfWider && image.Width <= newWidth) newWidth = image.Width;
var newHeight = image.Height;
newHeight = maxHeight;
var res = new Bitmap(newWidth, newHeight);
using (var graphic = Graphics.FromImage(res))
{
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, newWidth, newHeight);
}
return res;
}
}
}