Skip to content

Commit 3acdbf8

Browse files
Merge pull request #9 from red-the-random-dev/intermediate-changes [w2d3]
Introduction of camer tracking
2 parents c0c3a80 + 68d55ca commit 3acdbf8

14 files changed

+168
-33
lines changed

Objects/CameraFrameObject.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.Xna.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace DotRPG.Objects
7+
{
8+
public class CameraFrameObject
9+
{
10+
public Point Focus;
11+
public Point TrackTarget;
12+
public Single CameraVelocity;
13+
14+
public Point GetTopLeftAngle(Point screenSize)
15+
{
16+
return new Point(Focus.X - screenSize.X/2, Focus.Y - screenSize.Y/2);
17+
}
18+
public void Update(GameTime gameTime)
19+
{
20+
Vector2 cameraMovement = (Focus - TrackTarget).ToVector2();
21+
Vector2 cameraMovementDirection = cameraMovement / cameraMovement.Length();
22+
Vector2 cameraMovementNew = cameraMovementDirection * CameraVelocity * (Single)gameTime.ElapsedGameTime.TotalSeconds;
23+
if (cameraMovementNew.Length() <= cameraMovement.Length())
24+
{
25+
Focus -= cameraMovementNew.ToPoint();
26+
}
27+
else
28+
{
29+
Focus -= cameraMovement.ToPoint();
30+
}
31+
}
32+
}
33+
}

Objects/Dynamics/DynamicRectObject.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ public void CollideWith(DynamicRectObject another, Boolean hitVertically, Boolea
167167
}
168168
}
169169

170-
public void Draw(SpriteBatch _sb, GameTime gameTime, Int32 VirtualVSize, Point scrollOffset, Point scrollSize)
170+
public void Draw(SpriteBatch _sb, GameTime gameTime, Int32 VirtualVSize, Point scrollOffset, Point scrollSize, Single ZIndex = 0.0f)
171171
{
172172
Single sizeMorph = 1.0f * scrollSize.Y / VirtualVSize;
173173
Vector2 location = new Vector2
174174
(
175-
Location.X * sizeMorph - (BodySize.X * sizeMorph / 2) - scrollOffset.X * sizeMorph,
176-
Location.Y * sizeMorph - (BodySize.Y * sizeMorph / 2) - scrollOffset.Y * sizeMorph
175+
Location.X * sizeMorph - (Sprite.SpriteSize.X * sizeMorph / 2) - scrollOffset.X,
176+
Location.Y * sizeMorph + (BodySize.Y * sizeMorph / 2) - (Sprite.SpriteSize.Y * sizeMorph) - scrollOffset.Y
177177
);
178-
Sprite.Draw(_sb, location, gameTime, sizeMorph);
178+
Sprite.Draw(_sb, location, gameTime, sizeMorph, ZIndex);
179179
}
180180

