Skip to content

Commit

Permalink
Remove rifle light on spawn
Browse files Browse the repository at this point in the history
Improve enemy hit animation
Fix multiple player hits
Fix enemy animations on player death
  • Loading branch information
UstymUkhman committed Mar 12, 2023
1 parent 4244f4b commit 3b18bdd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
33 changes: 15 additions & 18 deletions src/characters/Enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,9 @@ export default class Enemy extends Character
const prevRun = this.previousAnimation === 'run';
const lastRun = this.lastAnimation === 'run';

if (this.hitting && this.running && prevRun && lastRun) {
if (this.updateHitDamage(this.life)) {
if (this.running && prevRun && lastRun) {
if (this.updateHitDamage(this.life))
return this.fallDeadAnimation('death');
}

this.toggleVisibility(false, 'death');
this.updateAnimation('Idle', 'death');
Expand All @@ -214,33 +213,31 @@ export default class Enemy extends Character

if (this.previousAnimation !== 'hit') {
this.animations.hit.time = this.hitStart;
this.updateAnimation('Idle', 'hit', 0.15);
this.updateAnimation('Idle', 'hit', 0.1);
}

this.hitTimeout = setTimeout(() => {
if (this.dead || this.attacking) return;

const attacking = this.previousAnimation.includes('Attack');
const hitting = this.previousAnimation === 'hit';
const screaming = this.previousAnimation === 'scream';
const blockingAnimation = attacking || screaming;

const attacking = this.previousAnimation.includes('Attack');
const blockingAnimation = hitting || screaming || attacking;

if (!blockingAnimation) this.animTimeout = this.updateAnimation(
animation, this.previousAnimation, 0.15
animation, this.previousAnimation, 0.1
);

this.hitTimeout = setTimeout(() => {
if (this.dead) return;
this.hitting = false;

const attack = this.distance < this.attackDistance;
const running = this.distance < this.runDistance;
const hit = this.previousAnimation === 'hit';
const run = running && !this.running;

if (attack) this.attack();
else if (hit || run) this.run();
}, +!blockingAnimation * 150);
}, this.hitDuration - 150);
const run = this.distance < this.runDistance && !this.running;
const attack = this.distance < this.attackDistance;
attack ? this.attack() : run && this.run();
}, +!blockingAnimation * 100);
}, this.hitDuration - 100);

const animation = this.animation;
this.hitTime = Date.now();
Expand Down Expand Up @@ -390,7 +387,7 @@ export default class Enemy extends Character
}

private run (): void {
if (this.dead) return;
if (this.dead || this.playerDead) return;

const slow = +(this.attacking || this.screaming) * 0.1;
this.updateAnimation('Running', 'run', slow + 0.1);
Expand Down Expand Up @@ -669,7 +666,7 @@ export default class Enemy extends Character
}

public set playerDeath (dead: boolean) {
this.playerDead = dead;
(this.playerDead = dead) && this.moving && this.idle();
}

public get hitBox (): Array<Object3D> {
Expand Down
2 changes: 1 addition & 1 deletion src/characters/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class Player extends Character
}

