From b2847368fbca26a53f099f83edf6f6f84e340fc7 Mon Sep 17 00:00:00 2001 From: asakura42 <51290160+asakura42@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:25:57 +0000 Subject: [PATCH 1/4] Added blinking --- src/main.nim | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main.nim b/src/main.nim index a7354ed..8b265ab 100644 --- a/src/main.nim +++ b/src/main.nim @@ -13,12 +13,17 @@ Printing = true TalkThreshold = 3000 TalkCooldown = 200 +[ Animation ] +BlinkDuration = 100 +BlinkInterval = 2000 + [ Window ] WindowWidth = 640 WindowHeight = 360 [ Animation Frames ] Quiet = "assets/popcat1.png" +EyeBlink = "assets/popcatblinks.png" Talk1 = "assets/popcat2.png" Talk2 = "assets/popcat3.png" """ @@ -55,10 +60,13 @@ let debugPrints = cfg.getSectionValue("Debug", "Printing") == "true" talkThreshold = cfg.getSectionValue("Audio", "TalkThreshold", "1000").parseInt() talkCooldown = cfg.getSectionValue("Audio", "TalkCooldown", "200").parseInt() + blinkDuration = cfg.getSectionValue("Animation", "BlinkDuration", "100").parseInt() + blinkInterval = cfg.getSectionValue("Animation", "BlinkInterval", "2000").parseInt() windowWidth = cfg.getSectionValue("Window", "WindowWidth", "640").parseInt() windowHeight = cfg.getSectionValue("Window", "WindowHeight", "480").parseInt() spritePaths = [ prefix & "/" & cfg.getSectionValue("Animation Frames", "Quiet"), + prefix & "/" & cfg.getSectionValue("Animation Frames", "EyeBlink"), prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk1"), prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk2"), ] @@ -78,6 +86,8 @@ var wasTalking = false startedTalkingAt: uint64 = 0 stoppedTalkingAt: uint64 = 0 + lastBlinkTime: uint32 = 0 + for path in spritePaths: sprites.add loadTexture(path) @@ -115,8 +125,17 @@ while not quit: stoppedTalkingAt = ticks wasTalking = isTalking + let + currentTime = getTicks() + timeSinceLastBlink = currentTime - lastBlinkTime + if isTalking or ticks - stoppedTalkingAt < talkCooldown.uint32: - frame = 1 + ticks.int div 250 mod 2 + frame = 2 + ticks.int div 250 mod 2 + elif timeSinceLastBlink >= blinkInterval.uint32: + if timeSinceLastBlink - blinkDuration.uint32 <= blinkInterval.uint32: + frame = 1 + else: + lastBlinkTime = currentTime else: frame = 0 From a6417582998a1561e5826f3301f2ccfd18873460 Mon Sep 17 00:00:00 2001 From: asakura42 <51290160+asakura42@users.noreply.github.com> Date: Thu, 28 Sep 2023 23:06:13 +0000 Subject: [PATCH 2/4] Added emotions --- src/main.nim | 77 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/src/main.nim b/src/main.nim index 8b265ab..ed4f3e4 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,4 +1,3 @@ - import ./sdl2, nimPNG, math, parsecfg, strutils, os proc loadTexture(path: string): TexturePtr @@ -26,11 +25,17 @@ Quiet = "assets/popcat1.png" EyeBlink = "assets/popcatblinks.png" Talk1 = "assets/popcat2.png" Talk2 = "assets/popcat3.png" + +[ Emotions ] +Amused = "assets/amused.png" +Good = "assets/good.png" +Bad = "assets/bad.png" """ var prefix = getCurrentDir() configPath = prefix & "/config.cfg" + keyIsPressed = false let params = commandLineParams() @@ -69,6 +74,9 @@ let prefix & "/" & cfg.getSectionValue("Animation Frames", "EyeBlink"), prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk1"), prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk2"), + prefix & "/" & cfg.getSectionValue("Emotions", "Amused"), + prefix & "/" & cfg.getSectionValue("Emotions", "Good"), + prefix & "/" & cfg.getSectionValue("Emotions", "Bad"), ] waggleMaxAngle = cfg.getSectionValue("Waggle", "MaxAngle", "5.0").parseFloat() wagglePeriod = cfg.getSectionValue("Waggle", "Period", "1.0").parseFloat() @@ -112,32 +120,53 @@ while not quit: case e.kind: of QuitEvent: quit = true + of KeyDown: + case e.key.keysym.sym + of K_1: + frame = 4 # frame for Amused + keyIsPressed = true + of K_2: + frame = 5 # frame for Good + keyIsPressed = true + of K_3: + frame = 6 # frame for Bad + keyIsPressed = true + else: + discard + of KeyUp: + case e.key.keysym.sym + of K_1, K_2, K_3: + frame = 0 # Back to default frame + keyIsPressed = false + else: + discard else: discard - let - isTalking = averageGain > talkThreshold.float - ticks = getTicks() - - if not wasTalking and isTalking: - startedTalkingAt = ticks - if wasTalking and not isTalking: - stoppedTalkingAt = ticks - wasTalking = isTalking - - let - currentTime = getTicks() - timeSinceLastBlink = currentTime - lastBlinkTime - - if isTalking or ticks - stoppedTalkingAt < talkCooldown.uint32: - frame = 2 + ticks.int div 250 mod 2 - elif timeSinceLastBlink >= blinkInterval.uint32: - if timeSinceLastBlink - blinkDuration.uint32 <= blinkInterval.uint32: - frame = 1 - else: - lastBlinkTime = currentTime - else: - frame = 0 + if not keyIsPressed: + let + isTalking = averageGain > talkThreshold.float + ticks = getTicks() + + if not wasTalking and isTalking: + startedTalkingAt = ticks + if wasTalking and not isTalking: + stoppedTalkingAt = ticks + wasTalking = isTalking + + let + currentTime = getTicks() + timeSinceLastBlink = currentTime - lastBlinkTime + + if isTalking or ticks - stoppedTalkingAt < talkCooldown.uint32: + frame = 2 + ticks.int div 250 mod 2 + elif timeSinceLastBlink >= blinkInterval.uint32: + if timeSinceLastBlink - blinkDuration.uint32 <= blinkInterval.uint32: + frame = 1 + else: + lastBlinkTime = currentTime + else: + frame = 0 renderer.setDrawColor(0, 255, 0, 0) renderer.clear() From 5fe1e2d2453a5a0820768faa0735edbe05ab5b62 Mon Sep 17 00:00:00 2001 From: asakura42 <51290160+asakura42@users.noreply.github.com> Date: Thu, 28 Sep 2023 23:09:28 +0000 Subject: [PATCH 3/4] Update config.cfg --- config.cfg | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/config.cfg b/config.cfg index 785133d..c9e432c 100644 --- a/config.cfg +++ b/config.cfg @@ -13,8 +13,16 @@ WindowHeight = 360 [ Animation Frames ] Quiet = "assets/popcat1.png" +EyeBlink = "assets/popcat3.png" Talk1 = "assets/popcat2.png" Talk2 = "assets/popcat3.png" +Talk3 = "assets/popcat3.png" + +[ Emotions ] +Amused = "assets/popcat3.png" +Good = "assets/popcat3.png" +Bad = "assets/popcat3.png" + [ Waggle ] MaxAngle = 5.0 From db9c42bfecb196092c8c5c14f7ecb7ae0895b9a9 Mon Sep 17 00:00:00 2001 From: asakura42 <51290160+asakura42@users.noreply.github.com> Date: Thu, 28 Sep 2023 23:10:09 +0000 Subject: [PATCH 4/4] little changes --- src/main.nim | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.nim b/src/main.nim index ed4f3e4..32d3299 100644 --- a/src/main.nim +++ b/src/main.nim @@ -74,6 +74,7 @@ let prefix & "/" & cfg.getSectionValue("Animation Frames", "EyeBlink"), prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk1"), prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk2"), + prefix & "/" & cfg.getSectionValue("Animation Frames", "Talk3"), prefix & "/" & cfg.getSectionValue("Emotions", "Amused"), prefix & "/" & cfg.getSectionValue("Emotions", "Good"), prefix & "/" & cfg.getSectionValue("Emotions", "Bad"), @@ -123,13 +124,13 @@ while not quit: of KeyDown: case e.key.keysym.sym of K_1: - frame = 4 # frame for Amused + frame = 5 # frame for Amused keyIsPressed = true of K_2: - frame = 5 # frame for Good + frame = 6 # frame for Good keyIsPressed = true of K_3: - frame = 6 # frame for Bad + frame = 7 # frame for Bad keyIsPressed = true else: discard @@ -159,7 +160,7 @@ while not quit: timeSinceLastBlink = currentTime - lastBlinkTime if isTalking or ticks - stoppedTalkingAt < talkCooldown.uint32: - frame = 2 + ticks.int div 250 mod 2 + frame = 2 + ticks.int div 250 mod 3 elif timeSinceLastBlink >= blinkInterval.uint32: if timeSinceLastBlink - blinkDuration.uint32 <= blinkInterval.uint32: frame = 1