source: lost-perception/main/Model.java@ 57674f3

Last change on this file since 57674f3 was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1package main;
2
3import java.awt.Graphics;
4import utils.DynamicImage;
5import utils.Utils;
6import java.awt.Color;
7import gamegui.Animation;
8
9public class Model
10{
11 Direction direction;
12 Action action;
13 private Animation[] anims;
14
15 public Model(final String name, final Color bgColor, final Color shadowColor, final int[] numFramesArr) {
16 this.anims = new Animation[48];
17 this.direction = Direction.North;
18 this.action = Action.Walking;
19 int numFrames = numFramesArr[0];
20 Direction[] values;
21 for (int length = (values = Direction.values()).length, i = 0; i < length; ++i) {
22 final Direction dir = values[i];
23 final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
24 for (int x = 0; x < numFrames; ++x) {
25 curAnim.addFrame(new DynamicImage("creatures/" + name + "/walking/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
26 }
27 curAnim.setWidth(96);
28 curAnim.setHeight(96);
29 this.addAnimation(dir, Action.Walking, curAnim);
30 }
31 numFrames = numFramesArr[1];
32 Direction[] values2;
33 for (int length2 = (values2 = Direction.values()).length, j = 0; j < length2; ++j) {
34 final Direction dir = values2[j];
35 final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
36 for (int x = 0; x < numFrames; ++x) {
37 curAnim.addFrame(new DynamicImage("creatures/" + name + "/standing/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
38 }
39 curAnim.setWidth(96);
40 curAnim.setHeight(96);
41 this.addAnimation(dir, Action.Standing, curAnim);
42 }
43 numFrames = numFramesArr[2];
44 Direction[] values3;
45 for (int length3 = (values3 = Direction.values()).length, k = 0; k < length3; ++k) {
46 final Direction dir = values3[k];
47 final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
48 for (int x = 0; x < numFrames; ++x) {
49 curAnim.addFrame(new DynamicImage("creatures/" + name + "/attacking/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
50 }
51 curAnim.setWidth(96);
52 curAnim.setHeight(96);
53 this.addAnimation(dir, Action.Attacking, curAnim);
54 }
55 numFrames = numFramesArr[3];
56 Direction[] values4;
57 for (int length4 = (values4 = Direction.values()).length, l = 0; l < length4; ++l) {
58 final Direction dir = values4[l];
59 final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
60 for (int x = 0; x < numFrames; ++x) {
61 curAnim.addFrame(new DynamicImage("creatures/" + name + "/beenhit/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
62 }
63 curAnim.setWidth(96);
64 curAnim.setHeight(96);
65 this.addAnimation(dir, Action.BeenHit, curAnim);
66 }
67 numFrames = numFramesArr[4];
68 Direction[] values5;
69 for (int length5 = (values5 = Direction.values()).length, n = 0; n < length5; ++n) {
70 final Direction dir = values5[n];
71 final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, false);
72 for (int x = 0; x < numFrames; ++x) {
73 curAnim.addFrame(new DynamicImage("creatures/" + name + "/dying/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
74 }
75 curAnim.setWidth(128);
76 curAnim.setHeight(128);
77 this.addAnimation(dir, Action.Dying, curAnim);
78 }
79 }
80
81 public Model(final Model copy) {
82 this.direction = copy.direction;
83 this.action = copy.action;
84 this.anims = new Animation[48];
85 for (int x = 0; x < this.anims.length; ++x) {
86 if (copy.anims[x] != null) {
87 this.anims[x] = new Animation(copy.anims[x]);
88 }
89 }
90 }
91
92 public void addAnimation(final Direction dir, final Action action, final Animation anim) {
93 this.anims[this.getIndex(dir, action)] = anim;
94 }
95
96 public void draw(final Graphics g, final int x, final int y) {
97 if (this.anims[this.getIndex(this.direction, this.action)] != null) {
98 this.anims[this.getIndex(this.direction, this.action)].draw(g, x, y);
99 }
100 }
101
102 public int getHeight() {
103 return this.getAnimation(this.direction, this.action).getHeight();
104 }
105
106 public int getWidth() {
107 return this.getAnimation(this.direction, this.action).getWidth();
108 }
109
110 private int getIndex(final Direction dir, final Action action) {
111 int pos = 0;
112 switch (action) {
113 case Running: {
114 pos = 8;
115 break;
116 }
117 case Standing: {
118 pos = 16;
119 break;
120 }
121 case Attacking: {
122 pos = 24;
123 break;
124 }
125 case BeenHit: {
126 pos = 32;
127 break;
128 }
129 case Dying: {
130 pos = 40;
131 break;
132 }
133 }
134 switch (dir) {
135 case NorthEast: {
136 ++pos;
137 break;
138 }
139 case East: {
140 pos += 2;
141 break;
142 }
143 case SouthEast: {
144 pos += 3;
145 break;
146 }
147 case South: {
148 pos += 4;
149 break;
150 }
151 case SouthWest: {
152 pos += 5;
153 break;
154 }
155 case West: {
156 pos += 6;
157 break;
158 }
159 case NorthWest: {
160 pos += 7;
161 break;
162 }
163 }
164 return pos;
165 }
166
167 public Animation getAnimation(final Direction dir, final Action action) {
168 return this.anims[this.getIndex(dir, action)];
169 }
170
171 public Animation getCurrentAnimation() {
172 return this.getAnimation(this.direction, this.action);
173 }
174}
Note: See TracBrowser for help on using the repository browser.