public hit (direction: HitDirection, damage: number): void {
if (this.dead || this.updateHealth(damage)) return;
if (this.dead || this.updateHealth(damage) || this.hitting) return;
this.aiming && this.stopAiming(this.running);

const hitAnimation = this.getHitAnimation(direction);
Expand Down
18 changes: 9 additions & 9 deletions src/utils/Material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { MeshPhongMaterial } from 'three/src/materials/MeshPhongMaterial';
import type { Shader } from 'three/src/renderers/shaders/ShaderLib';

// Development imports
import fragPars from '@/shaders/ground/pars.frag';
import vertPars from '@/shaders/ground/pars.vert';
import fragMain from '@/shaders/ground/main.frag';
import vertMain from '@/shaders/ground/main.vert';
// import fragPars from '@/shaders/ground/pars.frag';
// import vertPars from '@/shaders/ground/pars.vert';
// import fragMain from '@/shaders/ground/main.frag';
// import vertMain from '@/shaders/ground/main.vert';

// import { Assets } from '@/loaders/AssetsLoader';
import { Assets } from '@/loaders/AssetsLoader';
import { Color } from '@/utils/Color';

export namespace Material
Expand All @@ -23,8 +23,8 @@ export namespace Material

private async updateDefaultFragmentShader (shader: Shader): Promise<void> {
// Production imports:
// const fragPars = await Assets.Loader.loadShader('ground/pars.frag');
// const fragMain = await Assets.Loader.loadShader('ground/main.frag');
const fragPars = await Assets.Loader.loadShader('ground/pars.frag');
const fragMain = await Assets.Loader.loadShader('ground/main.frag');

shader.fragmentShader = `${fragPars}
${shader.fragmentShader.replace(
Expand All @@ -35,8 +35,8 @@ export namespace Material

private async updateDefaultVertexShader (shader: Shader): Promise<void> {
// Production imports:
// const vertPars = await Assets.Loader.loadShader('ground/pars.vert');
// const vertMain = await Assets.Loader.loadShader('ground/main.vert');
const vertPars = await Assets.Loader.loadShader('ground/pars.vert');
const vertMain = await Assets.Loader.loadShader('ground/main.vert');

shader.vertexShader = `${vertPars}
${shader.vertexShader.replace(
Expand Down
34 changes: 17 additions & 17 deletions src/weapons/Rifle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Texture } from 'three/src/textures/Texture';
import { PointLight } from 'three/src/lights/PointLight';
// import { PointLight } from 'three/src/lights/PointLight';
import type { Vector3 } from 'three/src/math/Vector3';
import type { Assets } from '@/loaders/AssetsLoader';
import type { WeaponConfig } from '@/weapons/types';
Expand All @@ -9,20 +9,20 @@ import type { LevelCoords } from '@/scenes/types';
import type { Euler } from 'three/src/math/Euler';
import { GameEvents } from '@/events/GameEvents';

import { Color } from '@/utils/Color';
// import { Color } from '@/utils/Color';
import Weapon from '@/weapons/Weapon';
import Settings from '@/settings';
// import Settings from '@/settings';
import Configs from '@/configs';
import anime from 'animejs';

export default class Rifle extends Weapon
{
private readonly halfLightPower = +Settings.getVisualValue('physicalLights') * 70 + 5;
/* private readonly halfLightPower = +Settings.getVisualValue('physicalLights') * 70 + 5;
private readonly light = new PointLight(
Color.WHITE, 1.0, +!Settings.getVisualValue('physicalLights') * 2.5 + 2.5,
+Settings.getVisualValue('physicalLights') + 1.0
);
); */

private readonly spinePosition = Configs.Rifle.spinePosition as Vector3;
private readonly spineRotation = Configs.Rifle.spineRotation as Euler;
Expand All @@ -37,7 +37,7 @@ export default class Rifle extends Weapon
private spine!: Assets.GLTF;

private appended = false;
private spawnTime = 0.0;
// private spawnTime = 0.0;
private spawned = false;

public constructor (envMap: Texture) {
Expand All @@ -53,11 +53,11 @@ export default class Rifle extends Weapon
GameEvents.dispatch('Level::AddObject', clone);
clone.rotation.set(0.0, 0.0, 0.0);

this.light.visible = false;
this.light.power = 0.0;
// this.light.visible = false;
// this.light.power = 0.0;
clone.visible = false;

clone.add(this.light);
// clone.add(this.light);
this.clone = clone;
return clone;
}
Expand Down Expand Up @@ -150,28 +150,28 @@ export default class Rifle extends Weapon
this.clone.rotation.y -= 0.025;

const playerDistance = this.clone.position.distanceToSquared(player);
const normalizedPower = Math.cos(this.spawnTime += 0.05) + 1.0;
this.light.power = normalizedPower * this.halfLightPower;
// const normalizedPower = Math.cos(this.spawnTime += 0.05) + 1.0;
// this.light.power = normalizedPower * this.halfLightPower;

if (this.inStock < this.maxStock && playerDistance < 6.25) {
GameEvents.dispatch('Player::PickRifle', this.clone);
GameEvents.dispatch('Rifle::Pick', null, true);

this.clone.visible = false;
this.light.visible = false;
this.light.power = 0.0;
// this.light.visible = false;
// this.light.power = 0.0;
this.spawned = false;
}
}

public spawn (coords: LevelCoords): void {
this.clone.position.set(coords[0], 1.75, coords[1]);
GameEvents.dispatch('Rifle::Spawn', coords, true);
this.light.power = this.halfLightPower * 2.0;
// this.light.power = this.halfLightPower * 2.0;

this.clone.visible = true;
this.light.visible = true;
this.spawnTime = 0.0;
// this.light.visible = true;
// this.spawnTime = 0.0;
this.spawned = true;
}

Expand Down Expand Up @@ -199,7 +199,7 @@ export default class Rifle extends Weapon
}

public override dispose (): void {
this.light.dispose();
// this.light.dispose();
this.clone.clear();
super.dispose();
}
Expand Down

0 comments on commit 3b18bdd

Please sign in to comment.