Converting game to new animation framework

This commit is contained in:
Yonokid
2024-08-24 03:49:57 -04:00
parent 10fd480e79
commit 511ae64ccc
2 changed files with 151 additions and 83 deletions

151
game.py
View File

@@ -592,84 +592,71 @@ class Player:
class Judgement: class Judgement:
def __init__(self, current_ms, type, big): def __init__(self, current_ms, type, big):
self.y_pos = 144
self.create_ms = current_ms
self.total_distance = 15
self.current_y_pos = self.y_pos
self.total_duration = 332
self.type = type self.type = type
self.fade = 1
self.hit_fade = 0.5
self.color = ray.fade(ray.WHITE, self.fade)
self.hit_color = ray.fade(ray.WHITE, self.hit_fade)
self.big = big self.big = big
self.index = 0
self.is_finished = False self.is_finished = False
self.fade_animation_1 = Animation(current_ms, 132, 'fade')
self.fade_animation_1.params['initial_opacity'] = 0.5
self.fade_animation_1.params['delay'] = 100
self.fade_animation_2 = Animation(current_ms, 316 - 233.3, 'fade')
self.fade_animation_2.params['delay'] = 233.3
self.move_animation = Animation(current_ms, 83, 'move')
self.move_animation.params['total_distance'] = 15
self.move_animation.params['start_position'] = 144
self.texture_animation = Animation(current_ms, 100, 'texture_change')
self.texture_animation.params['textures'] = [(33, 50, 1), (50, 83, 2), (83, 100, 3), (100, float('inf'), 4)]
def update(self, current_ms): def update(self, current_ms):
elapsed_time = current_ms - self.create_ms self.fade_animation_1.update(current_ms)
self.fade_animation_2.update(current_ms)
self.move_animation.update(current_ms)
self.texture_animation.update(current_ms)
if elapsed_time <= self.total_duration: if self.fade_animation_2.is_finished:
if elapsed_time <= 83:
progress = elapsed_time / 83
self.current_y_pos = self.y_pos + (self.total_distance * progress)
else:
self.current_y_pos = self.y_pos + self.total_distance
if 33 < elapsed_time <= 50:
self.index = 1
elif 50 < elapsed_time <= 83:
self.index = 2
elif 83 < elapsed_time <= 100:
self.index = 3
elif elapsed_time > 100:
self.index = 4
elif 83 < elapsed_time <= 166:
hit_fade_progress = (elapsed_time - 83) / (166 - 83)
self.hit_fade = 1 - hit_fade_progress
self.hit_color = ray.fade(ray.WHITE, self.fade)
if elapsed_time > 233.3:
fade_progress = (elapsed_time - 233.3) / (333 - 233.3)
self.fade = 1 - fade_progress
self.color = ray.fade(ray.WHITE, self.fade)
if elapsed_time > self.total_duration:
self.is_finished = True self.is_finished = True
def draw(self, game_screen): def draw(self, game_screen):
y = self.move_animation.attribute
index = self.texture_animation.attribute
hit_color = ray.fade(ray.WHITE, self.fade_animation_1.attribute)
color = ray.fade(ray.WHITE, self.fade_animation_2.attribute)
if self.type == 'GOOD': if self.type == 'GOOD':
if self.big: if self.big:
ray.draw_texture(game_screen.texture_good_hit_center_big, 342, 184, self.color) ray.draw_texture(game_screen.texture_good_hit_center_big, 342, 184, color)
ray.draw_texture(game_screen.texture_good_hit_effect_big[self.index], 304, 143, self.hit_color) ray.draw_texture(game_screen.texture_good_hit_effect_big[index], 304, 143, hit_color)
else: else:
ray.draw_texture(game_screen.texture_good_hit_center, 342, 184, self.color) ray.draw_texture(game_screen.texture_good_hit_center, 342, 184, color)
ray.draw_texture(game_screen.texture_good_hit_effect[self.index], 304, 143, self.hit_color) ray.draw_texture(game_screen.texture_good_hit_effect[index], 304, 143, hit_color)
ray.draw_texture(game_screen.texture_good, 370, int(self.current_y_pos), self.color) ray.draw_texture(game_screen.texture_good, 370, int(y), color)
elif self.type == 'OK': elif self.type == 'OK':
if self.big: if self.big:
ray.draw_texture(game_screen.texture_ok_hit_center_big, 342, 184, self.color) ray.draw_texture(game_screen.texture_ok_hit_center_big, 342, 184, color)
ray.draw_texture(game_screen.texture_ok_hit_effect_big[self.index], 304, 143, self.hit_color) ray.draw_texture(game_screen.texture_ok_hit_effect_big[index], 304, 143, hit_color)
else: else:
ray.draw_texture(game_screen.texture_ok_hit_center, 342, 184, self.color) ray.draw_texture(game_screen.texture_ok_hit_center, 342, 184, color)
ray.draw_texture(game_screen.texture_ok_hit_effect[self.index], 304, 143, self.hit_color) ray.draw_texture(game_screen.texture_ok_hit_effect[index], 304, 143, hit_color)
ray.draw_texture(game_screen.texture_ok, 370, int(self.current_y_pos), self.color) ray.draw_texture(game_screen.texture_ok, 370, int(y), color)
elif self.type == 'BAD': elif self.type == 'BAD':
ray.draw_texture(game_screen.texture_bad, 370, int(self.current_y_pos), self.color) ray.draw_texture(game_screen.texture_bad, 370, int(y), color)
class LaneHitEffect: class LaneHitEffect:
def __init__(self, current_ms, type): def __init__(self, current_ms, type):
self.type = type self.type = type
self.fade = 0.5 self.color = ray.fade(ray.WHITE, 0.5)
self.color = ray.fade(ray.WHITE, self.fade) self.animation = Animation(current_ms, 150, 'fade')
self.create_ms = current_ms self.animation.params['delay'] = 83
self.total_duration = 150 self.animation.params['initial_opacity'] = 0.5
self.is_finished = False self.is_finished = False
def update(self, current_ms): def update(self, current_ms):
elapsed_time = current_ms - self.create_ms self.animation.update(current_ms)
if elapsed_time >= 83: fade_opacity = self.animation.attribute
fade_progress = (elapsed_time - 83) / (self.total_duration - 83) self.color = ray.fade(ray.WHITE, fade_opacity)
self.fade = 0.5 - fade_progress if self.animation.is_finished:
self.color = ray.fade(ray.WHITE, self.fade)
if elapsed_time > self.total_duration:
self.is_finished = True self.is_finished = True
def draw(self, game_screen): def draw(self, game_screen):
@@ -684,19 +671,16 @@ class DrumHitEffect:
def __init__(self, current_ms, type, side): def __init__(self, current_ms, type, side):
self.type = type self.type = type
self.side = side self.side = side
self.fade = 1 self.color = ray.fade(ray.WHITE, 1)
self.color = ray.fade(ray.WHITE, self.fade)
self.create_ms = current_ms
self.total_duration = 150
self.is_finished = False self.is_finished = False
self.animation = Animation(current_ms, 100, 'fade')
self.animation.params['delay'] = 67
def update(self, current_ms): def update(self, current_ms):
elapsed_time = current_ms - self.create_ms self.animation.update(current_ms)
if elapsed_time >= 83: fade_opacity = self.animation.attribute
fade_progress = (elapsed_time - 83) / (self.total_duration - 83) self.color = ray.fade(ray.WHITE, fade_opacity)
self.fade = 0.5 - fade_progress if self.animation.is_finished:
self.color = ray.fade(ray.WHITE, self.fade)
if elapsed_time > self.total_duration:
self.is_finished = True self.is_finished = True
def draw(self, game_screen): def draw(self, game_screen):
@@ -718,16 +702,13 @@ class NoteArc:
self.arc_points = 25 self.arc_points = 25
self.create_ms = current_ms self.create_ms = current_ms
self.player_number = player_number self.player_number = player_number
self.x_i = 0 self.x_i = 414 - 64
self.y_i = 0 self.y_i = 168
self.is_finished = False self.is_finished = False
def update(self, game_screen): def update(self, current_ms):
if self.x_i >= 1150: if self.x_i >= 1150:
self.is_finished = True self.is_finished = True
def draw(self, game_screen):
if self.note_type == None:
return
radius = 414 radius = 414
#Start at 180 degrees, end at 0 #Start at 180 degrees, end at 0
theta_start = 3.14 theta_start = 3.14
@@ -739,7 +720,7 @@ class NoteArc:
theta_end = 0 theta_end = 0
center_x, center_y = 785, 468 center_x, center_y = 785, 468
ms_since_call = (game_screen.current_ms - self.create_ms) / 16.67 ms_since_call = (current_ms - self.create_ms) / 16.67
if ms_since_call < 0: if ms_since_call < 0:
ms_since_call = 0 ms_since_call = 0
if ms_since_call > self.arc_points: if ms_since_call > self.arc_points:
@@ -749,6 +730,9 @@ class NoteArc:
self.x_i = center_x + radius * math.cos(theta_i) self.x_i = center_x + radius * math.cos(theta_i)
self.y_i = center_y + radius * 0.5 * math.sin(theta_i) self.y_i = center_y + radius * 0.5 * math.sin(theta_i)
def draw(self, game_screen):
if self.note_type == None:
return
eighth_in_ms = (60000 * 4 / game_screen.tja.bpm) / 8 eighth_in_ms = (60000 * 4 / game_screen.tja.bpm) / 8
current_eighth = int(game_screen.current_ms // eighth_in_ms) current_eighth = int(game_screen.current_ms // eighth_in_ms)
ray.draw_texture(self.note_type[current_eighth % 2], int(self.x_i), int(self.y_i), ray.WHITE) ray.draw_texture(self.note_type[current_eighth % 2], int(self.x_i), int(self.y_i), ray.WHITE)
@@ -759,11 +743,11 @@ class DrumrollCounter:
self.is_finished = False self.is_finished = False
self.total_duration = 1349 self.total_duration = 1349
self.drumroll_count = 0 self.drumroll_count = 0
self.fade = 1
self.color = ray.fade(ray.WHITE, self.fade)
self.counter_stretch = 0 self.counter_stretch = 0
self.start_stretch = None self.start_stretch = None
self.is_stretching = False self.is_stretching = False
self.fade_animation = Animation(current_ms, 166, 'fade')
self.fade_animation.params['delay'] = self.total_duration - 166
def update_count(self, current_ms, count, elapsed_time): def update_count(self, current_ms, count, elapsed_time):
self.total_duration = elapsed_time + 1349 self.total_duration = elapsed_time + 1349
@@ -787,27 +771,24 @@ class DrumrollCounter:
def update(self, game_screen, current_ms, drumroll_count): def update(self, game_screen, current_ms, drumroll_count):
self.update_stretch(current_ms) self.update_stretch(current_ms)
self.fade_animation.update(current_ms)
elapsed_time = current_ms - self.create_ms elapsed_time = current_ms - self.create_ms
if drumroll_count != 0: if drumroll_count != 0:
self.update_count(current_ms, drumroll_count, elapsed_time) self.update_count(current_ms, drumroll_count, elapsed_time)
fade_start_time = self.total_duration - 166 if self.fade_animation.is_finished:
if elapsed_time >= fade_start_time:
fade_progress = (elapsed_time - fade_start_time) / 166
self.fade = 1 - fade_progress
self.color = ray.fade(ray.WHITE, self.fade)
if elapsed_time > self.total_duration:
self.is_finished = True self.is_finished = True
def draw(self, game_screen): def draw(self, game_screen):
ray.draw_texture(game_screen.texture_drumroll_count, 200, 0, self.color) color = ray.fade(ray.WHITE, self.fade_animation.attribute)
ray.draw_texture(game_screen.texture_drumroll_count, 200, 0, color)
counter = str(self.drumroll_count) counter = str(self.drumroll_count)
total_width = len(counter) * 52 total_width = len(counter) * 52
start_x = 344 - (total_width // 2) start_x = 344 - (total_width // 2)
source_rect = ray.Rectangle(0, 0, game_screen.texture_drumroll_number[0].width, game_screen.texture_drumroll_number[0].height) source_rect = ray.Rectangle(0, 0, game_screen.texture_drumroll_number[0].width, game_screen.texture_drumroll_number[0].height)
for i in range(len(counter)): for i in range(len(counter)):
dest_rect = ray.Rectangle(start_x + (i * 52), 50 - self.counter_stretch, game_screen.texture_drumroll_number[0].width, game_screen.texture_drumroll_number[0].height + self.counter_stretch) dest_rect = ray.Rectangle(start_x + (i * 52), 50 - self.counter_stretch, game_screen.texture_drumroll_number[0].width, game_screen.texture_drumroll_number[0].height + self.counter_stretch)
ray.draw_texture_pro(game_screen.texture_balloon_number[int(counter[i])], source_rect, dest_rect, ray.Vector2(0,0), 0, self.color) ray.draw_texture_pro(game_screen.texture_balloon_number[int(counter[i])], source_rect, dest_rect, ray.Vector2(0,0), 0, color)
class BalloonAnimation: class BalloonAnimation:
def __init__(self, current_ms, balloon_total): def __init__(self, current_ms, balloon_total):
@@ -997,3 +978,7 @@ class ScoreCounter:
for i in range(len(counter)): for i in range(len(counter)):
dest_rect = ray.Rectangle(start_x + (i * margin), y - self.counter_stretch, game_screen.texture_score_numbers[0].width, game_screen.texture_score_numbers[0].height + self.counter_stretch) dest_rect = ray.Rectangle(start_x + (i * margin), y - self.counter_stretch, game_screen.texture_score_numbers[0].width, game_screen.texture_score_numbers[0].height + self.counter_stretch)
ray.draw_texture_pro(game_screen.texture_score_numbers[int(counter[i])], source_rect, dest_rect, ray.Vector2(0,0), 0, ray.WHITE) ray.draw_texture_pro(game_screen.texture_score_numbers[int(counter[i])], source_rect, dest_rect, ray.Vector2(0,0), 0, ray.WHITE)
class ScoreCounterAnimation:
def __init__(self, current_ms):
pass

View File

@@ -232,3 +232,86 @@ class tja_parser:
draw_note_list = deque(sorted(play_note_list, key=lambda d: d['load_ms'])) draw_note_list = deque(sorted(play_note_list, key=lambda d: d['load_ms']))
bar_list = deque(sorted(bar_list, key=lambda d: d['load_ms'])) bar_list = deque(sorted(bar_list, key=lambda d: d['load_ms']))
return play_note_list, draw_note_list, bar_list return play_note_list, draw_note_list, bar_list
class Animation:
def __init__(self, current_ms, duration, type):
self.type = type
self.start_ms = current_ms
self.attribute = 0
self.duration = duration
self.params = dict()
self.is_finished = False
def update(self, current_ms):
if self.type == 'fade':
self.fade(current_ms,
self.duration,
initial_opacity=self.params.get('initial_opacity', 1.0),
final_opacity=self.params.get('final_opacity', 0.0),
delay=self.params.get('delay', 0.0),
ease_in=self.params.get('ease_in', None),
ease_out=self.params.get('ease_out', None))
elif self.type == 'move':
self.move(current_ms,
self.duration,
self.params['total_distance'],
self.params['start_position'])
elif self.type == 'texture_change':
self.texture_change(current_ms,
self.duration,
self.params['textures'])
def fade(self, current_ms, duration, initial_opacity, final_opacity, delay, ease_in, ease_out):
def ease_out_progress(progress, ease):
if ease == 'quadratic':
return progress * (2 - progress)
elif ease == 'cubic':
return 1 - pow(1 - progress, 3)
elif ease == 'exponential':
return 1 - pow(2, -10 * progress)
else:
return progress
def ease_in_progress(progress, ease):
if ease == 'quadratic':
return progress * progress
elif ease == 'cubic':
return progress * progress * progress
elif ease == 'exponential':
return pow(2, 10 * (progress - 1))
else:
return progress
elapsed_time = current_ms - self.start_ms
if elapsed_time < delay:
self.attribute = initial_opacity
elapsed_time -= delay
if elapsed_time >= duration:
self.attribute = final_opacity
self.is_finished = True
if ease_in is not None:
progress = ease_in_progress(elapsed_time / duration, ease_in)
elif ease_out is not None:
progress = ease_out_progress(elapsed_time / duration, ease_out)
else:
progress = elapsed_time / duration
current_opacity = initial_opacity + (final_opacity - initial_opacity) * progress
self.attribute = current_opacity
def move(self, current_ms, duration, total_distance, start_position):
elapsed_time = current_ms - self.start_ms
if elapsed_time <= duration:
progress = elapsed_time / duration
self.attribute = start_position + (total_distance * progress)
else:
self.attribute = start_position + total_distance
self.is_finished = True
def texture_change(self, current_ms, duration, textures):
elapsed_time = current_ms - self.start_ms
if elapsed_time <= duration:
for start, end, index in textures:
if start < elapsed_time <= end:
self.attribute = index
else:
self.is_finished = True