Another little component example:
I’m kind of experimenting with this style which I’ve seen in other games.
The <.dialog>
component uses a couple of attributes dialog_tag
and dialog_color
, added to the actors, as well as their existing name
attribute so it can style & attribute the dialog without a lot of noise:
<.dialog char={player}>Can I help you?</.dialog>
<.dialog char={gangster}>Where's the cash?</.dialog>
Looks pretty neat to my eye.
@styles {
.dialog-box {
margin-bottom: 1.5rem;
}
.speaker-name {
display: inline-block;
background: #485fc7;
color: white;
padding: 0.5rem 1rem;
border-radius: 4px 4px 0 0;
margin-bottom: 0;
}
.dialog-content {
background: #f5f5f5;
border: 1px solid #dbdbdb;
border-radius: 0 4px 4px 4px;
padding: 1.25rem;
}
}
@component dialog (bindings, assigns, content) => {
const char = assigns["char"];
const tag = char.dialog_tag ? ` (${char.dialog_tag})` : "";
const color = char.dialog_color || "#485fc7";
return `<div class="dialog-box"><p class="speaker-name is-size-5" style="background: ${color}">${char.name}${tag}</p><div class="dialog-content">${content}</div></div>`;
}
@card c_player_speaks {
bindings: [
player: #player
gangster: #mob_boss
]
content: ```
<.dialog char={player}>Can I help you?</.dialog>
<.dialog char={gangster}>Where's the cash?</.dialog>
```
}
@player {
dialog_color: "#485fc7"
dialog_tag: "You"
}
@actor mob_boss {
dialog_color: "#c3195d"
dialog_tag: "Mob Boss"
}
As an aside this use of components makes use of the attr={expr}
syntax for passing dynamic expressions to components that I totally pinched from Phoenix LiveView.