1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | using Microsoft.Xna.Framework; |
---|
9 | using Microsoft.Xna.Framework.Graphics; |
---|
10 | |
---|
11 | public class JRPG : PhysicsGame |
---|
12 | { |
---|
13 | public static JRPG Game { get; set; } |
---|
14 | |
---|
15 | public static BattleView BattleView { get; set; } |
---|
16 | |
---|
17 | public Player Player { get; set; } |
---|
18 | |
---|
19 | public SpriteFont MainFont { get; set; } |
---|
20 | |
---|
21 | bool UseShaders = false; |
---|
22 | float WarpAmount = 0.0f; |
---|
23 | double FadeSpeed = 0.75; |
---|
24 | Effect Shader; |
---|
25 | public Effect Wave; |
---|
26 | SpriteBatch spriteBatch; |
---|
27 | RenderTarget2D renderTarget; |
---|
28 | |
---|
29 | Widget Fade { get; set; } |
---|
30 | |
---|
31 | public override void Begin() |
---|
32 | { |
---|
33 | SoundEffect.MasterVolume = 0.4; |
---|
34 | |
---|
35 | MainFont = Content.Load<SpriteFont>("MainFont"); |
---|
36 | |
---|
37 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
38 | Shader = Content.Load<Effect>("Shader"); |
---|
39 | Wave = Content.Load<Effect>("Wave"); |
---|
40 | |
---|
41 | Game = this; |
---|
42 | |
---|
43 | Window.Width = GameValues.SCREEN_WIDTH; |
---|
44 | Window.Height = GameValues.SCREEN_HEIGHT; |
---|
45 | |
---|
46 | SmoothTextures = false; |
---|
47 | IsMouseVisible = true; |
---|
48 | |
---|
49 | OverworldView.LoadOverworldView("Test"); |
---|
50 | Level.Size = OverworldView.CurrentMap.Size; |
---|
51 | Level.Background.Color = Jypeli.Color.FromHexCode("000000"); |
---|
52 | |
---|
53 | LoadOWPlayer(new Vector(2,2)); |
---|
54 | LoadOWControlsKeyboard(); |
---|
55 | LoadOWControlsXboxController(); |
---|
56 | |
---|
57 | CheckWhereCanMove(); |
---|
58 | |
---|
59 | Camera.Follow(Player); |
---|
60 | Camera.ZoomFactor = 1.0f; |
---|
61 | Camera.StayInLevel = true; |
---|
62 | |
---|
63 | /* |
---|
64 | DialogBox DB = new DialogBox(); |
---|
65 | DB.SetTextBase(MainFont, TextAlignment.Left); |
---|
66 | DB.WriteRow("what"); |
---|
67 | DB.WriteRow(" "); |
---|
68 | DB.WriteRow("XDDDDDDDDDDD w0ah 20mui9809809809"); |
---|
69 | DB.WriteRow(" "); |
---|
70 | DB.DrawDialogBox();*/ |
---|
71 | } |
---|
72 | |
---|
73 | public void LoadOWPlayer(Vector PositionInTiles) |
---|
74 | { |
---|
75 | Player = new Player(new Vector(28, 40)); |
---|
76 | Player.Position = PositionInTiles * 40 + new Vector(20, 30); |
---|
77 | Player.PositionInTiles = PositionInTiles; |
---|
78 | Player.Color = Jypeli.Color.Blue; |
---|
79 | Add(Player); |
---|
80 | } |
---|
81 | |
---|
82 | #region Controls/Movement |
---|
83 | |
---|
84 | public void LoadOWControlsKeyboard() |
---|
85 | { |
---|
86 | Keyboard.Listen(Key.Delete, ButtonState.Pressed, delegate |
---|
87 | { |
---|
88 | renderTarget = new RenderTarget2D(GraphicsDevice, |
---|
89 | GraphicsDevice.PresentationParameters.BackBufferWidth, |
---|
90 | GraphicsDevice.PresentationParameters.BackBufferHeight); |
---|
91 | }, null); |
---|
92 | Keyboard.Listen(Key.Insert, ButtonState.Pressed, delegate { |
---|
93 | UseShaders = !UseShaders; }, null); |
---|
94 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
95 | |
---|
96 | Keyboard.Listen(Key.Left, ButtonState.Down, delegate { if(Keyboard.GetKeyState(Key.LeftShift) != ButtonState.Down) MoveLeft(GameValues.TILE_SIZE); }, null); |
---|
97 | Keyboard.Listen(Key.Right, ButtonState.Down, delegate { if (Keyboard.GetKeyState(Key.LeftShift) != ButtonState.Down) MoveRight(GameValues.TILE_SIZE); }, null); |
---|
98 | Keyboard.Listen(Key.Up, ButtonState.Down, delegate { if (Keyboard.GetKeyState(Key.LeftShift) != ButtonState.Down) MoveUp(GameValues.TILE_SIZE); }, null); |
---|
99 | Keyboard.Listen(Key.Down, ButtonState.Down, delegate { if (Keyboard.GetKeyState(Key.LeftShift) != ButtonState.Down) MoveDown(GameValues.TILE_SIZE); }, null); |
---|
100 | |
---|
101 | Keyboard.Listen(Key.Left, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Left; }, null); |
---|
102 | Keyboard.Listen(Key.Right, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Right; }, null); |
---|
103 | Keyboard.Listen(Key.Up, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Up; }, null); |
---|
104 | Keyboard.Listen(Key.Down, ButtonState.Pressed, delegate {Movement.FacingDir = Movement.Direction.Down; }, null); |
---|
105 | |
---|
106 | |
---|
107 | Keyboard.Listen(Key.PageDown, ButtonState.Pressed, FadeOut, null); |
---|
108 | Keyboard.Listen(Key.PageUp, ButtonState.Pressed, FadeIn, null); |
---|
109 | } |
---|
110 | public void LoadOWControlsXboxController() |
---|
111 | { |
---|
112 | ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, delegate { if (ControllerOne.GetButtonState(Button.LeftShoulder) != ButtonState.Down && ControllerOne.GetButtonState(Button.RightShoulder) != ButtonState.Down) MoveLeft(GameValues.TILE_SIZE); }, null); |
---|
113 | ControllerOne.Listen(Button.DPadRight, ButtonState.Down, delegate { if (ControllerOne.GetButtonState(Button.LeftShoulder) != ButtonState.Down && ControllerOne.GetButtonState(Button.RightShoulder) != ButtonState.Down) MoveRight(GameValues.TILE_SIZE); }, null); |
---|
114 | ControllerOne.Listen(Button.DPadUp, ButtonState.Down, delegate { if (ControllerOne.GetButtonState(Button.LeftShoulder) != ButtonState.Down && ControllerOne.GetButtonState(Button.RightShoulder) != ButtonState.Down) MoveUp(GameValues.TILE_SIZE); }, null); |
---|
115 | ControllerOne.Listen(Button.DPadDown, ButtonState.Down, delegate { if (ControllerOne.GetButtonState(Button.LeftShoulder) != ButtonState.Down && ControllerOne.GetButtonState(Button.RightShoulder) != ButtonState.Down) MoveDown(GameValues.TILE_SIZE); }, null); |
---|
116 | |
---|
117 | ControllerOne.Listen(Button.DPadLeft, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Left; }, null); |
---|
118 | ControllerOne.Listen(Button.DPadRight, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Right; }, null); |
---|
119 | ControllerOne.Listen(Button.DPadUp, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Up; }, null); |
---|
120 | ControllerOne.Listen(Button.DPadDown, ButtonState.Pressed, delegate { Movement.FacingDir = Movement.Direction.Down; }, null); |
---|
121 | } |
---|
122 | |
---|
123 | void MoveLeft(double Distance) |
---|
124 | { |
---|
125 | |
---|
126 | if (Keyboard.GetKeyState(Key.Right) != ButtonState.Down && Movement.FacingDir == Movement.Direction.Left |
---|
127 | || CheckIfAnotherArrowKeyIsDown(Key.Left, Key.Right) && Movement.FacingDir == Movement.Direction.Left |
---|
128 | || !CheckIfAnotherArrowKeyIsDown(Key.Left) && Keyboard.GetKeyState(Key.Left) == ButtonState.Down) |
---|
129 | { |
---|
130 | if (Movement.CanMoveLeft && Keyboard.GetKeyState(Key.Left) == ButtonState.Down) |
---|
131 | { |
---|
132 | Player.Move(new Vector(-Distance, 0), GameValues.MOVE_SPEED); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | void MoveRight(double Distance) |
---|
137 | { |
---|
138 | if (Keyboard.GetKeyState(Key.Left) != ButtonState.Down && Movement.FacingDir == Movement.Direction.Right |
---|
139 | || CheckIfAnotherArrowKeyIsDown(Key.Right, Key.Left) && Movement.FacingDir == Movement.Direction.Right |
---|
140 | || !CheckIfAnotherArrowKeyIsDown(Key.Right) && Keyboard.GetKeyState(Key.Right) == ButtonState.Down) |
---|
141 | { |
---|
142 | if (Movement.CanMoveRight) |
---|
143 | { |
---|
144 | Player.Move(new Vector(Distance, 0), GameValues.MOVE_SPEED); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | void MoveUp(double Distance) |
---|
149 | { |
---|
150 | if (Keyboard.GetKeyState(Key.Down) != ButtonState.Down && Movement.FacingDir == Movement.Direction.Up |
---|
151 | || CheckIfAnotherArrowKeyIsDown(Key.Up, Key.Down) && Movement.FacingDir == Movement.Direction.Up |
---|
152 | || !CheckIfAnotherArrowKeyIsDown(Key.Up) && Keyboard.GetKeyState(Key.Up) == ButtonState.Down) |
---|
153 | { |
---|
154 | if (Movement.CanMoveUp) |
---|
155 | { |
---|
156 | Player.Move(new Vector(0, Distance), GameValues.MOVE_SPEED); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | void MoveDown(double Distance) |
---|
161 | { |
---|
162 | if (Keyboard.GetKeyState(Key.Up) != ButtonState.Down && Movement.FacingDir == Movement.Direction.Down |
---|
163 | || CheckIfAnotherArrowKeyIsDown(Key.Down, Key.Up) && Movement.FacingDir == Movement.Direction.Down |
---|
164 | || !CheckIfAnotherArrowKeyIsDown(Key.Down) && Keyboard.GetKeyState(Key.Down) == ButtonState.Down) |
---|
165 | { |
---|
166 | if (Movement.CanMoveDown) |
---|
167 | { |
---|
168 | Player.Move(new Vector(0, -Distance), GameValues.MOVE_SPEED); |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | public void CheckWhereCanMove() |
---|
174 | { |
---|
175 | Tile LeftTile = (Tile)OverworldView.CurrentCollisionMap.CollisionMapArray.GetValue((int)Math.Max(Player.PositionInTiles.X - 1,0),(int)(Player.PositionInTiles.Y)); |
---|
176 | Tile RightTile = (Tile)OverworldView.CurrentCollisionMap.CollisionMapArray.GetValue((int)Math.Min(Player.PositionInTiles.X + 1, OverworldView.CurrentCollisionMap.CollisionMapArray.GetLength(0) - 1) , (int)Player.PositionInTiles.Y); |
---|
177 | Tile TopTile = (Tile)OverworldView.CurrentCollisionMap.CollisionMapArray.GetValue((int)Player.PositionInTiles.X, (int)Math.Min(Player.PositionInTiles.Y + 1, OverworldView.CurrentCollisionMap.CollisionMapArray.GetLength(1) - 1)); |
---|
178 | Tile BottomTile = (Tile)OverworldView.CurrentCollisionMap.CollisionMapArray.GetValue((int)Player.PositionInTiles.X, (int)Math.Max(Player.PositionInTiles.Y - 1, 0)); |
---|
179 | |
---|
180 | if (LeftTile.IsWalkable) Movement.CanMoveLeft = true; |
---|
181 | else Movement.CanMoveLeft = false; |
---|
182 | if (RightTile.IsWalkable) Movement.CanMoveRight = true; |
---|
183 | else Movement.CanMoveRight = false; |
---|
184 | if (TopTile.IsWalkable) Movement.CanMoveUp = true; |
---|
185 | else Movement.CanMoveUp = false; |
---|
186 | if (BottomTile.IsWalkable) Movement.CanMoveDown = true; |
---|
187 | else Movement.CanMoveDown = false; |
---|
188 | } |
---|
189 | |
---|
190 | bool CheckIfAnotherArrowKeyIsDown(Key Key) |
---|
191 | { |
---|
192 | if (Keyboard.GetKeyState(Key.Left) == ButtonState.Down) |
---|
193 | { |
---|
194 | if (Key != Key.Left) return true; |
---|
195 | } |
---|
196 | if (Keyboard.GetKeyState(Key.Right) == ButtonState.Down) |
---|
197 | { |
---|
198 | if (Key != Key.Right) return true; |
---|
199 | } |
---|
200 | if (Keyboard.GetKeyState(Key.Up) == ButtonState.Down) |
---|
201 | { |
---|
202 | if (Key != Key.Up) return true; |
---|
203 | } |
---|
204 | if (Keyboard.GetKeyState(Key.Down) == ButtonState.Down) |
---|
205 | { |
---|
206 | if (Key != Key.Down) return true; |
---|
207 | } |
---|
208 | |
---|
209 | return false; |
---|
210 | } |
---|
211 | bool CheckIfAnotherArrowKeyIsDown(Key Key, Key Key2) |
---|
212 | { |
---|
213 | if (Keyboard.GetKeyState(Key.Left) == ButtonState.Down) |
---|
214 | { |
---|
215 | if (Key != Key.Left && Key2 != Key.Left) return true; |
---|
216 | } |
---|
217 | if (Keyboard.GetKeyState(Key.Right) == ButtonState.Down) |
---|
218 | { |
---|
219 | if (Key != Key.Right && Key2 != Key.Right) return true; |
---|
220 | } |
---|
221 | if (Keyboard.GetKeyState(Key.Up) == ButtonState.Down) |
---|
222 | { |
---|
223 | if (Key != Key.Up && Key2 != Key.Up) return true; |
---|
224 | } |
---|
225 | if (Keyboard.GetKeyState(Key.Down) == ButtonState.Down) |
---|
226 | { |
---|
227 | if (Key != Key.Down && Key2 != Key.Down) return true; |
---|
228 | } |
---|
229 | |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | #endregion |
---|
234 | |
---|
235 | public void FadeOut() |
---|
236 | { |
---|
237 | if (Fade != null && Fade.IsAddedToGame) Fade.Destroy(); |
---|
238 | |
---|
239 | Fade = new Widget(Screen.WidthSafe, Screen.HeightSafe); |
---|
240 | Fade.Color = Jypeli.Color.Transparent; |
---|
241 | Add(Fade); |
---|
242 | Fade.FadeColorTo(Jypeli.Color.Black, FadeSpeed); |
---|
243 | } |
---|
244 | public void FadeIn() |
---|
245 | { |
---|
246 | if (Fade != null && Fade.IsAddedToGame) Fade.Destroy(); |
---|
247 | |
---|
248 | Fade = new Widget(Screen.WidthSafe, Screen.HeightSafe); |
---|
249 | Fade.Color = Jypeli.Color.Black; |
---|
250 | Add(Fade); |
---|
251 | Fade.FadeColorTo(Jypeli.Color.Transparent, FadeSpeed); |
---|
252 | } |
---|
253 | |
---|
254 | protected override void Update(Time time) |
---|
255 | { |
---|
256 | if (Flags.InBattle) |
---|
257 | { |
---|
258 | BattleView.Update(time); |
---|
259 | } |
---|
260 | |
---|
261 | base.Update(time); |
---|
262 | } |
---|
263 | |
---|
264 | protected override void Draw(GameTime gameTime) |
---|
265 | { |
---|
266 | GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black); |
---|
267 | |
---|
268 | if (UseShaders) GraphicsDevice.SetRenderTarget(renderTarget); |
---|
269 | |
---|
270 | base.Draw(gameTime); |
---|
271 | GraphicsDevice.SetRenderTarget(null); |
---|
272 | |
---|
273 | if (UseShaders) |
---|
274 | { |
---|
275 | GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black); |
---|
276 | |
---|
277 | spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); |
---|
278 | if (Shader == Content.Load<Effect>("Twirl")) |
---|
279 | { |
---|
280 | Shader.Parameters["Rotation"].SetValue(new Vector2(WarpAmount, WarpAmount)); |
---|
281 | Shader.Parameters["CenterRadius"].SetValue(new Vector4(0.5f, 0.5f, 0.5f, 0.5f)); |
---|
282 | Shader.CurrentTechnique.Passes[0].Apply(); |
---|
283 | } |
---|
284 | else if (Shader == Content.Load<Effect>("Shader") || Shader == Content.Load<Effect>("Wave")) |
---|
285 | { |
---|
286 | Shader.Parameters["parameter1"].SetValue((float)Time.SinceStartOfGame.TotalMilliseconds); |
---|
287 | Shader.CurrentTechnique.Passes[0].Apply(); |
---|
288 | } |
---|
289 | spriteBatch.Draw(renderTarget, new Vector2(0, 0), Microsoft.Xna.Framework.Color.White); |
---|
290 | spriteBatch.End(); |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|