Skip to content

Commit 146c41c

Browse files
committed
testing out: radial gradients, stop tweening, projectile templates
1 parent 2e4643f commit 146c41c

5 files changed

Lines changed: 105 additions & 40 deletions

File tree

Animation.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ class AnimatedPath
183183
}
184184
return grad;
185185
}
186+
case "radial":
187+
{
188+
let grad = ctx.createRadialGradient(...graddef.coords);
189+
for(let i=0;i<graddef.stops.length;i++)
190+
{
191+
grad.addColorStop(graddef.stops[i],graddef.colours[i]);
192+
}
193+
return grad;
194+
}
186195
}
187196
}
188197

@@ -191,16 +200,20 @@ class AnimatedPath
191200
{
192201
let finalcoords=[];
193202
let finalcolours=[];
203+
let finalstops = [];
194204
a.coords.forEach((val,i)=>{
195205
finalcoords.push(a.coords[i]+(b.coords[i]-a.coords[i])*t);
196206
});
197207
a.colours.forEach((val,i)=>{
198208
finalcolours.push(AnimatedPath.tweenHex(a.colours[i],b.colours[i],t));
199209
});
210+
a.stops.forEach((val,i)=>{
211+
finalstops.push(a.stops[i]+(b.stops[i]-a.stops[i])*t);
212+
});
200213
return {
201214
type: a.type,
202215
coords:finalcoords,
203-
stops:a.stops,
216+
stops:finalstops,
204217
colours:finalcolours
205218
};
206219
}

gameObjects/Player.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,12 @@ class Player extends Actor
122122
this.shootingCoolDown=0;
123123
}
124124
// spawn a bullet at player's location
125-
let bb = new Projectile();
125+
let bb = Projectile.fromTemplate(ProjectileData.projectiles.basic2);
126126
bb.x=this.x;
127127
bb.y=this.y;
128128
// make it home towards the point straight ahead of the player
129129
bb.targetX=this.x;
130130
bb.targetY =-1;
131-
bb.speed = 1200;
132131
this.scene.addObject(bb);
133132
}
134133
}

gameObjects/Projectile.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,13 @@ class Projectile extends GameObject
1111
Damage dealt by this projectile on hitting a target
1212
*/
1313
damage = 4;
14-
test1 = {
15-
"idle": [[
16-
{
17-
fill: "#FFFFFF",
18-
time: 0.0,
19-
path: "M 0 30 C -15 -20 15 -20 0 30 z"
20-
}
21-
]]
22-
};
23-
test2 = {
24-
"idle": [[
25-
{
26-
fill: {
27-
type: "linear",
28-
coords: [0, -5, 0, 30],
29-
stops: [0,1],
30-
colours: ["#FFFF00FF","#FF780030"]
31-
},
32-
time: 0.0,
33-
path: "M 0 30 C -15 -20 15 -20 0 30 z"
34-
}
35-
]]
36-
};
37-
constructor()
14+
constructor(shape)
3815
{
3916
super("bullet");
4017
// default oblong projectile
4118
this.hitbox = new Rectangle(-5,-5,10,30);
4219
this.originalHitbox = new Rectangle(-5,-5,10,30);
43-
this.sprite=VectorSprite.fromRawObject(this.test2);
44-
}
45-
draw(ctx)
46-
{
47-
super.draw(ctx);
48-
return;
49-
// define a trail fading from yellow to orange
50-
// #TODO: something less hardcoded
51-
const grad = ctx.createLinearGradient(this.x,this.y-5,this.x,this.y+30);
52-
grad.addColorStop(0, "rgb(255 255 0)");
53-
grad.addColorStop(1,"rgb(255 120 0 / 0.2)");
54-
ctx.fillStyle = grad;
55-
ctx.fillRect(this.x-5,this.y-5,10,30);
20+
this.sprite=VectorSprite.fromRawObject(shape);
5621
}
5722
/**
5823
* Defines what happens when this collides with an entity
@@ -75,4 +40,13 @@ class Projectile extends GameObject
7540
this.isDead=true;
7641
}
7742
}
43+
static fromTemplate(template)
44+
{
45+
let result = new Projectile(ProjectileData.graphics[template.sprite]);
46+
result.hitbox = new Rectangle(...template.rect);
47+
result.speed = template.speed;
48+
result.damage = template.damage;
49+
50+
return result;
51+
}
7852
}

gameObjects/ProjectileData.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class ProjectileData
2+
{
3+
static graphics =
4+
{
5+
basic_flame:
6+
{
7+
"idle":
8+
[
9+
[
10+
{
11+
fill: {
12+
type: "linear",
13+
coords: [0, -5, 0, 30],
14+
stops: [0,1],
15+
colours: ["#FFFF00FF","#FF780030"]
16+
},
17+
time: 0.0,
18+
path: "M 0 30 C -15 -20 15 -20 0 30 z"
19+
}
20+
]
21+
]
22+
},
23+
basic_orb:
24+
{
25+
"idle":
26+
[
27+
[
28+
{
29+
fill: {
30+
type: "radial",
31+
coords: [0, 0, 1, 0, 0, 20],
32+
stops: [0,0.2,0.4,0.7,1.0],
33+
colours: ["#CE00CEFF","#CE00CE00","#CE00CE00","#FFA0FFFF","#CE00CE00"]
34+
},
35+
time: 0.0,
36+
path: "M -20 -20 H 20 V 20 H -20 V -20 z"
37+
},
38+
{
39+
fill: {
40+
type: "radial",
41+
coords: [0, 0, 1, 0, 0, 20],
42+
stops: [0,0.2,0.8,0.9,1.0],
43+
colours: ["#FFA0FFFF","#CE00CE00","#CE00CE00","#FFA0FFFF","#CE00CE00"]
44+
},
45+
time: 0.2,
46+
path: "M -20 -20 H 20 V 20 H -20 V -20 z"
47+
},
48+
{
49+
fill: {
50+
type: "radial",
51+
coords: [0, 0, 1, 0, 0, 20],
52+
stops: [0,0.2,0.4,0.7,1.0],
53+
colours: ["#CE00CEFF","#CE00CE00","#CE00CE00","#FFA0FFFF","#CE00CE00"]
54+
},
55+
time: 0.4,
56+
path: "M -20 -20 H 20 V 20 H -20 V -20 z"
57+
}
58+
]
59+
]
60+
}
61+
};
62+
static projectiles = {
63+
basic1: {
64+
sprite: "basic_flame",
65+
rect: [-5,-5,10,30],
66+
speed: 1200,
67+
damage: 4,
68+
trajectory: "straight"
69+
},
70+
basic2: {
71+
sprite: "basic_orb",
72+
rect: [-10,-10,10,10],
73+
speed: 900,
74+
damage: 6,
75+
trajectory: "straight"
76+
}
77+
};
78+
}

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<script src="gameObjects/Pickup.js"></script>
2626
<script src="gameObjects/PickupData.js"></script>
2727
<script src="gameObjects/Projectile.js"></script>
28+
<script src="gameObjects/ProjectileData.js"></script>
2829
<script src="gameObjects/BgStar.js"></script>
2930
<script src="gameObjects/Hostile.js"></script>
3031
<script src="gameObjects/HostileData.js"></script>

0 commit comments

Comments
 (0)