-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Sprite changes to Logic Gates to show the input/output state #33277
base: master
Are you sure you want to change the base?
Conversation
use separate layers for a/b/output and none of the evil stuff is needed |
Is this better? im not exactly the best coder :godo: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
There's some small code style issues, and a bug where the code accidentally sets InputA instead of InputB.
Other than that, I like the change and the rest of the visualizer/layering stuff looks good (I don't have personal experience with any of it).
Also, I'm not a maintainer, so this is just a peer review. I may have made some mistakes in my review, or have missed things, so take this review with a grain of salt.
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing -- there should only be one line between fields.
@@ -18,6 +18,19 @@ public sealed partial class LogicGateComponent : Component | |||
[DataField] | |||
public LogicGate Gate = LogicGate.Or; | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing -- there should only be one line between fields.
[DataField] | ||
public LogicGateState Output = LogicGateState.Low; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing -- there should only be one line between fields.
{ | ||
case LogicGate.Or: | ||
output = a || b; | ||
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | ||
break; | ||
case LogicGate.And: | ||
output = a && b; | ||
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | ||
break; | ||
case LogicGate.Xor: | ||
output = a != b; | ||
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | ||
break; | ||
case LogicGate.Nor: | ||
output = !(a || b); | ||
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | ||
break; | ||
case LogicGate.Nand: | ||
output = !(a && b); | ||
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | ||
break; | ||
case LogicGate.Xnor: | ||
output = a == b; | ||
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can extract down the _appearance.SetData
call below the switch, as it's the same for every code path.
Also, there's a missing default
case.
{ | |
case LogicGate.Or: | |
output = a || b; | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | |
break; | |
case LogicGate.And: | |
output = a && b; | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | |
break; | |
case LogicGate.Xor: | |
output = a != b; | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | |
break; | |
case LogicGate.Nor: | |
output = !(a || b); | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | |
break; | |
case LogicGate.Nand: | |
output = !(a && b); | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | |
break; | |
case LogicGate.Xnor: | |
output = a == b; | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); | |
break; | |
} | |
{ | |
case LogicGate.Or: | |
output = a || b; | |
break; | |
case LogicGate.And: | |
output = a && b; | |
break; | |
case LogicGate.Xor: | |
output = a != b; | |
break; | |
case LogicGate.Nor: | |
output = !(a || b); | |
break; | |
case LogicGate.Nand: | |
output = !(a && b); | |
break; | |
case LogicGate.Xnor: | |
output = a == b; | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(); | |
} | |
_appearance.SetData(uid, LogicGateVisuals.Output, output ? LogicGateState.High : LogicGateState.Low); |
If you're adventurous, you can also just refactor the switch statement into a switch expression
@@ -3,6 +3,7 @@ | |||
using Content.Shared.DeviceLinking; | |||
using Content.Shared.Examine; | |||
using Content.Shared.Interaction; | |||
using Content.Shared.Lathe; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import?
using Content.Shared.Lathe; |
} | ||
else if (args.Port == comp.InputPortB) | ||
{ | ||
comp.StateB = state; | ||
_appearance.SetData(uid, LogicGateVisuals.InputA, UpdateState(state)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be modifying InputB instead of InputA?
_appearance.SetData(uid, LogicGateVisuals.InputA, UpdateState(state)); | |
_appearance.SetData(uid, LogicGateVisuals.InputB, UpdateState(state)); |
LogicGateState UpdateState(SignalState state) | ||
{ | ||
switch (state) | ||
{ | ||
case SignalState.High: | ||
return LogicGateState.High; | ||
break; | ||
case SignalState.Low: | ||
return LogicGateState.Low; | ||
break; | ||
default: | ||
return LogicGateState.Momentary; | ||
break; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're missing an access modifier on the function.
Also, you can make it more succinct by using a switch expression.
Finally, it makes sense to rename the function to SignalToLogicState
, as the previous name kind-of implies some kind of side effect ("UpdateState"), when really all it does is map all of the SignalState
s to their correct LogicGateState
.
LogicGateState UpdateState(SignalState state) | |
{ | |
switch (state) | |
{ | |
case SignalState.High: | |
return LogicGateState.High; | |
break; | |
case SignalState.Low: | |
return LogicGateState.Low; | |
break; | |
default: | |
return LogicGateState.Momentary; | |
break; | |
} | |
private static LogicGateState SignalToLogicState(SignalState state) | |
{ | |
return state switch | |
{ | |
SignalState.High => LogicGateState.High, | |
SignalState.Low => LogicGateState.Low, | |
_ => LogicGateState.Momentary, | |
}; |
@@ -63,6 +69,18 @@ | |||
Nor: { state: nor } | |||
Nand: { state: nand } | |||
Xnor: { state: xnor } | |||
enum.LogicGateVisuals.InputA: | |||
enum.LogicGateLayers.InputA: | |||
High: {state: logic_a } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a space.
High: {state: logic_a } | |
High: { state: logic_a } |
@@ -36,6 +36,12 @@ | |||
layers: | |||
- state: base | |||
- state: logic | |||
- state: empty | |||
map: ["enum.LogicGateLayers.InputA"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spacing.
map: ["enum.LogicGateLayers.InputA"] | |
map: [ "enum.LogicGateLayers.InputA" ] |
…o the function UpdateState
About the PR
Gates now show the state of their inputs and outputs on the sprite
Why / Balance
Its easier to use them if you can see the states
Media
2024-11-12.10-41-36.mp4
Requirements
Changelog