181181
public Boolean TryCollideWith(DynamicRectObject another, Boolean splitVector = false)
@@ -197,8 +197,8 @@ public Boolean TryCollideWith(DynamicRectObject another, Boolean splitVector = f
197197

198198
public virtual void Update(GameTime gameTime)
199199
{
200-
Velocity += AppliedForce / ((Single)gameTime.ElapsedGameTime.TotalSeconds * this.Mass);
201-
Location += Velocity / (Single)gameTime.ElapsedGameTime.TotalSeconds;
200+
Velocity += AppliedForce * ((Single)gameTime.ElapsedGameTime.TotalSeconds * this.Mass);
201+
Location += Velocity * (Single)gameTime.ElapsedGameTime.TotalSeconds;
202202
AppliedForce = Vector2.Zero;
203203
}
204204

Objects/SpriteController.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ public void AddAnimationSequence(String sequenceName, Texture2D frames, UInt16 f
5555
LoopTo.Add(sequenceName, loopTo);
5656
}
5757

58-
public void Draw(SpriteBatch _sb, Vector2 drawLocation, GameTime gameTime, Single drawSize = 1.0f)
58+
public Point SpriteSize
59+
{
60+
get
61+
{
62+
return new Point(AnimationSequenceCollection[CurrentAnimationSequence].Width / FrameAmount[CurrentAnimationSequence], AnimationSequenceCollection[CurrentAnimationSequence].Height);
63+
}
64+
}
65+
66+
public void Draw(SpriteBatch _sb, Vector2 drawLocation, GameTime gameTime, Single drawSize = 1.0f, Single ZIndex = 0.0f)
5967
{
6068
Single addFrames = 0.0f;
6169
if (PlaybackSpeed > 0.0f)
@@ -66,13 +74,13 @@ public void Draw(SpriteBatch _sb, Vector2 drawLocation, GameTime gameTime, Singl
6674
ScrollTimer -= (FrameTime / PlaybackSpeed) * (addFrames / PlaybackSpeed);
6775
}
6876
Single newPos = _i + addFrames;
69-
while (newPos > FrameAmount[CurrentAnimationSequence])
77+
if (newPos >= FrameAmount[CurrentAnimationSequence])
7078
{
71-
newPos = (ushort)(newPos % FrameAmount[CurrentAnimationSequence] + LoopTo[CurrentAnimationSequence]);
79+
newPos = LoopTo[CurrentAnimationSequence];
7280
}
7381
Texture2D toDraw = AnimationSequenceCollection[CurrentAnimationSequence];
7482
Single widthPerFrame = toDraw.Width / FrameAmount[CurrentAnimationSequence];
75-
UInt16 newPosI = (ushort)Math.Round(newPos);
83+
UInt16 newPosI = (ushort)Math.Floor(newPos);
7684
Rectangle lololol = new Rectangle
7785
(
7886
(int)(newPosI * widthPerFrame),
@@ -90,7 +98,7 @@ public void Draw(SpriteBatch _sb, Vector2 drawLocation, GameTime gameTime, Singl
9098
Vector2.Zero,
9199
drawSize,
92100
SpriteEffects.None,
93-
0
101+
ZIndex
94102
);
95103
_i = newPos;
96104
}

_Example/DynamicsTestFrame.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public override void Update(GameTime gameTime, bool[] controls)
7474
if (controls[3]) { loco_x += 1.0f; }
7575
Vector2 Locomotion = new Vector2(loco_x, loco_y);
7676
Locomotion /= (Locomotion.Length() != 0 ? Locomotion.Length() : 1.0f);
77-
Locomotion *= 0.1f;
77+
Locomotion *= 256f;
7878
Vector2 FrictionVector1 = new Vector2(0.0f - (Obstacle1.Velocity.X / (Obstacle1.Velocity.Length() != 0 ? Obstacle1.Velocity.Length() : 1.0f)), 0.0f - (Obstacle1.Velocity.Y / (Obstacle1.Velocity.Length() != 0 ? Obstacle1.Velocity.Length() : 1.0f)));
79-
FrictionVector1 *= Obstacle1.Mass * 0.00001f;
79+
FrictionVector1 *= Obstacle1.Mass * 0.5f;
8080
Vector2 FrictionVector2 = new Vector2(0.0f - (Obstacle2.Velocity.X / (Obstacle2.Velocity.Length() != 0 ? Obstacle2.Velocity.Length() : 1.0f)), 0.0f - (Obstacle2.Velocity.Y / (Obstacle2.Velocity.Length() != 0 ? Obstacle2.Velocity.Length() : 1.0f)));
81-
FrictionVector2 *= Obstacle2.Mass * 0.00000005f;
81+
FrictionVector2 *= Obstacle2.Mass * 0.05f;
8282
if (Obstacle1.Velocity.Length() >= 0.001f)
8383
{
8484
#if !MUTE

_Example/Game1.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public class Game1 : Game
2525
private Boolean[] IsCtrlKeyDown = new bool[8] { false, false, false, false, false, false, false, false};
2626
private Boolean FullScreen;
2727
private Double EscapeTimer = 0.0f;
28-
#if DEBUG
28+
#if DEBUG
2929
private Double LastRegisteredEventTime;
30+
#endif
3031
private Double TimeSinceError = 0.0f;
31-
#endif
3232
private HashSet<TimedEvent> LogicEventSet = new HashSet<TimedEvent>();
3333
private Boolean ContinuityError = false;
3434
private Boolean WideScreen
@@ -221,10 +221,7 @@ protected override void Draw(GameTime gameTime)
221221
GraphicsDevice.Clear(Color.Black);
222222

223223
_spriteBatch.Begin();
224-
#if DEBUG
225-
_spriteBatch.DrawString(_spriteFont, "FPS: "+FrameRate.ToString()+" || Fullscreen: "+FullScreen.ToString()+String.Format(" || Resolution: {0}x{1}", Window.ClientBounds.Width, Window.ClientBounds.Height) + " || Frame active: "+(ActiveFrame != null?ActiveFrame.FrameID.ToString():"-1")+" || Update rate: "+Math.Round(1000/LastRegisteredEventTime), new Vector2(0, 0), (FrameRate > 50 ? Color.White : (FrameRate > 24 ? Color.Yellow : Color.Red)));
226-
#endif
227-
_spriteBatch.DrawString(_spriteFont, "Quitting...", new Vector2(0, 12), new Color(new Vector4((float) EscapeTimer/1000)));
224+
228225
if (ContinuityError)
229226
{
230227
_spriteBatch.DrawString(_spriteFontLarge, "/!\\ CONTINUITY ERROR /!\\", SharedMethodSet.FindTextAlignment(_spriteFontLarge, "/!\\ CONTINUITY ERROR /!\\", Window.ClientBounds, 0.5f, 0.5f), Color.Red);
@@ -261,6 +258,10 @@ protected override void Draw(GameTime gameTime)
261258
{
262259
ActiveFrame.Draw(gameTime, _spriteBatch);
263260
}
261+
#if DEBUG
262+
_spriteBatch.DrawString(_spriteFont, "FPS: " + FrameRate.ToString() + " || Fullscreen: " + FullScreen.ToString() + String.Format(" || Resolution: {0}x{1}", Window.ClientBounds.Width, Window.ClientBounds.Height) + " || Frame active: " + (ActiveFrame != null ? ActiveFrame.FrameID.ToString() : "-1") + " || Update rate: " + Math.Round(1000 / LastRegisteredEventTime), new Vector2(0, 0), (FrameRate > 50 ? Color.White : (FrameRate > 24 ? Color.Yellow : Color.Red)));
263+
#endif
264+
_spriteBatch.DrawString(_spriteFont, "Quitting...", new Vector2(0, 12), new Color(new Vector4((float)EscapeTimer / 1000)));
264265
_spriteBatch.End();
265266

266267
base.Draw(gameTime);

_Example/GameData/DotRPG.Example_data.mgcb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@
6060
/processorParam:Quality=Best
6161
/build:Sounds/wallcling.wav
6262

63+
#begin Texture2D/backdrop.png
64+
/importer:TextureImporter
65+
/processor:TextureProcessor
66+
/processorParam:ColorKeyColor=255,0,255,255
67+
/processorParam:ColorKeyEnabled=True
68+
/processorParam:GenerateMipmaps=False
69+
/processorParam:PremultiplyAlpha=True
70+
/processorParam:ResizeToPowerOfTwo=False
71+
/processorParam:MakeSquare=False
72+
/processorParam:TextureFormat=Color
73+
/build:Texture2D/backdrop.png
74+
6375
#begin Texture2D/Banana.png
6476
/importer:TextureImporter
6577
/processor:TextureProcessor
@@ -108,6 +120,54 @@
108120
/processorParam:TextureFormat=Color
109121
/build:Texture2D/cube-p.png
110122

123+
#begin Texture2D/red.idle.down.png
124+
/importer:TextureImporter
125+
/processor:TextureProcessor
126+
/processorParam:ColorKeyColor=255,0,255,255
127+
/processorParam:ColorKeyEnabled=True
128+
/processorParam:GenerateMipmaps=False
129+
/processorParam:PremultiplyAlpha=True
130+
/processorParam:ResizeToPowerOfTwo=False
131+
/processorParam:MakeSquare=False
132+
/processorParam:TextureFormat=Color
133+
/build:Texture2D/red.idle.down.png
134+
135+
#begin Texture2D/red.idle.left.png
136+
/importer:TextureImporter
137+
/processor:TextureProcessor
138+
/processorParam:ColorKeyColor=255,0,255,255
139+
/processorParam:ColorKeyEnabled=True
140+
/processorParam:GenerateMipmaps=False
141+
/processorParam:PremultiplyAlpha=True
142+
/processorParam:ResizeToPowerOfTwo=False
143+
/processorParam:MakeSquare=False
144+
/processorParam:TextureFormat=Color
145+
/build:Texture2D/red.idle.left.png
146+
147+
#begin Texture2D/red.idle.right.png
148+
/importer:TextureImporter
149+
/processor:TextureProcessor
150+
/processorParam:ColorKeyColor=255,0,255,255
151+
/processorParam:ColorKeyEnabled=True
152+
/processorParam:GenerateMipmaps=False
153+
/processorParam:PremultiplyAlpha=True
154+
/processorParam:ResizeToPowerOfTwo=False
155+
/processorParam:MakeSquare=False
156+
/processorParam:TextureFormat=Color
157+
/build:Texture2D/red.idle.right.png
158+
159+
#begin Texture2D/red.idle.up.png
160+
/importer:TextureImporter
161+
/processor:TextureProcessor
162+
/processorParam:ColorKeyColor=255,0,255,255
163+
/processorParam:ColorKeyEnabled=True
164+
/processorParam:GenerateMipmaps=False
165+
/processorParam:PremultiplyAlpha=True
166+
/processorParam:ResizeToPowerOfTwo=False
167+
/processorParam:MakeSquare=False
168+
/processorParam:TextureFormat=Color
169+
/build:Texture2D/red.idle.up.png
170+
111171
#begin Texture2D/ScrollMarker.png
112172
/importer:TextureImporter
113173
/processor:TextureProcessor

_Example/GameData/Fonts/File.spritefont

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ with.
1111
<!--
1212
Modify this string to change the font that will be imported.
1313
-->
14-
<FontName>VCR OSD Mono</FontName>
15-
14+
<!--<FontName>VCR OSD Mono</FontName>-->
15+
<FontName>Alef</FontName>
1616
<!--
1717
Size is a float value, measured in points. Modify this value to change
1818
the size of the font.

_Example/GameData/Fonts/MainFont_Large.spritefont

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ with.
1111
<!--
1212
Modify this string to change the font that will be imported.
1313
-->
14-
<FontName>VCR OSD Mono</FontName>
15-
14+
<!--<FontName>VCR OSD Mono</FontName>-->
15+
<FontName>Alef</FontName>
1616
<!--
1717
Size is a float value, measured in points. Modify this value to change
1818
the size of the font.
685 KB
Loading
5.28 KB
Loading

0 commit comments

Comments
 (0)