Repository navigation
Expand file tree
/
Copy pathSpring.cs
More file actions
93 lines (80 loc) · 3.29 KB
/
Copy pathSpring.cs
File metadata and controls
93 lines (80 loc) · 3.29 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
// Gehört zu Johannes bzw. stellenweise Maxi
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Runtime.Serialization;
namespace Info2021 {
[DataContract]
class Spring : CinematicObject, ILevelElement {
[DataMember]
public override Vector2 Position { get; set; }
private bool hasTouchedLastFrame, hasTouchedThisFrame;
[DataMember]
Vector2 topLeft;
[DataMember]
Vector2 diag;
[DataMember]
private Vector2 direction;
// 0 = face left, 1 = face upwards, etc
[DataMember]
int rotation;
public Spring(Vector2 position, int rotation) {
Position = position;
this.rotation = rotation;
hasTouchedLastFrame = false;
hasTouchedThisFrame = false;
switch (rotation) {
case 0:
topLeft = position + Vector2.UnitX * 8;
diag = new Vector2(8, 16);
direction = -Vector2.UnitX;
break;
case 1:
topLeft = position;
diag = new Vector2(16, 8);
direction = Vector2.UnitY;
break;
case 2:
topLeft = position;
diag = new Vector2(8, 16);
direction = Vector2.UnitX;
break;
case 3:
topLeft = position + Vector2.UnitY * 8;
diag = new Vector2(16, 8);
direction = -Vector2.UnitY;
break;
default:
throw new System.InvalidOperationException();
}
CCollider = new CinematicCollider(this, topLeft, diag);
}
public override Texture2D GetTexture(ResourceAccessor resourceAccessor) {
return resourceAccessor.GetSprite(7, 15 - rotation);
}
public override void OnCollision(Player player) {
float newVel = 0;
if (player.OnGround())
// increase position by a bit to avoid ground issues
player.VelPos = player.VelPos.Translate(direction * 3);
// don't bounce two times at once
if (hasTouchedLastFrame) return;
if (rotation % 2 == 0) {
// if you're moving against the spring, reverse that velocity, otherwise gain some
if (Math.Sign(player.VelPos.V.X) == -Math.Sign(direction.X) && !player.OnGround())
newVel = -player.VelPos.V.X;
player.VelPos = player.VelPos.WithVelocity(new Vector2(MathF.MaxMagnitude(direction.X * 400, newVel), player.VelPos.V.Y));
}
if (rotation % 2 == 1) {
if (Math.Sign(player.VelPos.V.Y) == -Math.Sign(direction.Y) && !player.OnGround())
newVel = -player.VelPos.V.Y;
player.VelPos = player.VelPos.WithVelocity(new Vector2(player.VelPos.V.X, MathF.MaxMagnitude(direction.Y * 400, newVel)));
}
hasTouchedThisFrame = true;
}
public override void Update(float dt, Player player) {
hasTouchedLastFrame = hasTouchedThisFrame;
hasTouchedThisFrame = false;
}
}
}